module Faraday::DecodeMethods

Sub-module for decoding query-string into parameters.

Constants

SUBKEYS_REGEX

Public Instance Methods

@param query [nil, String]

@return [Array<Array, String>] the decoded params

@raise [TypeError] if the nesting is incorrect

# File lib/faraday/encoders/nested_params_encoder.rb, line 87
def decode(query)
  return nil if query.nil?

  params = {}
  query.split('&').each do |pair|
    next if pair.empty?

    key, value = pair.split('=', 2)
    key = unescape(key)
    value = unescape(value.tr('+', ' ')) if value
    decode_pair(key, value, params)
  end

  dehash(params, 0)
end

Protected Instance Methods

# File lib/faraday/encoders/nested_params_encoder.rb, line 160
def add_to_context(is_array, context, value, subkey)
  is_array ? context << value : context[subkey] = value
end

Returns the hash in the array that the remaining subkeys go into. Like Rack::Utils.parse_nested_query, this is the last element of the array if it is a hash that does not yet hold the full path of the remaining subkeys. Otherwise the subkeys start a new hash.

# File lib/faraday/encoders/nested_params_encoder.rb, line 143
def array_entry(array, subkeys)
  array << {} unless array.last.is_a?(Hash) && !key_path?(array.last, subkeys)
  array.last
end
# File lib/faraday/encoders/nested_params_encoder.rb, line 107
def decode_pair(key, value, context)
  subkeys = key.scan(SUBKEYS_REGEX)
  validate_params_depth!(subkeys.length)

  subkeys.each_with_index do |subkey, i|
    context = array_entry(context, subkeys[i..]) if context.is_a?(Array)
    is_array = subkey =~ /[\[\]]+\Z/
    subkey = Regexp.last_match.pre_match if is_array
    last_subkey = i == subkeys.length - 1

    context = prepare_context(context, subkey, is_array, last_subkey)
    add_to_context(is_array, context, value, subkey) if last_subkey
  end
end

Internal: convert a nested hash with purely numeric keys into an array. FIXME: this is not compatible with Rack::Utils.parse_nested_query @!visibility private

# File lib/faraday/encoders/nested_params_encoder.rb, line 173
def dehash(hash, depth)
  hash.each do |key, value|
    hash[key] = dehash(value, depth + 1) if value.is_a?(Hash)
  end

  if depth.positive? && !hash.empty? && hash.keys.all? { |k| k =~ /^\d+$/ }
    hash.sort_by { |key, _| key.to_i }.map(&:last)
  else
    hash
  end
end

A path that goes through an array never matches, because the array takes one more value instead, as in Rack.

# File lib/faraday/encoders/nested_params_encoder.rb, line 150
def key_path?(hash, subkeys)
  return false if subkeys.any? { |subkey| subkey.end_with?('[]') }

  subkeys.all? do |subkey|
    found = hash.is_a?(Hash) && hash.key?(subkey)
    hash = hash[subkey] if found
    found
  end
end
# File lib/faraday/encoders/nested_params_encoder.rb, line 129
def new_context(subkey, is_array, context)
  value_type = is_array ? Array : Hash
  if context[subkey] && !context[subkey].is_a?(value_type)
    raise TypeError, "expected #{value_type.name} " \
                     "(got #{context[subkey].class.name}) for param `#{subkey}'"
  end

  context[subkey] ||= value_type.new
end
# File lib/faraday/encoders/nested_params_encoder.rb, line 122
def prepare_context(context, subkey, is_array, last_subkey)
  if !last_subkey || is_array
    context = new_context(subkey, is_array, context)
  end
  context
end
# File lib/faraday/encoders/nested_params_encoder.rb, line 164
def validate_params_depth!(depth)
  return unless @param_depth_limit && depth > @param_depth_limit

  raise Faraday::Error, "exceeded nested parameter depth limit of #{@param_depth_limit}"
end