Created
March 11, 2014 04:20
-
-
Save bensie/9479467 to your computer and use it in GitHub Desktop.
Accept multipart upload with Sinatra
This file contains hidden or 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
post "/shots" do | |
if params[:photo] && params[:photo].is_a?(Hash) | |
class UploadedFile < OpenStruct; end | |
paperclip = UploadedFile.new( | |
tempfile: params[:photo][:tempfile], | |
path: params[:photo][:tempfile].path, | |
original_filename: params[:photo][:filename], | |
content_type: params[:photo][:type], | |
head: params[:photo][:head] | |
) | |
shot = @event.shots.new.tap do |s| | |
s.photo = paperclip | |
end | |
shot.save! | |
status 201 | |
headers "Location" => shot.api_url | |
json shot.api_authenticated_hash | |
else | |
halt_with_400_bad_request("You need to provide a photo to upload") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment