Skip to content

Instantly share code, notes, and snippets.

@mttkay
Created May 16, 2011 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mttkay/974084 to your computer and use it in GitHub Desktop.
Save mttkay/974084 to your computer and use it in GitHub Desktop.
Ruby OAuth file uploads
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment