module HTTP::FormData

def create(data, encoder: nil)

Returns:
  • (Urlencoded) - otherwise
  • (Multipart) - if any of values is a {FormData::File}

Parameters:
  • data (Enumerable, Hash, #to_h) --

Other tags:
    Api: - public
def create(data, encoder: nil)
  data = ensure_data data
  if multipart?(data)
    Multipart.new(data)
  else
    Urlencoded.new(data, encoder: encoder)
  end
end

def ensure_data(obj)

Returns:
  • (Enumerable) -

Raises:
  • (Error) - `obj` can't be coerced

Other tags:
    Api: - public
def ensure_data(obj)
  if    obj.nil?                  then []
  elsif obj.is_a?(Enumerable)     then obj
  elsif obj.respond_to?(:to_h)    then obj.to_h
  else raise Error, "#{obj.inspect} is neither Enumerable nor responds to :to_h"
  end
end

def ensure_hash(obj)

Returns:
  • (Hash) -

Raises:
  • (Error) - `obj` can't be coerced

Other tags:
    Api: - public
def ensure_hash(obj)
  if    obj.is_a?(Hash)        then obj
  elsif obj.respond_to?(:to_h) then obj.to_h
  else raise Error, "#{obj.inspect} is neither Hash nor responds to :to_h"
  end
end

def multipart?(data)

Returns:
  • (Boolean) -

Parameters:
  • data (Enumerable) --

Other tags:
    Api: - private
def multipart?(data)
  data.any? do |_, v|
    v.is_a?(Part) || (v.respond_to?(:to_ary) && v.to_ary.any?(Part))
  end
end