Module: Patron::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/patron/util.rb

Instance Method Summary collapse

Instance Method Details

#build_query_pairs_from_hash(hash, escape_values = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/patron/util.rb', line 32

def build_query_pairs_from_hash(hash, escape_values=false)
  pairs = []
  recursive = Proc.new do |h, prefix|
    h.each_pair do |k,v|
      key = prefix == '' ? k : "#{prefix}[#{k}]"
      v = CGI::escape(v.to_s) if escape_values
      v.is_a?(Hash) ? recursive.call(v, key) : pairs << "#{key}=#{v}"
    end
  end
  recursive.call(hash, '')
  pairs
end

#build_query_string_from_hash(hash, escape_values = false) ⇒ Object



45
46
47
# File 'lib/patron/util.rb', line 45

def build_query_string_from_hash(hash, escape_values=false)
  build_query_pairs_from_hash(hash, escape_values).join('&')
end