Permalink
Browse files

Added support for nested hashes, without this Weary can't be

upgraded to latest addressable gem version
  • Loading branch information...
1 parent ee198d4 commit b616232ce5801ced076ebb10738d687c2b98486b @mariochavez committed Aug 7, 2012
Showing with 24 additions and 1 deletion.
  1. +17 −1 lib/addressable/uri.rb
  2. +7 −0 spec/addressable/uri_spec.rb
View
@@ -1491,8 +1491,9 @@ def query_values=(new_query_values)
"Can't convert #{new_query_values.class} into Hash."
end
new_query_values = new_query_values.to_hash
+ new_query_values = flatten_keys new_query_values
new_query_values = new_query_values.map do |key, value|
- key = key.to_s if key.kind_of?(Symbol)
+ #key = key.to_s if key.kind_of?(Symbol)
[key, value]
end
# Useful default for OAuth and caching.
@@ -1525,6 +1526,21 @@ def query_values=(new_query_values)
self.query = buffer.chop
end
+ def flatten_keys hash, keys=nil
+ new_hash = {}
+ hash.map do |k, v|
+ string_key = k.to_s
+ new_keys = keys ? "#{keys}[#{string_key}]" : string_key
+ if v.is_a?(Hash)
+ sub_hash = flatten_keys v, new_keys
+ new_hash.merge! sub_hash
+ else
+ new_hash[new_keys] = v
+ end
+ end
+ new_hash
+ end
+
##
# The HTTP request URI for this URI. This is the path and the
# query string.
@@ -2933,6 +2933,13 @@ def to_s
}
end
+ it "should have the correct query string after multilevel hash assignment" do
+ @uri.query_values = {:flag1 => 'value1', :flag2 => {:flag3 => {:flag4 => 'value2'}}}
+ @uri.query.split("&").should include("flag1=value1")
+ @uri.query.split("&").should include("flag2%5Bflag3%5D%5Bflag4%5D=value2")
+ @uri.query_values(Array).sort.should == [["flag1", "value1"], ["flag2[flag3][flag4]", "value2"]]
+ end
+
it "should have the correct query string after flag hash assignment" do
@uri.query_values = {'flag?1' => true, 'fl=ag2' => true, 'flag3' => true}
@uri.query.split("&").should include("flag%3F1")

0 comments on commit b616232

Please sign in to comment.