Sha256: 96df1f356d889d3add96faccb98cbc7cb50f4c889ff545db3339559f5dfc25ee

Contents?: true

Size: 840 Bytes

Versions: 10

Compression:

Stored size: 840 Bytes

Contents

require 'faraday'

# @private
module Faraday
  # @private
  class Request::MultipartWithFile < Faraday::Middleware
    def call(env)
      if env[:body].is_a?(Hash)
        env[:body].each do |key, value|
          if value.is_a?(File)
            env[:body][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][key] = Faraday::UploadIO.new(value['io'], mime_type('.'+value['type']), '')
          end
        end
      end

      @app.call(env)
    end

    private

    def mime_type(path)
      case path
      when /\.jpe?g/i
        'image/jpeg'
      when /\.gif$/i
        'image/gif'
      when /\.png$/i
        'image/png'
      else
        'application/octet-stream'
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
angellist_api-1.0.1 lib/faraday/request/multipart_with_file.rb
angellist_api-0.1.2 lib/faraday/request/multipart_with_file.rb
angellist_api-1.0.0 lib/faraday/request/multipart_with_file.rb
peerindex-0.0.1 lib/faraday/request/multipart_with_file.rb
angellist_api-0.1.0 lib/faraday/request/multipart_with_file.rb
angellist_api-0.0.9 lib/faraday/request/multipart_with_file.rb
angellist_api-0.0.8 lib/faraday/request/multipart_with_file.rb
angellist_api-0.0.7 lib/faraday/request/multipart_with_file.rb
angellist_api-0.0.6 lib/faraday/request/multipart_with_file.rb
angellist_api-0.0.5 lib/faraday/request/multipart_with_file.rb