Sha256: 7c95d8c68cac1a9f5a51715d3b4942342b9bfe1f287853a910d1bd8f28fd308c

Contents?: true

Size: 976 Bytes

Versions: 2

Compression:

Stored size: 976 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(request)
          request.body.each do |key, value|
            next unless value.respond_to?(:to_io)
            request.body[key] = Faraday::UploadIO.new(value, mime_type(value.path), value.path)
          end if request.body.is_a?(::Hash)
          @app.call(request)
        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

Faraday::Request.register_middleware :multipart_with_file => Twitter::REST::Request::MultipartWithFile

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twitter-5.11.0 lib/twitter/rest/request/multipart_with_file.rb
twitter-5.10.0 lib/twitter/rest/request/multipart_with_file.rb