Add parameter to url Add parameter to url ruby ruby

Add parameter to url


require 'uri'uri =  URI.parse("http://www.youtube.com/watch?v=og9B3BEnBHo")uri.query = [uri.query, "wmode=opaque"].compact.join('&') puts uri.to_s#edit Since 1.9.2 there are methods added to the URI moduleuri =  URI.parse("http://www.youtube.com/watch?v=og9B3BEnBHo")new_query_ar = URI.decode_www_form(String(uri.query)) << ["wmode", "opaque"]uri.query = URI.encode_www_form(new_query_ar)puts uri.to_s

(The call to String ensures that this also works in the case when the original URI does not have a query string)


As Ruby has evolved over the years the answer differs between versions.

After 1.9.2

Ruby 1.9.2 saw decode_www_form and encode_www_form added to the URI module, making encoding parameters easier.

require 'uri'uri = URI.parse("http://www.youtube.com/watch?v=og9B3BEnBHo")new_query_ar = URI.decode_www_form(uri.query || '') << ["wmode", "opaque"]uri.query = URI.encode_www_form(new_query_ar)puts uri.to_s

Explanation

URI.decode_www_form breaks a string of query parameters (uri.query) into a nested array of parameters ([["v", "og9B3BEnBHo"]])

uri.query || '' supplies either the query string of the uri, or if it does not exist, an empty string. This prevents decode_www_form from running into an error if uri.query is nil.

<< ["wmode", "opaque"] adds another element to the array of query parameters. You may add more by further extending new_query_ar: new_query_ar << ["fullscreen", "1"]

URI.encode_www_form encodes the nested array new query parameters into a string.

Before 1.9.2

require 'uri'uri = URI.parse("http://www.youtube.com/watch?v=og9B3BEnBHo")uri.query = [uri.query, "wmode=opaque"].compact.join('&') puts uri.to_s

Explanation

[uri.query, "wmode=opaque"] is an array of all the eventual query parameters. You may add more by extending the array: [uri.query, "wmode=opaque", "fullscreen=1"] or by adding to the final element: "wmode=opaque&fullscreen=1"

compact removes nil elements from an array, thus it removes uri.query if there is not an existing query parameter.

join, finally, joins them into a query string.


Another option is to use the Addressable gem

https://github.com/sporkmonger/addressable

Once you include Addressable in your project all you will have to do is:

url = Addressable::URI.parse('http://www.youtube.com/watch?v=og9B3BEnBHo')url.query_values = (url.query_values || {}).merge(wmode:"opaque")