Class: Rmb::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/rmb/api.rb

Constant Summary collapse

RUBY_VERSION =
%w{MAJOR MINOR TEENY}.map { |k| Config::CONFIG[k] }.join(".")
USER_AGENT_STRING =
"RmbAPI-Ruby/#{Rmb::VERSION} (Ruby #{RUBY_VERSION} #{Config::CONFIG["host"]}; +http://github.com/dropio/rmb/tree/)"

Instance Method Summary collapse

Constructor Details

#initializeApi

Returns a new instance of Api.



15
16
17
18
19
# File 'lib/rmb/api.rb', line 15

def initialize
  self.class.debug_output $stderr if Rmb::Config.debug
  self.class.base_uri Rmb::Config.api_url
  self.class.default_options[:timeout] = Rmb::Config.timeout
end

Instance Method Details

#add_file(drop_name, file_path, description = nil, conversion = nil, pingback_url = nil, output_locations = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rmb/api.rb', line 71

def add_file(drop_name, file_path, description = nil, conversion = nil, pingback_url = nil, output_locations = nil)
  url  = URI.parse(Rmb::Config.upload_url)
  locs = output_locations.is_a?(Array) ? output_locations.join(',') : output_locations
  r    = nil

  File.open(file_path) do |file|
    mime_type = (MIME::Types.type_for(file_path)[0] || MIME::Types["application/octet-stream"][0])

    params = {
      :api_key => Rmb::Config.api_key.to_s,
      :format => 'json',
      :version           => Rmb::Config.version
    }

    # stuff passed in by a user. Done like this as if you pass a parameter without a value it can cause an issue (with the output_locations anyway)
    # although this will be fixed in the API, we shouldn't be doing it anyway.
    params[:drop_name] = drop_name if drop_name
    params[:description] = description if description
    params[:pingback_url] = pingback_url if pingback_url
    params[:output_locations] = locs if locs
    params[:conversion] = conversion if conversion
    params[:signature_mode] = "STRICT"
    #sign the params at this point
    params = sign_if_needed(params)
    params[:file]  = UploadIO.new(file, mime_type, file_path)

    req  = Net::HTTP::Post::Multipart.new(url.path, params)
    http = Net::HTTP.new(url.host, url.port)
    http.set_debug_output $stderr if Rmb::Config.debug
    r = http.start{|http| http.request(req)}
  end

  (r.nil? or r.body.nil? or r.body.empty?) ? r : HTTParty::Response.new(r,Crack::JSON.parse(r.body))
end

#add_file_from_url(drop_name, url, description = nil, convert_to = nil, pingback_url = nil) ⇒ Object



106
107
108
# File 'lib/rmb/api.rb', line 106

def add_file_from_url(drop_name, url, description = nil, convert_to = nil, pingback_url = nil)
  rmb_post("/drops/#{drop_name}/assets", {:file_url => url, :description => description, :convert_to => convert_to, :pingback_url => pingback_url})
end

#all_drops(page = 1) ⇒ Object



25
26
27
# File 'lib/rmb/api.rb', line 25

def all_drops(page = 1)
  rmb_get("/accounts/drops", {:page => page})
end

#asset(drop_name, asset_name) ⇒ Object



114
115
116
# File 'lib/rmb/api.rb', line 114

def asset(drop_name, asset_name)
  rmb_get("/drops/#{drop_name}/assets/#{asset_name}", {})
end

#assets(drop_name, page = 1, order = :oldest) ⇒ Object



110
111
112
# File 'lib/rmb/api.rb', line 110

def assets(drop_name, page = 1, order = :oldest)
  rmb_get("/drops/#{drop_name}/assets", {:page => page, :order => order.to_s, :show_pagination_details => true})
end

#change_asset_name(drop_name, asset_name, new_name) ⇒ Object



140
141
142
143
# File 'lib/rmb/api.rb', line 140

def change_asset_name(drop_name, asset_name, new_name)
  params = {:name => new_name}
  rmb_put("/drops/#{drop_name}/assets/#{asset_name}", params)
end

#change_drop_name(drop_name, new_name) ⇒ Object



41
42
43
44
# File 'lib/rmb/api.rb', line 41

def change_drop_name(drop_name, new_name)
  params = {:name => new_name}
  rmb_put("/drops/#{drop_name}", params)
end

#copy_asset(drop_name, asset_name, target_drop) ⇒ Object



157
158
159
160
# File 'lib/rmb/api.rb', line 157

def copy_asset(drop_name, asset_name, target_drop)
  params = {:drop_name => target_drop}
  rmb_post("/drops/#{drop_name}/assets/#{asset_name}/copy", params)
end

#create_drop(params = {}) ⇒ Object



33
34
35
# File 'lib/rmb/api.rb', line 33

def create_drop(params = {})
  rmb_post("/drops",params)
end

#create_job(job = {}) ⇒ Object Also known as: convert



189
190
191
# File 'lib/rmb/api.rb', line 189

def create_job(job = {})
  rmb_post("/jobs",job)
end


62
63
64
# File 'lib/rmb/api.rb', line 62

def create_link(drop_name, url, title = nil, description = nil)
  rmb_post("/drops/#{drop_name}/assets", {:url => url, :title => title, :description => description})
end

#create_note(drop_name, contents, title = nil, description = nil) ⇒ Object



66
67
68
69
# File 'lib/rmb/api.rb', line 66

def create_note(drop_name, contents, title = nil, description = nil)
  params = {:contents => contents, :title => title, :description => description}
  rmb_post("/drops/#{drop_name}/assets", params)
