Ruby: How to Turn a Hash into Http Parameters

Ruby: How to turn a hash into HTTP parameters?

Update: This functionality was removed from the gem.

Julien, your self-answer is a good one, and I've shameless borrowed from it, but it doesn't properly escape reserved characters, and there are a few other edge cases where it breaks down.

require "addressable/uri"
uri = Addressable::URI.new
uri.query_values = {:a => "a", :b => ["c", "d", "e"]}
uri.query
# => "a=a&b[0]=c&b[1]=d&b[2]=e"
uri.query_values = {:a => "a", :b => [{:c => "c", :d => "d"}, {:e => "e", :f => "f"}]}
uri.query
# => "a=a&b[0][c]=c&b[0][d]=d&b[1][e]=e&b[1][f]=f"
uri.query_values = {:a => "a", :b => {:c => "c", :d => "d"}}
uri.query
# => "a=a&b[c]=c&b[d]=d"
uri.query_values = {:a => "a", :b => {:c => "c", :d => true}}
uri.query
# => "a=a&b[c]=c&b[d]"
uri.query_values = {:a => "a", :b => {:c => "c", :d => true}, :e => []}
uri.query
# => "a=a&b[c]=c&b[d]"

The gem is 'addressable'

gem install addressable

Unable to convert url params string to hash and then back to url string

The CGI module is a complete dinosaur and should probably be thrown in the garbage because of how bad it is, but for some reason it persists in the Ruby core. Maybe some day someone will refactor it and make it workable. Until then, skip it and use something better like URI, which is also built-in.

Given your irregular, non-compliant query string:

query_string = 'something=1,2,3,4,5&something_else=6,7,8'

You can handle this by using the decode_www_form method which handles query-strings:

require 'uri'
decoded = URI.decode_www_form(query_string).to_h
# => {"something"=>"1,2,3,4,5", "something_else"=>"6,7,8"}

To re-encode it you just call encode_www_form and then force unescape to undo what it's correctly doing to handle the , values:

encoded = URI.unescape(URI.encode_www_form(decoded))
# => "something=1,2,3,4,5&something_else=6,7,8"

That should get the effect you want.

Convert POST parameters to hash in Ruby without rails

URI.decode_www_form from the Ruby standard library can do this: http://rubydoc.info/docs/ruby-stdlib/1.9.2/URI#decode_www_form-class_method

Passing a hash into a link is not encoded

In rails 5 params isn't a subclass of Hash anymore (for security reasons).

See: Rails 5: unable to retrieve hash values from parameter

To overcome this you can call to_unsafe_h.

It'll turn params in a proper hash and thus properly encoded it in the url.

<%= link_to "My Path", slides_path(query: params.to_unsafe_h[:q]) %>

How to generate query string hash with query parameters

The parameters that are hashed in:

qsh = Digest::SHA256.hexdigest("GET&#{endpoint}&limit=50&start=50")

should be added in the request url:

"#{jwt_auth.api_base_url}#{endpoint}?jwt=#{jwt}&start=50&limit=50"

Ruby hash with duplicate keys to create URL parameters

require 'net/http'
params = {
:center => Geocoder.coordinates(currentlocation).join(","),
:zoom => 10,
:size => "460x280",
:markers => [Geocoder.coordinates(markerlocation).join(",")],
:sensor => true,
:key => ENV["GOOGLE_API_KEY"]
}
query_string = URI.encode_www_form(params)
image_tag ...

How can I get named parameters into a Hash?

def my_method(required_param, named_param_1: nil, named_param_2: nil)
named_params = method(__method__).parameters.each_with_object({}) do |p,h|
h[p[1]] = eval(p[1].to_s) if p[0] == :key
end
p named_params # {:named_param_1=>"hello", :named_param_2=>"world"}

# do something with the named params
end

my_method( 'foo', named_param_1: 'hello', named_param_2: 'world' )

How to extract URL parameters from a URL with Ruby or Rails?

I think you want to turn any given URL string into a HASH?

You can try http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000075

require 'cgi'

CGI::parse('param1=value1¶m2=value2¶m3=value3')

returns

{"param1"=>["value1"], "param2"=>["value2"], "param3"=>["value3"]}


Related Topics



Leave a reply



Submit