From eb69606dc8793567f23ccd50dd4aad63975dcf3b Mon Sep 17 00:00:00 2001 From: Mael Clerambault Date: Wed, 11 Nov 2009 20:11:33 +0100 Subject: [PATCH] merge replace api with upload api --- lib/flickraw.rb | 85 +++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 59 deletions(-) diff --git a/lib/flickraw.rb b/lib/flickraw.rb index b5a16fc..48d270c 100644 --- a/lib/flickraw.rb +++ b/lib/flickraw.rb @@ -174,49 +174,34 @@ def call(req, args={}) # flickr.upload_photo '/path/to/the/photo', :title => 'Title', :description => 'This is the description' # # See http://www.flickr.com/services/api/upload.api.html for more information on the arguments. - def upload_photo(file, args={}) - photo = File.open(file, 'rb') { |f| f.read } - boundary = Digest::MD5.hexdigest(photo) - - header = {'Content-type' => "multipart/form-data, boundary=#{boundary} ", 'User-Agent' => "Flickraw/#{VERSION}"} - query = '' - build_args(args).each { |a, v| - query << - "--#{boundary}\r\n" << - "Content-Disposition: form-data; name=\"#{a}\"\r\n\r\n" << - "#{v}\r\n" - } - query << - "--#{boundary}\r\n" << - "Content-Disposition: form-data; name=\"photo\"; filename=\"#{file}\"\r\n" << - "Content-Transfer-Encoding: binary\r\n" << - "Content-Type: image/jpeg\r\n\r\n" << - photo << - "\r\n" << - "--#{boundary}--" + def upload_photo(file, args={}); upload_flickr(UPLOAD_PATH, file, args={}) end - http_response = open_flickr {|http| http.post(UPLOAD_PATH, query, header) } - xml = http_response.body - if xml[/stat="(\w+)"/, 1] == 'fail' - msg = xml[/msg="([^"]+)"/, 1] - code = xml[/code="([^"]+)"/, 1] - raise FailedResponse.new(msg, code, 'flickr.upload') - end - type = xml[/<(\w+)/, 1] - h = { - :secret => xml[/secret="([^"]+)"/, 1], - :originalsecret => xml[/originalsecret="([^"]+)"/, 1], - :_content => xml[/>([^<]+)<\//, 1] - }.delete_if {|k,v| v.nil? } - Response.build(h, type) - end - - # Use this to replace the photo with :photo_id with the photo in _file_. Async not yet supported. + # Use this to replace the photo with :photo_id with the photo in _file_. # # flickr.replace_photo '/path/to/the/photo', :photo_id => id # - # See http://www.flickr.com/services/api/upload.api.html for more information on the arguments. - def replace_photo(file, args={}) + # See http://www.flickr.com/services/api/replace.api.html for more information on the arguments. + def replace_photo(file, args={}); upload_flickr(REPLACE_PATH, file, args={}) end + + private + def build_args(args={}, req = nil) + full_args = {:api_key => FlickRaw.api_key, :format => 'json', :nojsoncallback => 1} + full_args[:method] = req if req + full_args[:auth_token] = @token if @token + args.each {|k, v| full_args[k.to_sym] = v.to_s } + full_args[:api_sig] = FlickRaw.api_sig(full_args) if FlickRaw.shared_secret + args.each {|k, v| full_args[k.to_sym] = CGI.escape(v.to_s) } if req + full_args + end + + def open_flickr + Net::HTTP::Proxy(FlickRawOptions['proxy_host'], FlickRawOptions['proxy_port'], FlickRawOptions['proxy_user'], FlickRawOptions['proxy_password']).start(FLICKR_HOST) {|http| + http.read_timeout = FlickRawOptions['timeout'] if FlickRawOptions['timeout'] + yield http + } + end + + def upload_flickr(method, file, args={}) photo = File.open(file, 'rb') { |f| f.read } boundary = Digest::MD5.hexdigest(photo) @@ -237,12 +222,12 @@ def replace_photo(file, args={}) "\r\n" << "--#{boundary}--" - http_response = open_flickr {|http| http.post(REPLACE_PATH, query, header) } + http_response = open_flickr {|http| http.post(method, query, header) } xml = http_response.body if xml[/stat="(\w+)"/, 1] == 'fail' msg = xml[/msg="([^"]+)"/, 1] code = xml[/code="([^"]+)"/, 1] - raise FailedResponse.new(msg, code, 'flickr.replace') + raise FailedResponse.new(msg, code, 'flickr.upload') end type = xml[/<(\w+)/, 1] h = { @@ -252,24 +237,6 @@ def replace_photo(file, args={}) }.delete_if {|k,v| v.nil? } Response.build(h, type) end - - private - def build_args(args={}, req = nil) - full_args = {:api_key => FlickRaw.api_key, :format => 'json', :nojsoncallback => 1} - full_args[:method] = req if req - full_args[:auth_token] = @token if @token - args.each {|k, v| full_args[k.to_sym] = v.to_s } - full_args[:api_sig] = FlickRaw.api_sig(full_args) if FlickRaw.shared_secret - args.each {|k, v| full_args[k.to_sym] = CGI.escape(v.to_s) } if req - full_args - end - - def open_flickr - Net::HTTP::Proxy(FlickRawOptions['proxy_host'], FlickRawOptions['proxy_port'], FlickRawOptions['proxy_user'], FlickRawOptions['proxy_password']).start(FLICKR_HOST) {|http| - http.read_timeout = FlickRawOptions['timeout'] if FlickRawOptions['timeout'] - yield http - } - end end class << self