Skip to content

Instantly share code, notes, and snippets.

@bensie
Created March 11, 2014 04:20
Show Gist options
  • Save bensie/9479467 to your computer and use it in GitHub Desktop.
Save bensie/9479467 to your computer and use it in GitHub Desktop.
Accept multipart upload with Sinatra
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