Module: HTTParty::HashConversions

Defined in:
lib/pearson_kitchen_manager.rb

Class Method Summary collapse

Class Method Details

.normalize_param(key, value) ⇒ String

Returns This key value pair as a param.

Examples:

normalize_param(:name, “Bob Jones”) #=> “name=Bob%20Jones&”

Parameters:

  • key (Object)

    The key for the param.

  • value (Object)

    The value for the param.

Returns:

  • (String)

    This key value pair as a param



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/pearson_kitchen_manager.rb', line 108

def self.normalize_param(key, value)
  param = ''
  stack = []

  if value.is_a?(Array)
    param << Hash[*(0...value.length).to_a.zip(value).flatten].map {|i,element| normalize_param("#{key}[#{i}]", element)}.join
  elsif value.is_a?(Hash)
    stack << [key,value]
  else
    param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
  end

  stack.each do |parent, hash|
    hash.each do |key, value|
      if value.is_a?(Hash)
        stack << ["#{parent}[#{key}]", value]
      else
        param << normalize_param("#{parent}[#{key}]", value)
      end
    end
  end

  param
end