Class: Request::ConvertFileToUploadIO

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday/request/convert_file_to_upload_io.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/faraday/request/convert_file_to_upload_io.rb', line 3

def call(env)
  if env[:body].is_a?(Hash)
    resource = env[:body].keys.first
    env[:body][resource].each do |key, value|
      if value.is_a?(File) || value.is_a?(Tempfile)
        env[:body][resource][key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
      elsif value.is_a?(Hash) && (value['io'].is_a?(IO) || value['io'].is_a?(StringIO))
        env[:body][resource][key] = Faraday::UploadIO.new(value['io'], mime_type('.'+value['type']), '')
      end
    end
  end
  @app.call(env)
end