class Faraday::Multipart::Middleware

Middleware for supporting multi-part requests.

Constants

CONTENT_TYPE
DEFAULT_BOUNDARY_PREFIX

Public Class Methods

new(app = nil, options = {}) click to toggle source
Calls superclass method
# File lib/faraday/multipart/middleware.rb, line 12
def initialize(app = nil, options = {})
  super(app)
  @options = options
end

Public Instance Methods

call(env) click to toggle source

Checks for files in the payload, otherwise leaves everything untouched.

@param env [Faraday::Env]

# File lib/faraday/multipart/middleware.rb, line 20
def call(env)
  match_content_type(env) do |params|
    env.request.boundary ||= unique_boundary
    env.request_headers[CONTENT_TYPE] +=
      "; boundary=#{env.request.boundary}"
    env.body = create_multipart(env, params)
  end
  @app.call env
end

Private Instance Methods

create_multipart(env, params) click to toggle source

@param env [Faraday::Env] @param params [Hash]

# File lib/faraday/multipart/middleware.rb, line 76
def create_multipart(env, params)
  boundary = env.request.boundary
  parts = process_params(params) do |key, value|
    part(boundary, key, value)
  end
  parts << Faraday::Multipart::Parts::EpiloguePart.new(boundary)

  body = Faraday::Multipart::CompositeReadIO.new(parts)
  env.request_headers[Faraday::Env::ContentLength] = body.length.to_s
  body
end
has_multipart?(obj) click to toggle source

Returns true if obj is an enumerable with values that are multipart.

@param obj [Object] @return [Boolean]

# File lib/faraday/multipart/middleware.rb, line 65
def has_multipart?(obj)
  if obj.respond_to?(:each)
    (obj.respond_to?(:values) ? obj.values : obj).each do |val|
      return true if val.respond_to?(:content_type) || has_multipart?(val)
    end
  end
  false
end
match_content_type(env) { |body| ... } click to toggle source

@param env [Faraday::Env] @yield [request_body] Body of the request

# File lib/faraday/multipart/middleware.rb, line 34
def match_content_type(env)
  return unless process_request?(env)

  env.request_headers[CONTENT_TYPE] ||= mime_type
  return if env.body.respond_to?(:to_str) || env.body.respond_to?(:read)

  yield(env.body)
end
mime_type() click to toggle source

Determines and provides the multipart mime type for the request.

@return [String] the multipart mime type

# File lib/faraday/multipart/middleware.rb, line 125
def mime_type
  @mime_type ||= if @options[:content_type].to_s.match?(%r{\Amultipart/.+})
                   @options[:content_type].to_s
                 else
                   'multipart/form-data'
                 end
end
part(boundary, key, value) click to toggle source
# File lib/faraday/multipart/middleware.rb, line 88
def part(boundary, key, value)
  if value.respond_to?(:to_part)
    value.to_part(boundary, key)
  else
    Faraday::Multipart::Parts::Part.new(boundary, key, value)
  end
end
process_params(params, prefix = nil, pieces = nil, &block) click to toggle source

@param params [Hash] @param prefix [String] @param pieces [Array]

# File lib/faraday/multipart/middleware.rb, line 104
def process_params(params, prefix = nil, pieces = nil, &block)
  params.inject(pieces || []) do |all, (key, value)|
    if prefix
      key = @options[:flat_encode] ? prefix.to_s : "#{prefix}[#{key}]"
    end

    case value
    when Array
      values = value.inject([]) { |a, v| a << [nil, v] }
      process_params(values, key, all, &block)
    when Hash
      process_params(value, key, all, &block)
    else
      all << block.call(key, value) # rubocop:disable Performance/RedundantBlockCall
    end
  end
end
process_request?(env) click to toggle source

@param env [Faraday::Env]

# File lib/faraday/multipart/middleware.rb, line 44
def process_request?(env)
  type = request_type(env)
  env.body.respond_to?(:each_key) && !env.body.empty? && (
    (type.empty? && has_multipart?(env.body)) ||
      (type == mime_type)
  )
end
request_type(env) click to toggle source

@param env [Faraday::Env]

@return [String]

# File lib/faraday/multipart/middleware.rb, line 55
def request_type(env)
  type = env.request_headers[CONTENT_TYPE].to_s
  type = type.split(';', 2).first if type.index(';')
  type
end
unique_boundary() click to toggle source

@return [String]

# File lib/faraday/multipart/middleware.rb, line 97
def unique_boundary
  "#{DEFAULT_BOUNDARY_PREFIX}-#{SecureRandom.hex}"
end