Class: HashUtil

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

Class Method Summary collapse

Class Method Details

.to_params(hash) ⇒ Object


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/web_translate_it/util/hash_util.rb', line 2

def self.to_params(hash)
  params = ''
  stack = []

  hash.each do |k, v|
    if v.is_a?(Hash)
      stack << [k,v]
    else
      params << "#{k}=#{v}&"
    end
  end

  stack.each do |parent, hash|
    hash.each do |k, v|
      if v.is_a?(Hash)
        stack << ["#{parent}[#{k}]", v]
      else
        params << "#{parent}[#{k}]=#{v}&"
      end
    end
  end

  params.chop! # trailing &
  params
end