end

#create_pingback_subscription(drop_name, url, events = {}) ⇒ Object



167
168
169
# File 'lib/rmb/api.rb', line 167

def create_pingback_subscription(drop_name, url, events = {})
  rmb_post("/drops/#{drop_name}/subscriptions", :body => {:type => "pingback", :url => url}.merge(events))
end

#delete_asset(drop_name, asset_id) ⇒ Object



145
146
147
# File 'lib/rmb/api.rb', line 145

def delete_asset(drop_name, asset_id)
  rmb_delete("/drops/#{drop_name}/assets/#{asset_id}", {})
end

#delete_drop(drop_name) ⇒ Object



50
51
52
# File 'lib/rmb/api.rb', line 50

def delete_drop(drop_name)
  rmb_delete("/drops/#{drop_name}", {})
end

#delete_role(drop_name, asset_id, role, location = nil) ⇒ Object



149
150
151
# File 'lib/rmb/api.rb', line 149

def delete_role(drop_name, asset_id, role, location=nil)
  rmb_delete("/drops/#{drop_name}/assets/#{asset_id}", {:role => role, :output_location => location})
end

#delete_subscription(drop_name, subscription_id) ⇒ Object



175
176
177
# File 'lib/rmb/api.rb', line 175

def delete_subscription(drop_name, subscription_id)
  rmb_delete("/drops/#{drop_name}/subscriptions/#{subscription_id}", {})
end

#drop(drop_name) ⇒ Object



21
22
23
# File 'lib/rmb/api.rb', line 21

def drop(drop_name)
  rmb_get("/drops/#{drop_name}", {})
end

#drop_upload_code(drop_name) ⇒ Object



58
59
60
# File 'lib/rmb/api.rb', line 58

def drop_upload_code(drop_name)
  rmb_get("/drops/#{drop_name}/upload_code", {})
end

#empty_drop(drop_name) ⇒ Object



46
47
48
# File 'lib/rmb/api.rb', line 46

def empty_drop(drop_name)
  rmb_put("/drops/#{drop_name}/empty", {})
end

#generate_asset_url(drop_name, asset_name) ⇒ Object



118
119
120
# File 'lib/rmb/api.rb', line 118

def generate_asset_url(drop_name, asset_name)
  signed_url(drop_name, asset_name)
end

#generate_drop_url(drop_name) ⇒ Object



29
30
31
# File 'lib/rmb/api.rb', line 29

def generate_drop_url(drop_name)
  signed_url(drop_name)
end

#generate_original_file_url(drop_name, asset_id, time_to_live = 600) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rmb/api.rb', line 122

def generate_original_file_url(drop_name, asset_id, time_to_live = 600)
  #TODO - signed download URLs
  #this is now available via the API response itself
  download_url = Rmb::Config.api_url + "/drops/#{drop_name}/assets/#{asset_id}/download/original?"
  params = {:version => Rmb::Config.version, :api_key=>Rmb::Config.api_key.to_s, :format=>'json'}
  params = sign_if_needed(params)
  paramstring = ''
  params.each do |k, v|
    paramstring << "#{k}=#{v}&"
  end
  paramstring.chop!
  download_url += paramstring
end

#get_signature(params = {}) ⇒ Object



179
180
181
182
183
# File 'lib/rmb/api.rb', line 179

def get_signature(params={})
  #returns a signature for the passed params, without any modifcation to the params
  params = sign_request(params)
  params[:signature]
end

#job(id, drop_name, asset_name_or_id) ⇒ Object



185
186
187
# File 'lib/rmb/api.rb', line 185

def job(id, drop_name, asset_name_or_id)
  rmb_get("/drops/#{drop_name}/assets/#{asset_name_or_id}/jobs/#{id}", {})
end

#move_asset(drop_name, asset_name, target_drop) ⇒ Object



162
163
164
165
# File 'lib/rmb/api.rb', line 162

def move_asset(drop_name, asset_name, target_drop)
  params = {:drop_name => target_drop}
  rmb_post("/drops/#{drop_name}/assets/#{asset_name}/move", params)
end

#promote_nick(drop_name, nick) ⇒ Object



54
55
56
# File 'lib/rmb/api.rb', line 54

def promote_nick(drop_name, nick)
  rmb_post("/drops/#{drop_name}", {:nick => nick})
end

#send_asset_to_drop(drop_name, asset_name, target_drop) ⇒ Object



153
154
155
# File 'lib/rmb/api.rb', line 153

def send_asset_to_drop(drop_name, asset_name, target_drop)
  rmb_post("/drops/#{drop_name}/assets/#{asset_name}/send_to", {:medium => "drop", :drop_name => target_drop})
end

#subscriptions(drop_name, page) ⇒ Object



171
172
173
# File 'lib/rmb/api.rb', line 171

def subscriptions(drop_name, page)
  rmb_get("/drops/#{drop_name}/subscriptions", :query => {:page => page, :show_pagination_details => true})
end

#update_asset(drop_name, asset_name, params = {}) ⇒ Object



136
137
138
# File 'lib/rmb/api.rb', line 136

def update_asset(drop_name, asset_name, params = {})
  rmb_put("/drops/#{drop_name}/assets/#{asset_name}", params)
end

#update_drop(drop_name, params = {}) ⇒ Object



37
38
39
# File 'lib/rmb/api.rb', line 37

def update_drop(drop_name, params = {})
  rmb_put("/drops/#{drop_name}", params)
end