Method: Iri#over

Defined in:
lib/iri.rb

#over(hash) ⇒ Iri

Replaces query parameters in the URI.

Unlike #add, this method replaces any existing parameters with the same name rather than adding additional instances. If a parameter doesn’t exist, it will be added.

Examples:

Replacing a query parameter

Iri.new('https://google.com?q=test').over(q: 'hey you!')
# => "https://google.com?q=hey+you%21"

Replacing multiple parameters

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

Parameters:

  • Hash of parameter names/values to replace in the query part

Returns:

  • A new Iri instance

Raises:

  • If the argument is not a Hash

See Also:



197
198
199
200
201
202
203
204
205
206
# File 'lib/iri.rb', line 197

def over(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