Module: URI::QueryParams::Mixin

Included in:
Addressable::URI, Generic
Defined in:
lib/uri/query_params/mixin.rb

Overview

Adds the ability to parse individual parameters from a the query field of a URI.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#query_paramsHash{String => String}

The query params of the URI.



89
90
91
92
# File 'lib/uri/query_params/mixin.rb', line 89

def query_params
  parse_query_params! unless @query_params
  return @query_params
end

Class Method Details

.included(base) ⇒ Object

Called when URI::QueryParams::Mixin is included into a URI Class. Overrides the query and query= methods to transparently update the #query_params.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/uri/query_params/mixin.rb', line 17

def self.included(base)
  base.module_eval do
    alias raw_query query

    #
    # The raw query-string.
    #
    # @return [String, nil]
    #   The raw query-string.
    #
    # @see QueryParams.dump
    #
    # @since 0.5.2
    #
    def query
      if @query_params
        URI::QueryParams.dump(@query_params)
      else
        raw_query
      end
    end

    alias raw_query= query=

    #
    # Sets the query string and updates query_params.
    #
    # @param [String] query_str
    #   The new URI query string to use.
    #
    # @return [String]
    #   The new URI query string.
    #
    # @example
    #   url.query = 'a=1&b=2'
    #   # => "a=1&b=2"
    #
    # @see QueryParams.parse
    #
    def query=(new_query)
      new_query = (self.raw_query = new_query)

      parse_query_params! if @query_params
      return new_query
    end
  end
end

Instance Method Details

#each_query_param {|name, value| ... } ⇒ Object

Iterates over every query parameter.

Examples:

url.each_query_param do |name,value|
  puts "#{name} = #{value}"
end

Yields:

  • (name, value)

    The block to pass each query parameter to.

Yield Parameters:

  • name (String)

    The name of the query parameter.

  • value (String)

    The value of the query parameter.



111
112
113
# File 'lib/uri/query_params/mixin.rb', line 111

def each_query_param(&block)
  query_params.each(&block)
end

#initialize_copy(url) ⇒ Object

Deep-copies the #query_params from another URL.

Since:

  • 0.5.1



73
74
75
76
77
78
79
# File 'lib/uri/query_params/mixin.rb', line 73

def initialize_copy(url)
  if (params = url.instance_variable_get('@query_params'))
    @query_params = params.dup
  end

  super(url)
end