Skip to content

Commit

Permalink
support file upload (-f or --file option) via multipart form
Browse files Browse the repository at this point in the history
  • Loading branch information
theganyo committed Mar 29, 2013
1 parent c8d0142 commit 97cc802
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
6 changes: 5 additions & 1 deletion ugc/bin/ugc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ end
on_error do |e|
# return false to skip default error handling
if e.is_a? RestClient::Exception
puts e.response.data['error_description']
begin
puts e.response.data['error_description']
rescue MultiJson::DecodeError
puts e.response
end
false
else
true
Expand Down
11 changes: 9 additions & 2 deletions ugc/lib/ugc/commands/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
arg_name 'url [data]'

command :post,:create do |c|
c.flag [:d,:data]
c.flag [:d,:data], :desc => 'does nothing: for ease of conversion from curl'
c.flag [:f,:file], :must_match => /.*=.*/, :arg_name => 'field_name=file_name', :desc => 'will force multipart upload'

c.action do |global_options,options,args|
help_now! unless args[0]

format_response $context[args[0]].post parse_data(options[:data] || args[1])
resource = $context[args[0]]
payload = parse_data(options[:data] || args[1])
if options[:file]
format_response multipart_upload resource, payload, options[:file]
else
format_response resource.post payload
end
end

end
11 changes: 9 additions & 2 deletions ugc/lib/ugc/commands/put.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
arg_name 'url [data]'

command :put,:update do |c|
c.flag [:d,:data]
c.flag [:d,:data], :desc => 'does nothing: for ease of conversion from curl'
c.flag [:f,:file], :must_match => /=/, :arg_name => 'field_name=file_name', :desc => 'will force multipart upload'

c.action do |global_options,options,args|
help_now! unless args[0]

format_response $context[args[0]].put parse_data(options[:data] || args[1])
resource = $context[args[0]]
payload = parse_data(options[:data] || args[1])
if options[:file]
format_response multipart_upload resource, payload, options[:file], :put
else
format_response resource.put payload
end
end

end
17 changes: 17 additions & 0 deletions ugc/lib/ugc/helpers/rest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def multipart_upload(resource, payload, file, method=:post, additional_headers = {})
payload = payload.is_a?(Hash) ? payload : MultiJson.load(payload)
filename = 'file'
filename, file = file.split '=' if file.is_a?(String)
file = file[1..-1] if file.start_with? '@' # be kind to curl users
payload[filename] = file.is_a?(File) ? file : File.new(file, 'rb')
payload[:multipart] = true
headers = (resource.options[:headers] || {}).merge(additional_headers)
response = RestClient::Request.execute(resource.options.merge(
:method => method,
:url => resource.url,
:payload => payload,
:headers => headers))
resource.instance_variable_set :@response, response
response.resource = resource
response
end

0 comments on commit 97cc802

Please sign in to comment.