Skip to content

Commit

Permalink
merge replace api with upload api
Browse files Browse the repository at this point in the history
  • Loading branch information
hanklords committed Nov 11, 2009
1 parent 8cb5128 commit eb69606
Showing 1 changed file with 26 additions and 59 deletions.
85 changes: 26 additions & 59 deletions lib/flickraw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

This comment has been minimized.

Copy link
@hsribei

hsribei Nov 11, 2009

Contributor

You're missing a ';' after upload_flickr()


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

This comment has been minimized.

Copy link
@hsribei

hsribei Nov 11, 2009

Contributor

Missing ';' after upload_flickr() here as well.


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)

Expand All @@ -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 = {
Expand All @@ -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
Expand Down

2 comments on commit eb69606

@hsribei
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, my bad. Didn't know you didn't need those :)

@hsribei
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the new code here and it works just fine!

Please sign in to comment.