Module: HTTParty::HashConversions

Defined in:
lib/extensions/httparty/hash_conversions.rb

Overview

Extension of HTTParty:HashConversions. Changes made to add index to arrays github.com/jnunemaker/httparty/blob/master/lib/httparty/hash_conversions.rb#L42,L55

Class Method Summary collapse

Class Method Details

.normalize_keys(key, value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/extensions/httparty/hash_conversions.rb', line 8

def self.normalize_keys(key, value)
  stack = []
  normalized_keys = []

  if value.respond_to?(:to_ary)
    if value.empty?
      normalized_keys << ["#{key}[]", '']
    else
      normalized_keys = value.to_ary.flat_map.with_index do
        |element, index| normalize_keys("#{key}[#{index}]", element)
      end
    end
  elsif value.respond_to?(:to_hash)
    stack << [key, value.to_hash]
  else
    normalized_keys << [key.to_s, value]
  end

  stack.each do |parent, hash|
    hash.each do |child_key, child_value|
      if child_value.respond_to?(:to_hash)
        stack << ["#{parent}[#{child_key}]", child_value.to_hash]
      elsif child_value.respond_to?(:to_ary)
        child_value.to_ary.each_with_index do |v, index|
          normalized_keys << normalize_keys("#{parent}[#{child_key}][#{index}]", v).flatten
        end
      else
        normalized_keys << normalize_keys("#{parent}[#{child_key}]", child_value).flatten
      end
    end
  end

  normalized_keys
end