Class: Dropio::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/dropio/client.rb

Defined Under Namespace

Modules: MultipartPost Classes: Mapper

Constant Summary collapse

DEFAULT_HEADER =

The default set of headers for each request.

{
    'User-Agent' => user_agent_string,
    'Accept' => 'application/json'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.user_agent_stringObject

Generates a user agent string.



7
8
9
10
# File 'lib/dropio/client.rb', line 7

def self.user_agent_string
  ruby_version = %w{MAJOR MINOR TEENY}.map { |k| Config::CONFIG[k] }.join(".")
  "DropioAPI-Ruby/#{Dropio::VERSION} (Ruby #{ruby_version} #{Config::CONFIG["host"]}; +http://dropio.rubyforge.org/)"
end

Instance Method Details

#add_file(drop, file_path) ⇒ Object

Adds a file to a Drop



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dropio/client.rb', line 91

def add_file(drop, file_path)
  token = get_default_token(drop)
  asset = nil
  
  File.open(file_path, 'r') do |file|
    uri = URI.parse(Dropio.upload_url)
    req = Net::HTTP::Post.new(uri.path, DEFAULT_HEADER)
    form = create_form( { :drop_name => drop.name, :token => token , :file => file } )
    req.multipart_params = form  
    complete_request(req, uri.host) { |body| asset = Mapper.map_assets(drop, body) }
  end
  
  asset
end

#create_comment(asset, contents) ⇒ Object

Creates a Comment



153
154
155
156
157
158
159
160
161
162
# File 'lib/dropio/client.rb', line 153

def create_comment(asset, contents)
  token = get_default_token(asset.drop)
  uri = URI::HTTP.build({:path => comment_path(asset.drop, asset)})
  form = create_form( { :token => token, :contents => contents })
  req = Net::HTTP::Post.new(uri.request_uri, DEFAULT_HEADER)
  req.set_form_data(form)
  comment = nil
  complete_request(req) { |body| comment = Mapper.map_comments(asset, body) }
  comment
end

#create_drop(attributes = {}) ⇒ Object

Creates a drop with an attributes hash. Valid attributes: name (string), guests_can_comment (boolean), guests_can_add (boolean), guests_can_delete (boolean), expiration_length (string), password (string), admin_password (string), and premium_code (string) Descriptions can be found here: groups.google.com/group/dropio-api/web/full-api-documentation



50
51
52
53
54
55
56
57
58
# File 'lib/dropio/client.rb', line 50

def create_drop(attributes = {})
  uri = URI::HTTP.build({:path => drop_path})
  form = create_form(attributes)
  req = Net::HTTP::Post.new(uri.request_uri, DEFAULT_HEADER)
  req.set_form_data(form)
  drop = nil
  complete_request(req) { |body| drop = Mapper.map_drops(body) }
  drop
end

Creates a link Asset



119
120
121
122
123
124
125
126
127
128
# File 'lib/dropio/client.rb', line 119

def create_link(drop, url, title = nil, description = nil)
  token = get_default_token(drop)
  uri = URI::HTTP.build({:path => asset_path(drop)})
  form = create_form( { :token => token, :url => url, :title => title, :description => description })
  req = Net::HTTP::Post.new(uri.request_uri, DEFAULT_HEADER)
  req.set_form_data(form)
  asset = nil
  complete_request(req) { |body| asset = Mapper.map_assets(drop, body) }
  asset
end

#create_note(drop, title, contents) ⇒ Object

Creates a note Asset



107
108
109
110
111
112
113
114
115
116
# File 'lib/dropio/client.rb', line 107

def create_note(drop, title, contents)
  token = get_default_token(drop)
  uri = URI::HTTP.build({:path => asset_path(drop)})
  form = create_form( { :token => token, :title => title, :contents => contents })
  req = Net::HTTP::Post.new(uri.request_uri, DEFAULT_HEADER)
  req.set_form_data(form)
  asset = nil
  complete_request(req) { |body| asset = Mapper.map_assets(drop, body) }
  asset
end

#destroy_asset(asset) ⇒ Object

Destroys an Asset



198
199
200
201
202
203
204
205
206
# File 'lib/dropio/client.rb', line 198

def destroy_asset(asset)
  token = get_default_token(asset.drop)
  uri = URI::HTTP.build({:path => asset_path(asset.drop, asset)})
  form = create_form( { :token => token })
  req = Net::HTTP::Delete.new(uri.request_uri, DEFAULT_HEADER)
  req.set_form_data(form)
  complete_request(req)
  true
end

#destroy_comment(comment) ⇒ Object

Destroys a Comment, requires admin token.



142
143
144
145
146
147
148
149
150
# File 'lib/dropio/client.rb', line 142

def destroy_comment(comment)
  token = get_admin_token(comment.asset.drop)
  uri = URI::HTTP.build({:path => comment_path(comment.asset.drop, comment.asset, comment)})
  form = create_form( { :token => token })
  req = Net::HTTP::Delete.new(uri.request_uri, DEFAULT_HEADER)
  req.set_form_data(form)
  complete_request(req)
  true
end

#destroy_drop(drop) ⇒ Object

Destroys a Drop



80
81
82
83
84
85
86
87
88
# File 'lib/dropio/client.rb', line 80

def destroy_drop(drop)
  token = get_admin_token(drop)
  uri = URI::HTTP.build({:path => drop_path(drop)})
  form = create_form( { :token => token })
  req = Net::HTTP::Delete.new(uri.request_uri, DEFAULT_HEADER)
  req.set_form_data(form)
  complete_request(req)
  true
end

#find_assets(drop, page = 1) ⇒ Object

Finds a collection of Asset objects for a given Drop.



28
29
30
31
32
33
34
35
# File 'lib/dropio/client.rb', line 28

def find_assets(drop, page = 1)
  token = get_default_token(drop)
  uri = URI::HTTP.build({:path => asset_path(drop), :query => get_request_tokens(token) + "&page=#{page}"})
  req = Net::HTTP::Get.new(uri.request_uri, DEFAULT_HEADER)
  assets = nil
  complete_request(req) { |body| assets = Mapper.map_assets(drop, body) }
  assets
end

#find_comments(asset) ⇒ Object

Finds a collection of Comment objects for a given Asset.



38
39
40
41
42
43
44
45
# File 'lib/dropio/client.rb', line 38

def find_comments(asset)
  token = get_default_token(asset.drop)
  uri = URI::HTTP.build({:path => comment_path(asset.drop, asset), :query => get_request_tokens(token)})
  req = Net::HTTP::Get.new(uri.request_uri, DEFAULT_HEADER)
  comments = nil
  complete_request(req) { |body| comments = Mapper.map_comments(asset, body) }
  comments
end

#find_drop(drop_name, token = nil) ⇒ Object

Takes a drop name and optional token and returns a Drop or errors.



19
20
21
22
23
24
25
# File 'lib/dropio/client.rb', line 19

def find_drop(drop_name, token = nil)
  uri = URI::HTTP.build({:path => drop_path(drop_name), :query => get_request_tokens(token)})
  req = Net::HTTP::Get.new(uri.request_uri, DEFAULT_HEADER)
  drop = nil
  complete_request(req) { |body| drop = Mapper.map_drops(body) }
  drop
end

#generate_asset_url(asset) ⇒ Object

Generates the authenticated Asset url.



214
215
216
# File 'lib/dropio/client.rb', line 214

def generate_asset_url(asset)
  signed_url(asset.drop, asset)
end

#generate_drop_url(drop) ⇒ Object

Generates the authenticated Drop url.



209
210
211
# File 'lib/dropio/client.rb', line 209

def generate_drop_url(drop)
  signed_url(drop)
end

#save_asset(asset) ⇒ Object

Saves an Asset back to drop.io



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/dropio/client.rb', line 183

def save_asset(asset)
  token = get_default_token(asset.drop)
  uri = URI::HTTP.build({:path => asset_path(asset.drop, asset)})
  form = create_form({ :token => token, 
                       :title => asset.title,
                       :url => asset.url,
                       :description => asset.description,
                       :contents => asset.contents })
  req = Net::HTTP::Put.new(uri.request_uri, DEFAULT_HEADER)
  req.set_form_data(form)
  complete_request(req) { |body| asset = Mapper.map_assets(asset.drop, body)}
  asset
end

#save_comment(comment) ⇒ Object

Saves a Comment, requires admin token.



131
132
133
134
135
136
137
138
139
# File 'lib/dropio/client.rb', line 131

def save_comment(comment)
  token = get_default_token(comment.asset.drop)
  uri = URI::HTTP.build({:path => comment_path(comment.asset.drop, comment.asset, comment)})
  form = create_form( { :token => token, :contents => comment.contents })
  req = Net::HTTP::Put.new(uri.request_uri, DEFAULT_HEADER)
  req.set_form_data(form)
  complete_request(req) { |body| comment = Mapper.map_comments(comment.asset, body) }
  comment
end

#save_drop(drop) ⇒ Object

Saves a Drop back to drop.io



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dropio/client.rb', line 61

def save_drop(drop)
  uri = URI::HTTP.build({:path => drop_path(drop) })
  token = get_admin_token(drop)
  form = create_form :token => token,
                     :expiration_length => drop.expiration_length,
                     :guests_can_comment => drop.guests_can_comment,
                     :premium_code => drop.premium_code,
                     :guests_can_add => drop.guests_can_add,
                     :guests_can_delete => drop.guests_can_delete,
                     :password => drop.password,
                     :admin_password => drop.admin_password
  req = Net::HTTP::Put.new(uri.request_uri, DEFAULT_HEADER)
  req.set_form_data(form)
  drop = nil
  complete_request(req) { |body| drop = Mapper.map_drops(body) }
  drop
end

#send_to_drop(asset, drop_name) ⇒ Object

Sends an Asset to a given Drop with drop_name



177
178
179
180
# File 'lib/dropio/client.rb', line 177

def send_to_drop(asset, drop_name)
  params = { :medium => "drop", :drop_name => drop_name }
  send_asset(asset,params)
end

#send_to_emails(asset, emails = [], message = nil) ⇒ Object

Sends an email message, containing the asset to a list of emails



171
172
173
174
# File 'lib/dropio/client.rb', line 171

def send_to_emails(asset, emails = [], message = nil)
  params = { :medium => "email", :emails => emails.join(","), :message => message }
  send_asset(asset,params)
end

#send_to_fax(asset, fax_number) ⇒ Object

Sends an Asset (of type Document) to a fax_number



165
166
167
168
# File 'lib/dropio/client.rb', line 165

def send_to_fax(asset, fax_number)
  params = { :medium => "fax", :fax_number => fax_number }
  send_asset(asset,params)
end