Sha256: 85b8d625e2562b337f9830a637466541d67fa415e97f1b53d34be4be62b71a77

Contents?: true

Size: 894 Bytes

Versions: 1

Compression:

Stored size: 894 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)
        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 if env[:body].is_a?(::Hash)
        @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

1 entries across 1 versions & 1 rubygems

Version Path
twitter-4.4.0 lib/twitter/request/multipart_with_file.rb