Method: HTTPTools::Encoding.www_form_encode

Defined in:
lib/http_tools/encoding.rb

.www_form_encode(hash) ⇒ Object

:call-seq: Encoding.www_form_encode(hash) -> string

Takes a Hash and converts it to a String as if it was a HTML form being submitted, e.g. => “fish”, “lang” => “en” becomes “query=fish&lang=en”

To get multiple key value pairs with the same key use an array as the value, e.g. => [“en”, “fr”] become “lang=en&lang=fr”



54
55
56
57
58
59
60
61
62
# File 'lib/http_tools/encoding.rb', line 54

def www_form_encode(hash)
  hash.map do |key, value|
    if value.respond_to?(:map) && !value.is_a?(String)
      value.map {|val| www_form_encode(key => val.to_s)}.join(AMPERSAND)
    else
      url_encode(key.to_s) << EQUALS << url_encode(value.to_s)
    end
  end.join(AMPERSAND)
end