rmb 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rmb/client.rb ADDED
@@ -0,0 +1,205 @@
1
+ class Rmb::Client
2
+ attr_accessor :service
3
+
4
+ def initialize
5
+ self.service = Rmb::Api.new
6
+ end
7
+
8
+ def drop(drop_name)
9
+ handle(:drop, self.service.drop(drop_name))
10
+ end
11
+
12
+ def all_drops(page = 1)
13
+ handle(:drops, self.service.all_drops(page))
14
+ end
15
+
16
+ def generate_drop_url(drop)
17
+ self.service.generate_drop_url(drop.name)
18
+ end
19
+
20
+ def create_drop(params = {})
21
+ handle(:drop, self.service.create_drop(params))
22
+ end
23
+
24
+ def change_drop_name(drop, new_name)
25
+ handle(:drop, self.service.change_drop_name(drop.name,new_name))
26
+ drop.name = new_name
27
+ drop
28
+ end
29
+
30
+ def update_drop(drop)
31
+ params = { :description => drop.description, :admin_email => drop.admin_email,
32
+ :email_key => drop.email_key, :chat_password => drop.chat_password,
33
+ :expiration_length => drop.expiration_length, :password => drop.password,
34
+ :admin_password => drop.admin_password }
35
+ handle(:drop, self.service.update_drop(drop.name,params))
36
+ end
37
+
38
+ def empty_drop(drop)
39
+ r = handle(:response, self.service.empty_drop(drop.name))
40
+ r["result"]
41
+ end
42
+
43
+ def delete_drop(drop)
44
+ r = handle(:response, self.service.delete_drop(drop.name))
45
+ r["result"]
46
+ end
47
+
48
+ def promote_nick(drop,nick)
49
+ r = handle(:response, self.service.promote_nick(drop.name,nick))
50
+ r["result"]
51
+ end
52
+
53
+ def drop_upload_code(drop)
54
+ r = handle(:response, self.service.drop_upload_code(drop.name))
55
+ r["upload_code"]
56
+ end
57
+
58
+ def create_link(drop, url, title = nil, description = nil)
59
+ a = handle(:asset, self.service.create_link(drop.name, url, title, description))
60
+ a.drop = drop
61
+ a
62
+ end
63
+
64
+ def create_note(drop, contents, title = nil, description = nil)
65
+ a = handle(:asset, self.service.create_note(drop.name, contents, title, description))
66
+ a.drop = drop
67
+ a
68
+ end
69
+
70
+ def add_file(drop, file_path, description = nil, convert_to = nil, pingback_url = nil, output_locations = nil)
71
+ a = handle(:asset, self.service.add_file(drop.name, file_path, description, convert_to, pingback_url, output_locations))
72
+ a.drop = drop
73
+ a
74
+ end
75
+
76
+ def add_file_from_url(drop, url, description = nil, convert_to = nil, pingback_url = nil)
77
+ a = handle(:asset, self.service.add_file_from_url(drop.name, url, description, convert_to, pingback_url))
78
+ a.drop = drop
79
+ a
80
+ end
81
+
82
+ def assets(drop, page = 1, order = :oldest)
83
+ assets = handle(:assets, self.service.assets(drop.name,page,order))
84
+ assets.each{|a| a.drop = drop}
85
+ assets
86
+ end
87
+
88
+ def asset(drop, asset_name)
89
+ a = handle(:asset, self.service.asset(drop.name,asset_name))
90
+ a.drop = drop
91
+ a
92
+ end
93
+
94
+ def generate_asset_url(asset)
95
+ self.service.generate_asset_url(asset.drop.name, asset.name)
96
+ end
97
+
98
+ def generate_original_file_url(asset)
99
+ self.service.generate_original_file_url(asset.drop.name, asset.name)
100
+ end
101
+
102
+ def update_asset(asset)
103
+ params = { :title => asset.title, :description => asset.description, :url => asset.url, :contents => asset.contents }
104
+ a = handle(:asset, self.service.update_asset(asset.drop.name,asset.name,params))
105
+ a.drop = asset.drop
106
+ a
107
+ end
108
+
109
+ def change_asset_name(asset, new_name)
110
+ handle(:asset, self.service.change_asset_name(asset.drop.name,asset.name,new_name))
111
+ asset.name = new_name
112
+ asset
113
+ end
114
+
115
+ def delete_asset(asset)
116
+ r = handle(:response, self.service.delete_asset(asset.drop.name, asset.id))
117
+ r["result"]
118
+ end
119
+
120
+ def delete_role(asset, role, location=nil)
121
+ r = handle(:response, self.service.delete_role(asset.drop.name, asset.id, role, location))
122
+ r["result"]
123
+ end
124
+
125
+ def send_asset_to_drop(asset, target_drop)
126
+ r = handle(:response, self.service.send_asset_to_drop(asset.drop.name, asset.name, target_drop.name))
127
+ r["result"]
128
+ end
129
+
130
+ def copy_asset(asset,target_drop)
131
+ r = handle(:response, self.service.copy_asset(asset.drop.name,asset.name,target_drop.name))
132
+ r["result"]
133
+ end
134
+
135
+ def move_asset(asset,target_drop)
136
+ r = handle(:response, self.service.move_asset(asset.drop.name,asset.name,target_drop.name))
137
+ r["result"]
138
+ end
139
+
140
+ def create_pingback_subscription(drop, url, events)
141
+ s = handle(:subscription, self.service.create_pingback_subscription(drop.name, url, events))
142
+ s.drop = drop
143
+ s
144
+ end
145
+
146
+ def subscriptions(drop, page = 1)
147
+ subscriptions = handle(:subscriptions, self.service.subscriptions(drop.name, page))
148
+ subscriptions.each{|s| s.drop = drop}
149
+ subscriptions
150
+ end
151
+
152
+ def delete_subscription(subscription)
153
+ r = handle(:response, self.service.delete_subscription(subscription.drop.name,subscription.id))
154
+ r["result"]
155
+ end
156
+
157
+ def job(id, drop_name, asset_name_or_id)
158
+ handle(:job, self.service.job(id, drop_name, asset_name_or_id))
159
+ end
160
+
161
+ def create_job(job = {})
162
+ handle(:job, self.service.create_job(job))
163
+ end
164
+
165
+ private
166
+
167
+ def handle(type, response)
168
+ if response.code != 200
169
+ parse_response(response)
170
+ end
171
+
172
+ case type
173
+ when :drop then return Rmb::Drop.new(response)
174
+ when :drops then return response['drops'].collect{|d| Rmb::Drop.new(d)}
175
+ when :asset then return Rmb::Asset.new(response)
176
+ when :assets then return response['assets'].collect{|a| Rmb::Asset.new(a)}
177
+ when :subscription then return Rmb::Subscription.new(response)
178
+ when :subscriptions then return response['subscriptions'].collect{|s| Rmb::Subscription.new(s)}
179
+ when :job then return Rmb::Job.new(response)
180
+ when :response then return parse_response(response)
181
+ end
182
+ end
183
+
184
+ def parse_response(response)
185
+ case response.code
186
+ when 200 then return response
187
+ when 400 then raise Rmb::RequestError, parse_error_message(response)
188
+ when 403 then raise Rmb::AuthorizationError, parse_error_message(response)
189
+ when 404 then raise Rmb::MissingResourceError, parse_error_message(response)
190
+ when 500 then raise Rmb::ServerError, "There was a problem connecting to rmb.io."
191
+ else
192
+ raise "Received an unexpected HTTP response: #{response.code} #{response.body}"
193
+ end
194
+ end
195
+
196
+ # Extracts the error message from the response for the exception.
197
+ def parse_error_message(error_hash)
198
+ if (error_hash && error_hash.is_a?(Hash) && error_hash["response"] && error_hash["response"]["message"])
199
+ return error_hash["response"]["message"]
200
+ else
201
+ return "There was a problem connecting to rmb.io."
202
+ end
203
+ end
204
+
205
+ end
data/lib/rmb/drop.rb ADDED
@@ -0,0 +1,89 @@
1
+ class Rmb::Drop < Rmb::Resource
2
+
3
+ attr_accessor :name, :email, :description, :expires_at, :expiration_length,
4
+ :max_bytes, :current_bytes, :asset_count, :chat_password, :password,
5
+ :admin_password, :admin_email, :email_key
6
+
7
+ # Gets a list of assets associated with the Drop. Paginated at
8
+ def assets(page = 1, order = :oldest)
9
+ Rmb::Resource.client.assets(self, page, order)
10
+ end
11
+
12
+ # Finds a drop with +name+
13
+ def self.find(name)
14
+ Rmb::Resource.client.drop(name)
15
+ end
16
+
17
+ # Finds all the drops associated with an API account by page
18
+ def self.find_all(page = 1)
19
+ Rmb::Resource.client.all_drops(page)
20
+ end
21
+
22
+ # Creates a drop with an +attributes+ hash.
23
+ # Valid attributes: name (string), expiration_length (string), password (string), and admin_password (string)
24
+ # Descriptions can be found here: http://backbonedocs.drop.io/Ruby-API-Client-Library:Drop
25
+ def self.create(attributes = {})
26
+ Rmb::Resource.client.create_drop(attributes)
27
+ end
28
+
29
+ # Changes the name of a drop.
30
+ def change_name(new_name)
31
+ Rmb::Resource.client.change_drop_name(self,new_name)
32
+ end
33
+
34
+ # Empties the drop, including it's assets.
35
+ def empty
36
+ Rmb::Resource.client.empty_drop(self)
37
+ end
38
+
39
+ # Promotes a nickname in the drop chat to admin.
40
+ def promote(nick)
41
+ Rmb::Resource.client.promote_nick(self,nick)
42
+ end
43
+
44
+ # Saves the Drop.
45
+ def save
46
+ Rmb::Resource.client.update_drop(self)
47
+ end
48
+
49
+ # Deletes the Drop from the system including all associated assets.
50
+ def destroy!
51
+ Rmb::Resource.client.delete_drop(self)
52
+ end
53
+
54
+ # Adds a file to the Drop from a given +url+
55
+ def add_file_from_url(url, description = nil, convert_to = nil, pingback_url = nil)
56
+ Rmb::Resource.client.add_file_from_url(self,url,description, convert_to, pingback_url)
57
+ end
58
+
59
+ # Adds a file to the Drop given the +file_path+.
60
+ def add_file(file_path, description = nil, convert_to = nil, pingback_url = nil, output_locations = nil)
61
+ Rmb::Resource.client.add_file(self, file_path, description, convert_to, pingback_url, output_locations)
62
+ end
63
+
64
+ # Creates a note with a +title+ and +contents+
65
+ def create_note(contents, title = nil, description = nil)
66
+ Rmb::Resource.client.create_note(self, contents, title, description)
67
+ end
68
+
69
+ # Creates a link with a +url+, +title+, and +description+.
70
+ def create_link(url, title = nil, description = nil)
71
+ Rmb::Resource.client.create_link(self, url, title, description)
72
+ end
73
+
74
+ # Creates a subscription to receive POSTs from rmb.io.
75
+ def create_pingback_subscription(url, events = {})
76
+ Rmb::Resource.client.create_pingback_subscription(self,url,events)
77
+ end
78
+
79
+ # Gets a list of Subscription objects.
80
+ def subscriptions(page = 1)
81
+ Rmb::Resource.client.subscriptions(self, page)
82
+ end
83
+
84
+ # Generates an authenticated URL that will bypass any login action.
85
+ def generate_url
86
+ Rmb::Resource.client.generate_drop_url(self)
87
+ end
88
+
89
+ end
data/lib/rmb/job.rb ADDED
@@ -0,0 +1,22 @@
1
+ class Rmb::Job < Rmb::Resource
2
+
3
+ attr_accessor :id, :job_type, :using, :inputs, :outputs, :pingback_url, :state
4
+
5
+ # Finds a job for a drop and asset
6
+ def self.find(id,drop_name,asset_name_or_id)
7
+ Rmb::Resource.client.job(id, drop_name, asset_name_or_id)
8
+ end
9
+
10
+ # Creates a job
11
+ def self.create(job_type,using,inputs,outputs,pingback_url=nil)
12
+ Rmb::Resource.client.create_job({
13
+ :job_type => job_type,
14
+ :using => using,
15
+ :inputs => inputs,
16
+ :outputs => outputs,
17
+ :pingback_url => pingback_url
18
+ })
19
+ end
20
+
21
+
22
+ end
@@ -0,0 +1,13 @@
1
+ class Rmb::Resource
2
+
3
+ def initialize(params = {})
4
+ params.each do |key,val|
5
+ self.send("#{key}=", val) if self.respond_to?("#{key}=")
6
+ end
7
+ end
8
+
9
+ def self.client
10
+ @@client ||= Rmb::Client.new
11
+ end
12
+
13
+ end
@@ -0,0 +1,10 @@
1
+ class Rmb::Subscription < Rmb::Resource
2
+ attr_accessor :id, :username, :message, :type, :drop
3
+
4
+ # Destroys the given subscription
5
+ def destroy!
6
+ Rmb::Resource.client.delete_subscription(self)
7
+ nil
8
+ end
9
+
10
+ end
@@ -0,0 +1,3 @@
1
+ module Rmb
2
+ VERSION = '3.0.0'
3
+ end
data/lib/rmb.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'rmb/version'
2
+
3
+ module Rmb
4
+ class Error < StandardError; end
5
+
6
+ class MissingResourceError < Error; end
7
+ class AuthorizationError < Error; end
8
+ class RequestError < Error; end
9
+ class ServerError < Error; end
10
+
11
+ class Config
12
+ class << self
13
+ attr_accessor :api_key, :api_secret, :base_url, :api_url, :upload_url, :version, :debug, :timeout
14
+ end
15
+ end
16
+
17
+ end
18
+
19
+ Rmb::Config.base_url = "http://down.rmb.io"
20
+ Rmb::Config.api_url = "http://api.rmb.io"
21
+ Rmb::Config.upload_url = "http://up.rmb.io/upload"
22
+ Rmb::Config.version = "3.0"
23
+ Rmb::Config.debug = false
24
+ Rmb::Config.timeout = 60 # Default in Ruby
25
+
26
+ require 'rmb/api'
27
+ require 'rmb/client'
28
+ require 'rmb/resource'
29
+ require 'rmb/drop'
30
+ require 'rmb/asset'
31
+ require 'rmb/subscription'
32
+ require 'rmb/job'
33
+
data/rmb.gemspec ADDED
@@ -0,0 +1,53 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{rmb}
5
+ s.version = "3.0.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Jake Good, Eric Skiff, Kunal Shah, Seth Thomas Rasmussen, Matthew Rathbone", "Bryan Woods"]
9
+ s.date = %q{2010-09-21}
10
+ s.description = %q{A Ruby client library for the Rich Media Backbone (RMB) API (http://rmb.io). Please send all feedback to backbone@dropio.com}
11
+ s.email = ["jake@dropio.com", "eric@dropio.com", "kunal@dropio.com", "seth@dropio.com", "matthew@dropio.com", "bryan@dropio.com"]
12
+ s.extra_rdoc_files = ["History.rdoc", "Readme.rdoc"]
13
+ s.files = ["Gemfile", "History.rdoc", "LICENSE.txt", "Manifest", "Rakefile", "Readme.rdoc", "rmb.gemspec", "lib/rmb.rb", "lib/rmb/api.rb", "lib/rmb/asset.rb", "lib/rmb/client.rb", "lib/rmb/drop.rb", "lib/rmb/job.rb", "lib/rmb/resource.rb", "lib/rmb/subscription.rb", "lib/rmb/version.rb", "spec/rmb/api_spec.rb", "spec/rmb/asset_spec.rb", "spec/rmb/client_spec.rb", "spec/rmb/drop_spec.rb", "spec/rmb/subscription_spec.rb", "spec/rmb_spec.rb", "spec/spec.opts", "spec/spec_helper.rb"]
14
+ s.homepage = %q{http://github.com/dropio/rmb}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Dropio", "--main", "Readme.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{rmb}
18
+ s.rubygems_version = %q{1.3.6}
19
+ s.summary = %q{A Ruby client library for the Drop.io Rich Media Backbone (RMB) API (http://rmb.io)}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
25
+ s.add_runtime_dependency(%q<mime-types>, [">= 0"])
26
+ s.add_runtime_dependency(%q<json>, [">= 0"])
27
+ s.add_runtime_dependency(%q<httparty>, ["= 0.6.1"])
28
+ s.add_runtime_dependency(%q<multipart-post>, ["= 1.0.1"])
29
+ s.add_runtime_dependency(%q<activesupport>, ["= 2.3.9"])
30
+ s.add_development_dependency(%q<rspec>, [">= 0"])
31
+ s.add_development_dependency(%q<diff-lcs>, [">= 0"])
32
+ s.add_development_dependency(%q<fakeweb>, [">= 0"])
33
+ else
34
+ s.add_dependency(%q<mime-types>, [">= 0"])
35
+ s.add_dependency(%q<json>, [">= 0"])
36
+ s.add_dependency(%q<httparty>, ["= 0.6.1"])
37
+ s.add_dependency(%q<multipart-post>, ["= 1.0.1"])
38
+ s.add_runtime_dependency(%q<activesupport>, ["= 2.3.9"])
39
+ s.add_dependency(%q<rspec>, [">= 0"])
40
+ s.add_dependency(%q<diff-lcs>, [">= 0"])
41
+ s.add_dependency(%q<fakeweb>, [">= 0"])
42
+ end
43
+ else
44
+ s.add_dependency(%q<mime-types>, [">= 0"])
45
+ s.add_dependency(%q<json>, [">= 0"])
46
+ s.add_dependency(%q<httparty>, ["= 0.6.1"])
47
+ s.add_dependency(%q<multipart-post>, ["= 1.0.1"])
48
+ s.add_runtime_dependency(%q<activesupport>, ["= 2.3.9"])
49
+ s.add_dependency(%q<rspec>, [">= 0"])
50
+ s.add_dependency(%q<diff-lcs>, [">= 0"])
51
+ s.add_dependency(%q<fakeweb>, [">= 0"])
52
+ end
53
+ end
File without changes
@@ -0,0 +1,93 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Rmb::Asset do
4
+ before(:each) do
5
+ @drop = Rmb::Drop.new
6
+ @drop.name = "test_drop"
7
+ @asset = Rmb::Asset.new
8
+ @asset.name = "test_asset"
9
+ @asset.drop = @drop
10
+
11
+ @client = Rmb::Client.new
12
+ @api = stub(Rmb::Api)
13
+ @client.service = @api
14
+
15
+ Rmb::Resource.stub!(:client).and_return(@client)
16
+ Rmb::Resource.client.should == @client
17
+ Rmb::Resource.client.service.should == @api
18
+ end
19
+
20
+ it "should have the attributes of an Asset" do
21
+ @asset.should respond_to(:drop, :name, :type, :title, :description, :filesize, :created_at,
22
+ :status, :pages, :duration, :artist,
23
+ :track_title, :height, :width, :contents, :url)
24
+ end
25
+
26
+ it "should save itself" do
27
+ @client.should_receive(:handle).with(:asset,{}).and_return(@asset)
28
+ expected_hash = {:url=> "http://rmb.io", :contents=>nil, :description=>nil, :title=>nil}
29
+ @asset.url = expected_hash[:url]
30
+ @api.stub!(:update_asset).and_return({})
31
+ @asset.save
32
+ end
33
+
34
+ it "should destroy itself" do
35
+ @client.should_receive(:handle).with(:response,{}).and_return({"result" => "Success"})
36
+ @api.stub!(:delete_asset).with(@drop.name, @asset.id).and_return({})
37
+ @asset.destroy!
38
+ end
39
+
40
+ it "should destroy roles" do
41
+ @client.should_receive(:handle).with(:response,{}).and_return({"result" => "Success"})
42
+ @api.stub!(:delete_role).with(@drop.name, @asset.id, "thumbnail", nil).and_return({})
43
+ @asset.destroy_role!("thumbnail")
44
+ end
45
+
46
+ it "should destroy roles at locations" do
47
+ @client.should_receive(:handle).with(:response,{}).and_return({"result" => "Success"})
48
+ @api.stub!(:delete_role).with(@drop.name, @asset.id, "thumbnail", "RmbS3").and_return({})
49
+ @asset.destroy_location!("thumbnail","RmbS3")
50
+ end
51
+
52
+ it "should send itself to another drop." do
53
+ @target_drop = Drop.new
54
+ @target_drop.name = "target_drop"
55
+ @client.should_receive(:handle).with(:response,{}).and_return({"result" => "Success"})
56
+ @api.stub!(:copy_asset).and_return({})
57
+ @asset.send_to_drop(@target_drop)
58
+ end
59
+
60
+ it "should copy itself to another drop." do
61
+ @target_drop = Drop.new
62
+ @target_drop.name = "target_drop"
63
+ @client.should_receive(:handle).with(:response,{}).and_return({"result" => "Success"})
64
+ @api.stub!(:copy_asset).and_return({})
65
+ @asset.copy_to(@target_drop)
66
+ end
67
+
68
+ it "should move itself to another drop." do
69
+ @target_drop = Drop.new
70
+ @target_drop.name = "target_drop"
71
+ @client.should_receive(:handle).with(:response,{}).and_return({"result" => "Success"})
72
+ @api.stub!(:move_asset).and_return({})
73
+ @asset.move_to(@target_drop)
74
+ end
75
+
76
+ it "should find itself" do
77
+ @client.should_receive(:handle).with(:asset,{}).and_return(@asset)
78
+ @api.should_receive(:asset).with(@drop.name, @asset.id).and_return({})
79
+ Asset.find(@drop, @asset.id).should == @asset
80
+ end
81
+
82
+ it "should generate a signed url" do
83
+ @api.should_receive(:generate_asset_url)
84
+ @asset.generate_url
85
+ end
86
+
87
+ it "should have an original file url" do
88
+ @api.should_receive(:generate_original_file_url)
89
+ @asset.original_file_url
90
+ end
91
+
92
+
93
+ end
File without changes