Created
March 9, 2009 02:05
-
-
Save jkatz/76054 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def query_string_encode(data) | |
query = [] | |
data.each { |k,v| query << query_type(k, v) } | |
query.join('&') | |
end | |
def query_string_encode_array(key, array) | |
query = [] | |
array.each do |v| | |
url_key = "#{key.to_s}[]" | |
query << query_type(url_key, v) | |
end | |
query.join('&') | |
end | |
def query_string_encode_hash(key, hash) | |
query = [] | |
hash.each do |k,v| | |
url_key = "#{key.to_s}[#{k.to_s}]" | |
query << query_type(url_key, v) | |
end | |
query.join('&') | |
end | |
def query_type(key, value) | |
case value | |
when Array | |
query_string_encode_array(key, value) | |
when Hash | |
query_string_encode_hash(key, value) | |
else | |
[key.to_s, value.to_s].join('=') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment