Class: FoxyCart::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/foxycart/util.rb

Class Method Summary collapse

Class Method Details

.flatten_params(params, parent_key = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/foxycart/util.rb', line 7

def self.flatten_params(params, parent_key=nil)
  result = []
  params.each do |key, value|
    calculated_key = parent_key ? "#{parent_key}[#{url_encode(key)}]" : url_encode(key)
    if value.is_a?(Hash)
      result += flatten_params(value, calculated_key)
    elsif value.is_a?(Array)
      result += flatten_params_array(value, calculated_key)
    else
      result << [calculated_key, value]
    end
  end
  result
end

.flatten_params_array(value, calculated_key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/foxycart/util.rb', line 22

def self.flatten_params_array(value, calculated_key)
  result = []
  value.each do |elem|
    if elem.is_a?(Hash)
      result += flatten_params(elem, calculated_key)
    elsif elem.is_a?(Array)
      result += flatten_params_array(elem, calculated_key)
    else
      result << ["#{calculated_key}[]", elem]
    end
  end
  result
end

.url_encode(key) ⇒ Object



3
4
5
# File 'lib/foxycart/util.rb', line 3

def self.url_encode(key)
  URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end