flickraw 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/examples/upload.rb +22 -0
  2. data/lib/flickraw.rb +50 -14
  3. metadata +3 -2
@@ -0,0 +1,22 @@
1
+ require 'flickraw'
2
+
3
+ # This is how to upload photos on flickr.
4
+ # You need to be authentified to do that.
5
+ API_KEY=''
6
+ SHARED_SECRET=''
7
+ PHOTO_PATH='photo.jpg'
8
+
9
+ FlickRaw.api_key=API_KEY
10
+ FlickRaw.shared_secret=SHARED_SECRET
11
+
12
+ frob = flickr.auth.getFrob
13
+ auth_url = FlickRaw.auth_url :frob => frob, :perms => 'write'
14
+
15
+ puts "Open this url in your process to complete the authication process : #{auth_url}"
16
+ puts "Press Enter when you are finished."
17
+ STDIN.getc
18
+
19
+ flickr.auth.getToken :frob => frob
20
+ login = flickr.test.login
21
+
22
+ flickr.upload_photo PHOTO_PATH, :title => 'Title', :description => 'This is the description'
@@ -25,18 +25,22 @@ require 'open-uri'
25
25
  require 'md5'
26
26
 
27
27
  module FlickRaw
28
- VERSION='0.2'
28
+ VERSION='0.3'
29
29
 
30
- # URL of the flickr REST api
31
- REST_URL='http://www.flickr.com/services/rest/?'.freeze
30
+ FLICKR_HOST='api.flickr.com'.freeze
32
31
 
33
- # URL of the flickr auth page
34
- AUTH_URL='http://flickr.com/services/auth/?'
32
+ # Path of the flickr REST api
33
+ REST_PATH='/services/rest/?'.freeze
34
+
35
+ # Path of the flickr auth page
36
+ AUTH_PATH='/services/auth/?'.freeze
37
+
38
+ UPLOAD_PATH='/services/upload/'.freeze
35
39
 
36
40
  @api_key = '7b124df89b638e545e3165293883ef62'
37
41
 
38
42
  # This is a wrapper around the xml response which provides an easy interface.
39
- class Xml
43
+ class Xml
40
44
  # Returns the text content of the response
41
45
  attr_reader :to_s
42
46
 
@@ -163,21 +167,53 @@ module FlickRaw
163
167
  #
164
168
  # Raises FailedResponse if the response status is _failed_.
165
169
  def call(req, args={})
166
- url = build_call_url(req, args)
170
+ url = 'http://' + FLICKR_HOST + REST_PATH + build_args(args, req).collect { |a, v| "#{a}=#{v}" }.join('&')
171
+
167
172
  res = Response.new open(url, 'User-Agent' => "Flickraw/#{VERSION}")
168
173
  raise FailedResponse.new( res.msg, res.code) if res.stat == 'fail'
169
- lookup_token( req, res)
174
+ lookup_token(req, res)
175
+ res
176
+ end
177
+
178
+ # Use this to upload the photo in _file_.
179
+ #
180
+ # flickr.upload_photo '/path/to/the/photo', :title => 'Title', :description => 'This is the description'
181
+ #
182
+ # See http://www.flickr.com/services/api/upload.api.html for more information on the arguments.
183
+ def upload_photo(file, args={})
184
+ photo = File.read file
185
+ boundary = MD5.md5(photo).to_s
186
+
187
+ header = {'Content-type' => "multipart/form-data, boundary=#{boundary} ", 'User-Agent' => "Flickraw/#{VERSION}"}
188
+ query = build_args(args).collect { |a, v|
189
+ "--#{boundary}\r\n" <<
190
+ "Content-Disposition: form-data; name=\"#{a}\"\r\n\r\n" <<
191
+ "#{v}\r\n"
192
+ }.join('')
193
+ query << "--#{boundary}\r\n" <<
194
+ "Content-Disposition: form-data; name=\"photo\"; filename=\"#{file}\"\r\n" <<
195
+ "Content-Transfer-Encoding: binary\r\n" <<
196
+ "Content-Type: image/jpeg\r\n\r\n" <<
197
+ photo <<
198
+ "\r\n" <<
199
+ "--#{boundary}--"
200
+
201
+ http_response = Net::HTTP.start(FLICKR_HOST) { |http|
202
+ http.post(UPLOAD_PATH, query, header)
203
+ }
204
+ res = Response.new http_response.body
205
+ raise FailedResponse.new(res.msg, res.code) if res.stat == 'fail'
170
206
  res
171
207
  end
172
208
 
173
209
  private
174
- def build_call_url(req, args={})
175
- full_args = {:api_key => FlickRaw.api_key, :method => req}
210
+ def build_args(args={}, req = nil)
211
+ full_args = {:api_key => FlickRaw.api_key}
212
+ full_args[:method] = req if req
176
213
  full_args[:auth_token] = @token if @token
177
214
  args.each {|k, v| full_args[k.to_sym] = v }
178
- full_args[:api_sig] = FlickRaw.api_sig(full_args) if FlickRaw.shared_secret
179
-
180
- url = REST_URL + full_args.collect { |a, v| "#{a}=#{v}" }.join('&')
215
+ full_args[:api_sig] = FlickRaw.api_sig(full_args) if FlickRaw.shared_secret
216
+ full_args
181
217
  end
182
218
 
183
219
  def lookup_token(req, res)
@@ -200,7 +236,7 @@ module FlickRaw
200
236
 
201
237
  full_args[:api_sig] = api_sig(full_args) if FlickRaw.shared_secret
202
238
 
203
- url = AUTH_URL + full_args.collect { |a, v| "#{a}=#{v}" }.join('&')
239
+ 'http://' + FLICKR_HOST + AUTH_PATH + full_args.collect { |a, v| "#{a}=#{v}" }.join('&')
204
240
  end
205
241
 
206
242
  # Returns the signature of hsh. This is meant to be passed in the _api_sig_ parameter.
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: flickraw
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.2"
7
- date: 2006-07-14 00:00:00 +02:00
6
+ version: "0.3"
7
+ date: 2006-09-02 00:00:00 +02:00
8
8
  summary: Flickr library with a syntax close to the syntax described on http://www.flickr.com/services/api
9
9
  require_paths:
10
10
  - lib
@@ -35,6 +35,7 @@ files:
35
35
  - examples/auth.rb
36
36
  - examples/interestingness.rb
37
37
  - examples/flickr_KDE.rb
38
+ - examples/upload.rb
38
39
  test_files: []
39
40
 
40
41
  rdoc_options: []