Module: URI::QueryParams

Defined in:
lib/uri/query_params/mixin.rb,
lib/uri/query_params/version.rb,
lib/uri/query_params/query_params.rb

Defined Under Namespace

Modules: Mixin

Constant Summary collapse

VERSION =

uri-query_params version

'0.8.2'
UNSAFE =

RFC 3986 unsafe characters (including ' ')

[
  '!', '*', "'", '(', ')', ';', ':', '@', '&', '=', '+', '$', ',',
  '/', '?', '%', '#', '[', ']', ' ', "\f", "\n", "\r", "\t", "\v",
  "\x7f", *("\x00".."\x1f")
].join

Class Method Summary collapse

Class Method Details

.dump(query_params) ⇒ String

Dumps the URI query params.

Examples:

Dumping Strings

QueryParams.dump('x' => '1', 'y' => '2')
# => "x=1&x=2"

Dumping non-Strings

QueryParams.dump(:x => 1, :y => true, :z => false)
# => "x=1&x=active&z="

Dumping Arrays

QueryParams.dump(:x => ['a','b','c'])
# => "x=a%20b%20c"

Parameters:

  • query_params (Hash{String => String})

    The query params.

Returns:

  • (String)

    The dumped URI query string.

Since:

  • 0.5.0



114
115
116
117
118
# File 'lib/uri/query_params/query_params.rb', line 114

def self.dump(query_params)
  query_params.map { |name,value|
    "#{name}=#{escape(value)}"
  }.join('&')
end

.escape(value) ⇒ String

Escapes a URI query param value.

Parameters:

  • value (Array, true, false, nil, #to_s)

    The query param value to escape.

Returns:

  • (String)

    The raw escaped query param value.

Since:

  • 0.7.1



80
81
82
83
84
85
86
87
# File 'lib/uri/query_params/query_params.rb', line 80

def self.escape(value)
  case value
  when Array      then URI::DEFAULT_PARSER.escape(value.join(' '),UNSAFE)
  when true       then String.new('active')
  when false, nil then String.new('')
  else                 URI::DEFAULT_PARSER.escape(value.to_s,UNSAFE)
  end
end

.parse(query_string) {|name, value| ... } ⇒ Hash{String => String}

Note:

Version 0.6.0 allows parse to yield the query params, in the order they are parsed.

Parses a URI query string.

Examples:

QueryParams.parse("x=1&y=2")
# => {"x"=>"1", "y"=>"2"}
QueryParams.parse("x=a%20b%20c&y")
# => {"x"=>"a b c", "y"=>""}

Parameters:

  • query_string (String)

    The URI query string.

Yields:

  • (name, value)

    The given block will be passed each parsed query param.

Yield Parameters:

  • name (String)

    The name of the query param.

  • value (String)

    The value of the query param.

Returns:

  • (Hash{String => String})

    The parsed query parameters.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/uri/query_params/query_params.rb', line 46

def self.parse(query_string)
  query_params = {}

  if query_string
    query_string.split('&').each do |param|
      # skip empty params
      next if param.empty?

      name, value = param.split('=',2)
      value = if value then URI::DEFAULT_PARSER.unescape(value)
              else          ''
              end

      yield(name,value) if block_given?
      query_params[name] = value
    end
  end

  return query_params
end