Class: Request::MultipartWithFile

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

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/faraday/request/multipart_with_file.rb', line 9

def call(env)
  if env[:body].is_a?(Hash)

    # Check for IO (and IO-like objects, like Zip::InputStream) in the request,
    # which represent data to be uploaded.  Replace these with Faraday
    env[:body].each do |key, value|
      # Faraday seems to expect a few IO methods to be available, but that's all:
      # https://github.com/lostisland/faraday/blob/master/lib/faraday/file_part.rb
      # :length seems to be an optional one
      #
      # UploadIO also seems to do a duck typing check for :read, with :path optional
      # https://www.rubydoc.info/gems/multipart-post/2.0.0/UploadIO:initialize
      #
      # We attempt to make our duck typing compatible with their duck typing
      if value.respond_to?(:read) && value.respond_to?(:rewind) && value.respond_to?(:close)
        env[:body][key] = Faraday::Multipart::FilePart.new(value, mime_type(value))
      end
    end
  end

  @app.call(env)
end