Created
May 16, 2011 08:18
-
-
Save mttkay/974084 to your computer and use it in GitHub Desktop.
Ruby OAuth file uploads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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