module Faraday::FlatParamsEncoder

def self.encode(params)

Returns:
  • (String) - the URI querystring (without the leading '?')

Parameters:
  • params (Hash) -- query arguments to convert.
def self.encode(params)
  return nil if params.nil?
  unless params.is_a?(Array)
    unless params.respond_to?(:to_hash)
      raise TypeError,
            "Can't convert #{params.class} into Hash."
    end
    params = params.to_hash
    params = params.map do |key, value|
      key = key.to_s if key.is_a?(Symbol)
      [key, value]
    end
    # Only to be used for non-Array inputs. Arrays should preserve order.
    params.sort! if @sort_params
  end
  # The params have form [['key1', 'value1'], ['key2', 'value2']].
  buffer = +''
  params.each do |key, value|
    encoded_key = escape(key)
    if value.nil?
      buffer << "#{encoded_key}&"
    elsif value.is_a?(Array)
      if value.empty?
        buffer << "#{encoded_key}=&"
      else
        value.each do |sub_value|
          encoded_value = escape(sub_value)
          buffer << "#{encoded_key}=#{encoded_value}&"
        end
      end
    else
      encoded_value = escape(value)
      buffer << "#{encoded_key}=#{encoded_value}&"
    end
  end
  buffer.chop
end