Sha256: d088073f6bd838d6e4b35acaf517be960a354af7d1fe07965942921eee66760c

Contents?: true

Size: 867 Bytes

Versions: 5

Compression:

Stored size: 867 Bytes

Contents

require 'faraday'

module Twitter
  module REST
    module Request
      class MultipartWithFile < Faraday::Middleware
        CONTENT_TYPE = 'Content-Type'
        GIF_REGEX = /\.gif$/i
        JPEG_REGEX = /\.jpe?g/i
        PNG_REGEX = /\.png$/i

        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)
            end
          end if env[:body].is_a?(::Hash)
          @app.call(env)
        end

      private

        def mime_type(path)
          case path
          when GIF_REGEX
            'image/gif'
          when JPEG_REGEX
            'image/jpeg'
          when PNG_REGEX
            'image/png'
          else
            'application/octet-stream'
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
twitter-5.5.1 lib/twitter/rest/request/multipart_with_file.rb
twitter-5.5.0 lib/twitter/rest/request/multipart_with_file.rb
twitter-5.4.1 lib/twitter/rest/request/multipart_with_file.rb
twitter-5.4.0 lib/twitter/rest/request/multipart_with_file.rb
twitter-5.3.1 lib/twitter/rest/request/multipart_with_file.rb