public
Last active

Ruby OAuth file uploads

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# patch OAuth class to be able to create multipart requests (required for image uploading)
# NOTE that for my purpose I simply made it "auto-sensing" that an image is passed and
# only then creates a multipart body. You may simply want to use some flag instead or whatever.
module OAuth
class Consumer
alias_method :create_default_http_request, :create_http_request
protected
def create_http_request(http_method, path, *arguments)
# CHANGE THIS -- I only did this because it was for a quick-and-dirty script that had to upload pictures
if http_method == :post && path.include?('/assets')
create_multipart_http_request(http_method, path, *arguments)
else
create_default_http_request(http_method, path, *arguments)
end
end
def create_multipart_http_request(http_method, path, *arguments)
image_path = arguments.shift
Net::HTTP::Post::Multipart.new(path, "uploaded_data" => UploadIO.new(File.open(image_path), "image/jpeg", "print_ad.jpg"))
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.