audiobank-client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/COPYRIGHT ADDED
@@ -0,0 +1,14 @@
1
+ Copyright © 2013 Alban Peignier, Florent Peyraud
2
+
3
+ This program is free software: you can redistribute it and/or modify
4
+ it under the terms of the GNU General public License as published by
5
+ the Free Software Foundation, either version 3 of the License, or
6
+ (at your option) any later version.
7
+
8
+ This program is distributed in the hope that it will be useful,
9
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ GNU General public License for more details.
12
+
13
+ You should have received a copy of the GNU General public License
14
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in audiobank-client.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rb-inotify', '~> 0.9'
8
+ gem 'libnotify'
9
+ end
data/Guardfile ADDED
@@ -0,0 +1,13 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
10
+ guard 'bundler' do
11
+ watch('Gemfile')
12
+ watch(/^.+\.gemspec/)
13
+ end
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Audiobank::Client
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'audiobank-client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install audiobank-client
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir['tasks/*.rake'].each { |t| load t }
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'audiobank/client/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "audiobank-client"
8
+ gem.version = Audiobank::Client::VERSION
9
+ gem.authors = ["Alban Peignier", "Florent Peyraud"]
10
+ gem.email = ["alban@tryphon.eu", "florent@tryphon.eu" ]
11
+ gem.description = %q{Manage AudioBank documents from command-line or Ruby}
12
+ gem.summary = %q{AudioBank API client}
13
+ gem.homepage = "http://projects.tryphon.eu/projects/audiobank-client"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency "httparty"
21
+ gem.add_runtime_dependency "activesupport", "< 4"
22
+ gem.add_runtime_dependency "json"
23
+ gem.add_runtime_dependency "virtus"
24
+ gem.add_runtime_dependency "null_logger"
25
+
26
+ gem.add_development_dependency "rake"
27
+ gem.add_development_dependency "simplecov"
28
+ gem.add_development_dependency "rspec"
29
+ gem.add_development_dependency "fakeweb"
30
+ gem.add_development_dependency "guard"
31
+ gem.add_development_dependency "guard-rspec"
32
+ gem.add_development_dependency "guard-bundler"
33
+ end
data/bin/audiobank ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'audiobank-client'
4
+
5
+ # Audiobank::Account.base_uri "audiobank.local"
6
+
7
+ account = Audiobank::Account.new(ARGV.shift)
8
+ while not ARGV.empty?
9
+ account.documents.import ARGV.shift
10
+ end
@@ -0,0 +1,30 @@
1
+ require "audiobank/client/version"
2
+
3
+ require 'logger'
4
+ require 'json'
5
+
6
+ require "virtus"
7
+
8
+ require "null_logger"
9
+ require "active_support/core_ext/module/attribute_accessors"
10
+ require "active_support/core_ext/class/attribute_accessors"
11
+
12
+ require "active_support/core_ext/module/delegation"
13
+ require "active_support/json"
14
+ require "active_support/core_ext/object/to_json"
15
+
16
+ require 'net/ftp'
17
+
18
+ module Audiobank
19
+ module Client
20
+
21
+ @@logger = NullLogger.instance
22
+ mattr_accessor :logger
23
+
24
+ end
25
+ end
26
+
27
+
28
+ require "audiobank/document"
29
+ require "audiobank/documents"
30
+ require "audiobank/account"
@@ -0,0 +1,46 @@
1
+ require 'httparty'
2
+
3
+ class Audiobank::Account
4
+ include HTTParty
5
+ base_uri 'audiobank.tryphon.eu'
6
+ headers "Content-Type" => "application/json"
7
+ format :json
8
+
9
+ attr_accessor :token
10
+
11
+ def initialize(token)
12
+ @token = token
13
+ end
14
+
15
+ def default_options
16
+ { :query => { :auth_token => token } }
17
+ end
18
+
19
+ def get(url, &block)
20
+ process_response self.class.get(url, default_options), &block
21
+ end
22
+
23
+ def post(url, attributes = {}, &block)
24
+ process_response self.class.post(url, default_options.merge(:body => attributes.to_json)), &block
25
+ end
26
+
27
+ def process_response(response, &block)
28
+ if block_given?
29
+ yield response
30
+ else
31
+ response
32
+ end
33
+ end
34
+
35
+ def document(id)
36
+ Audiobank::Client.logger.info "Request document information : #{id}"
37
+ get "/documents/#{id}.json" do |response|
38
+ Audiobank::Document.new(response) if response.code == 200
39
+ end
40
+ end
41
+
42
+ def documents
43
+ @documents ||= Audiobank::Documents.new(self)
44
+ end
45
+
46
+ end
@@ -0,0 +1,5 @@
1
+ module Audiobank
2
+ module Client
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,41 @@
1
+ class Audiobank::Document
2
+ include Virtus
3
+
4
+ attribute :id, Integer
5
+ attribute :download_count, Integer
6
+ attribute :description, String
7
+ attribute :length, Integer
8
+ attribute :title, String
9
+ attribute :cast, String
10
+ attribute :upload, String
11
+
12
+ attr_accessor :account
13
+ attr_accessor :errors
14
+
15
+ def errors=(errors)
16
+ @errors = (String === errors ? JSON.parse(errors) : errors)
17
+ end
18
+
19
+ def valid?
20
+ errors.blank?
21
+ end
22
+
23
+ def upload_uri
24
+ @upload_uri ||= URI.parse(upload)
25
+ end
26
+
27
+ def upload!(file)
28
+ Net::FTP.open(upload_uri.host) do |ftp|
29
+ ftp.login
30
+ ftp.chdir upload_uri.path
31
+ ftp.putbinaryfile file
32
+ end
33
+
34
+ self
35
+ end
36
+
37
+ def confirm
38
+ account.post "/documents/#{id}/upload/confirm.json"
39
+ end
40
+
41
+ end
@@ -0,0 +1,29 @@
1
+ class Audiobank::Documents
2
+
3
+ attr_accessor :account
4
+
5
+ def initialize(account)
6
+ @account = account
7
+ end
8
+
9
+ delegate :post, :to => :account
10
+
11
+ def create(attributes)
12
+ Audiobank::Client.logger.debug "Create AudioBank document : #{attributes.inspect}"
13
+ post "/documents.json", :document => attributes do |response|
14
+ Audiobank::Document.new(response).tap do |document|
15
+ document.account = account
16
+ end
17
+ end
18
+ end
19
+
20
+ def import(file, attributes = {})
21
+ attributes = {
22
+ :title => File.basename(file, File.extname(file)),
23
+ :description => "Uploaded at #{Time.now}"
24
+ }.merge(attributes)
25
+
26
+ create(attributes).upload!(file).confirm
27
+ end
28
+
29
+ end
data/log/.gitkeep ADDED
File without changes
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Audiobank::Account do
4
+
5
+ subject { Audiobank::Account.new("secret") }
6
+
7
+ before(:each) do
8
+ FakeWeb.allow_net_connect = false
9
+ end
10
+
11
+ describe "#get" do
12
+
13
+ it "should send get request to the specified url with auth_token" do
14
+ FakeWeb.register_uri :get, "http://audiobank.tryphon.eu/dummy?auth_token=secret", :body => 'dummy'
15
+ subject.get("/dummy").body.should == 'dummy'
16
+ end
17
+
18
+ end
19
+
20
+ describe "#post" do
21
+
22
+ it "should send post request to the specified url with auth_token" do
23
+ FakeWeb.register_uri :post, "http://audiobank.tryphon.eu/dummy?auth_token=secret", :body => 'dummy'
24
+ subject.post("/dummy", :key => "value")
25
+ FakeWeb.last_request.body.should == '{"key":"value"}'
26
+ end
27
+
28
+ end
29
+
30
+ describe "#document" do
31
+
32
+ let(:json_response) {
33
+ '{"download_count":5,"description":"Dummy","length":1744,"cast":"8a9cygzn","id":721,"upload":"ftp://audiobank.tryphon.eu/pqxijmcetmodn25s/","title":"Test"}'
34
+ }
35
+
36
+ it "should retrieve invoke http://audiobank.tryphon.eu/documents/<id>.json" do
37
+ FakeWeb.register_uri :get, "http://audiobank.tryphon.eu/documents/1.json?auth_token=secret", :body => json_response
38
+ subject.document(1).title.should == "Test"
39
+ end
40
+
41
+ it "should return nil if documet is not found" do
42
+ FakeWeb.register_uri :get, "http://audiobank.tryphon.eu/documents/1.json?auth_token=secret", :status => ["404", "Not Found"]
43
+ subject.document(1).should be_nil
44
+ end
45
+
46
+ end
47
+
48
+ describe "#documents" do
49
+
50
+ it "should be associated to this account" do
51
+ subject.documents.account.should == subject
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe Audiobank::Document do
4
+
5
+ subject { Audiobank::Document.new :id => 1, :account => account }
6
+
7
+ let(:account) { Audiobank::Account.new("secret") }
8
+
9
+ describe "#errors=" do
10
+
11
+ let(:errors) { %w{first second} }
12
+
13
+ it "should parse JSON array" do
14
+ subject.errors = errors.to_json
15
+ subject.errors.should == errors
16
+ end
17
+
18
+ it "should accept errors array" do
19
+ subject.errors = errors
20
+ subject.errors.should == errors
21
+ end
22
+
23
+ end
24
+
25
+ it "should be valid when errors is blank" do
26
+ subject.errors = []
27
+ subject.should be_valid
28
+ end
29
+
30
+ describe "#upload_uri" do
31
+
32
+ it "should parse upload url" do
33
+ subject.upload = "ftp://audiobank.tryphon.eu/directory"
34
+ subject.upload_uri.host.should == "audiobank.tryphon.eu"
35
+ subject.upload_uri.path.should == "directory"
36
+ end
37
+
38
+ end
39
+
40
+ describe "confirm" do
41
+
42
+ it "should post to /documents/:id/upload/confirm.json" do
43
+ account.should_receive(:post).with "/documents/#{subject.id}/upload/confirm.json"
44
+ subject.confirm
45
+ end
46
+
47
+ end
48
+
49
+ describe "upload!" do
50
+
51
+ it "should upload file in ftp" do
52
+ pending "Use fake_ftp"
53
+ end
54
+
55
+ it "should return the document" do
56
+ pending
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Audiobank::Documents do
4
+
5
+ let(:account) { Audiobank::Account.new "secret" }
6
+
7
+ subject { Audiobank::Documents.new account }
8
+
9
+ before(:each) do
10
+ FakeWeb.allow_net_connect = false
11
+ end
12
+
13
+ describe "#create" do
14
+
15
+ it "should post to /documents.json the document attributes" do
16
+ account.should_receive(:post).with("/documents.json", :document => { :title => "dummy" })
17
+ subject.create :title => "dummy"
18
+ end
19
+
20
+ it "should return a Document with received attributes" do
21
+ account.register_uri :post, "/documents.json", :body => {"title" => "dummy"}.to_json
22
+ subject.create({}).title.should == "dummy"
23
+ end
24
+
25
+ it "should return a Document associated to the account" do
26
+ account.register_uri :post, "/documents.json"
27
+ subject.create({}).account.should == account
28
+ end
29
+
30
+ end
31
+
32
+ describe "#import" do
33
+
34
+ let(:document) { mock }
35
+ let(:file) { "/path/to/file" }
36
+
37
+ before do
38
+ subject.stub :create => document
39
+ document.stub :upload! => document, :confirm => true
40
+ end
41
+
42
+ it "should create a Document with specified attributes" do
43
+ subject.should_receive(:create).with(hash_including(:title => "dummy")).and_return(document)
44
+ subject.import file, :title => "dummy"
45
+ end
46
+
47
+ it "should upload the specified file" do
48
+ document.should_receive(:upload!).with(file)
49
+ subject.import file
50
+ end
51
+
52
+ it "should confirm the upload" do
53
+ document.should_receive(:confirm)
54
+ subject.import file
55
+ end
56
+
57
+ it "should use the filename (without extension) as default title" do
58
+ subject.should_receive(:create).with(hash_including(:title => "filename")).and_return(document)
59
+ subject.import "/path/to/filename.flac"
60
+ end
61
+
62
+ it "should use a default description" do
63
+ subject.should_receive(:create).with(hash_including(:description)).and_return(document)
64
+ subject.import file
65
+ end
66
+
67
+ end
68
+
69
+ end