module URI::QueryParams::Mixin
Adds the ability to parse individual parameters from a the query field of a URI
.
Attributes
Allows setting the query_params.
Public Class Methods
Called when {Mixin} is included into a URI
Class. Overrides the `query` and `query=` methods to transparently update the {#query_params}.
# 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
Public Instance Methods
Iterates over every query parameter.
@yield [name, value]
The block to pass each query parameter to.
@yieldparam [String] name
The name of the query parameter.
@yieldparam [String] value
The value of the query parameter.
@example
url.each_query_param do |name,value| puts "#{name} = #{value}" end
# File lib/uri/query_params/mixin.rb, line 111 def each_query_param(&block) query_params.each(&block) end
Deep-copies the {#query_params} from another URL.
@param [Mixin] url
The other URL to deep-copy the query_params from.
@since 0.5.1
# 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
The raw query-string.
@return [String, nil]
The raw query-string.
@see QueryParams.dump
@since 0.5.2
# File lib/uri/query_params/mixin.rb, line 31 def query if @query_params URI::QueryParams.dump(@query_params) else raw_query end end
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
# File lib/uri/query_params/mixin.rb, line 56 def query=(new_query) new_query = (self.raw_query = new_query) parse_query_params! if @query_params return new_query end
The query params of the URI
.
@return [Hash{String => String}]
The query params of the URI.
@see QueryParams.parse
# File lib/uri/query_params/mixin.rb, line 89 def query_params parse_query_params! unless @query_params return @query_params end
Private Instance Methods
Parses the query parameters from the query data, populating query_params
with the parsed parameters.
@see QueryParams.parse
@since 0.5.2
# File lib/uri/query_params/mixin.rb, line 125 def parse_query_params! @query_params = URI::QueryParams.parse(@query) end