aboutsummaryrefslogtreecommitdiffstats
path: root/lib/uri/common.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/uri/common.rb')
-rw-r--r--lib/uri/common.rb24
1 files changed, 21 insertions, 3 deletions
diff --git a/lib/uri/common.rb b/lib/uri/common.rb
index b23e971f5e..719dbca4b8 100644
--- a/lib/uri/common.rb
+++ b/lib/uri/common.rb
@@ -798,15 +798,33 @@ module URI
# This is an implementation of
# http://www.w3.org/TR/html5/forms.html#url-encoded-form-data
#
+ # URI.encode_www_form([["q", "ruby"], ["lang", "en"]])
+ # #=> "q=ruby&lang=en"
+ # URI.encode_www_form("q" => "ruby", "lang" => "en")
+ # #=> "q=ruby&lang=en"
+ # URI.encode_www_form("q" => ["ruby", "perl"], "lang" => "en")
+ # #=> "q=ruby&q=perl&lang=en"
+ # URI.encode_www_form([["q", "ruby"], ["q", "perl"], ["lang", "en"]])
+ # #=> "q=ruby&q=perl&lang=en"
+ #
# See URI.encode_www_form_component, URI.decode_www_form
def self.encode_www_form(enum)
enum.map do |k,v|
- str = encode_www_form_component(k)
- if v
+ if v.nil?
+ encode_www_form_component(k)
+ elsif v.respond_to?(:to_ary)
+ v.to_ary.map do |w|
+ str = encode_www_form_component(k)
+ unless w.nil?
+ str << '='
+ str << encode_www_form_component(w)
+ end
+ end.join('&')
+ else
+ str = encode_www_form_component(k)
str << '='
str << encode_www_form_component(v)
end
- str
end.join('&')
end