Method: Iri#add

Defined in:
lib/iri.rb

#add(hash) ⇒ Iri Also known as: with

Adds query parameters to the URI.

This method appends query parameters to existing ones. If a parameter with the same name already exists, both values will be present in the resulting URI.

You can ensure only one instance of a parameter by using del first:

Examples:

Adding query parameters

Iri.new('https://google.com').add(q: 'test', limit: 10)
# => "https://google.com?q=test&limit=10"

Adding parameters with the same name

Iri.new('https://google.com?q=foo').add(q: 'bar')
# => "https://google.com?q=foo&q=bar"

Replacing a parameter by deleting it first

Iri.new('https://google.com?q=foo').del(:q).add(q: 'test')
# => "https://google.com?q=test"

Parameters:

  • hash (Hash)

    Hash of parameter names/values to add to the query part

Returns:

  • (Iri)

    A new Iri instance

Raises:

See Also:



141
142
143
144
145
146
147
148
149
150
# File 'lib/iri.rb', line 141

def add(hash)
  raise ArgumentError, "The hash can't be nil" if hash.nil?
  raise InvalidArguments unless hash.is_a?(Hash)
  modify_query do |params|
    hash.each do |k, v|
      params[k.to_s] = [] unless params[k.to_s]
      params[k.to_s] << v
    end
  end
end