Sha256: 70e13031ad1ce4817c0633dfeb3bee9151c299256562021d815f64380ff7d058

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

require 'faraday'

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

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

      private

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

Faraday::Request.register_middleware twitter_multipart_with_file: Twitter::REST::Request::MultipartWithFile

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twitter-6.1.0 lib/twitter/rest/request/multipart_with_file.rb
twitter-6.0.0 lib/twitter/rest/request/multipart_with_file.rb