lib/rackbox/rackbox.rb in remi-rackbox-1.0.1 vs lib/rackbox/rackbox.rb in remi-rackbox-1.1.0

- old
+ new

@@ -29,7 +29,39 @@ raise "RackBox.app not configured." end end @app end + + # helper method for taking a Hash of params and turning them into POST params + # + # >> RackBox.build_query :hello => 'there' + # => 'hello=there' + # + # >> RackBox.build_query :hello => 'there', :foo => 'bar' + # => 'hello=there&foo=bar' + # + # >> RackBox.build_query :user => { :name => 'bob', :password => 'secret' } + # => 'user[name]=bob&user[password]=secret' + # + def build_query params_hash = { } + # check to make sure no values are Hashes ... + # if they are, we need to flatten them! + params_hash.each do |key, value| + # params_hash :a => { :b => X, :c => Y } + # needs to be 'a[b]' => X, 'a[b]' => Y + if value.is_a? Hash + inner_hash = params_hash.delete key # { :b => X, :c => Y } + inner_hash.each do |subkey, subvalue| + new_key = "#{ key }[#{ subkey }]" # a[b] or a[c] + puts "warning: overwriting query parameter #{ new_key }" if params_hash[new_key] + params_hash[new_key] = subvalue # 'a[b]' => X or a[c] => Y + end + # we really shouldn't keep going thru the #each now that we've altered data! + return build_query(params_hash) + end + end + Rack::Utils.build_query params_hash + end + end end