Sha256: b91448190f862af1cc818bc6ba66ae2bf3cb002aa23777b5d9928a30360bc6c7

Contents?: true

Size: 924 Bytes

Versions: 4

Compression:

Stored size: 924 Bytes

Contents

require 'faraday'

module Twitter
  module Request
    class MultipartWithFile < Faraday::Middleware
      CONTENT_TYPE = 'Content-Type'.freeze
      class << self
        attr_accessor :mime_type
        mime_type = 'multipart/form-data'.freeze
      end

      def call(env)
        if env[:body].is_a?(Hash)
          env[:body].each do |key, value|
            if value.respond_to?(:to_io)
              env[:body][key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
              env[:request_headers][CONTENT_TYPE] = self.class.mime_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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
twitter-4.3.0 lib/twitter/request/multipart_with_file.rb
twitter-4.2.0 lib/twitter/request/multipart_with_file.rb
twitter-4.1.2 lib/twitter/request/multipart_with_file.rb
twitter-4.1.1 lib/twitter/request/multipart_with_file.rb