Class: Common::Client::Middleware::Request::MultipartRequest
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- Common::Client::Middleware::Request::MultipartRequest
- Includes:
- Rack::Mime
- Defined in:
- lib/common/client/middleware/request/multipart_request.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #file_name(value) ⇒ Object private
- #io_object_for(key, value, io_keys) ⇒ Object private
- #make_io_for_body(key, value) ⇒ Object private
Instance Method Details
#call(env) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/common/client/middleware/request/multipart_request.rb', line 12 def call(env) return @app.call(env) unless env[:body].is_a?(Hash) io_keys = [] env[:body].each do |key, value| env[:body][key] = io_object_for(key, value, io_keys) end if io_keys.any? env[:body].except(*io_keys.uniq).each do |key, value| env[:body][key] = make_io_for_body(key, value) end end @app.call(env) end |
#file_name(value) ⇒ Object (private)
55 56 57 |
# File 'lib/common/client/middleware/request/multipart_request.rb', line 55 def file_name(value) value.original_filename || value.path end |
#io_object_for(key, value, io_keys) ⇒ Object (private)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/common/client/middleware/request/multipart_request.rb', line 32 def io_object_for(key, value, io_keys) if value.respond_to?(:to_io) io_keys << key Faraday::UploadIO.new( value, mime_type(File.extname(value.path)), file_name(value) ) elsif value.is_a?(Array) value.map { |each_value| io_object_for(key, each_value, io_keys) } else value end end |
#make_io_for_body(key, value) ⇒ Object (private)
47 48 49 50 51 52 53 |
# File 'lib/common/client/middleware/request/multipart_request.rb', line 47 def make_io_for_body(key, value) Faraday::UploadIO.new( StringIO.new(value.to_json), 'application/json', key.to_s ) end |