Class: Rack::QueryParser
- Inherits:
-
Object
- Object
- Rack::QueryParser
- Defined in:
- lib/rack/query_parser.rb
Defined Under Namespace
Classes: InvalidParameterError, ParameterTypeError, Params, ParamsTooDeepError
Constant Summary collapse
- DEFAULT_SEP =
/[&] */n
- COMMON_SEP =
{ ";" => /[;] */n, ";," => /[;,] */n, "&" => /[&] */n }
Instance Attribute Summary collapse
-
#param_depth_limit ⇒ Object
readonly
Returns the value of attribute param_depth_limit.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(params_class, param_depth_limit) ⇒ QueryParser
constructor
A new instance of QueryParser.
- #make_params ⇒ Object
- #new_depth_limit(param_depth_limit) ⇒ Object
-
#normalize_params(params, name, v, _depth = nil) ⇒ Object
normalize_params recursively expands parameters into structural types.
-
#parse_nested_query(qs, separator = nil) ⇒ Object
parse_nested_query expands a query string into structural types.
-
#parse_query(qs, separator = nil, &unescaper) ⇒ Object
Stolen from Mongrel, with some small modifications: Parses a query string by breaking it up at the '&'.
Constructor Details
#initialize(params_class, param_depth_limit) ⇒ QueryParser
Returns a new instance of QueryParser.
27 28 29 30 |
# File 'lib/rack/query_parser.rb', line 27 def initialize(params_class, param_depth_limit) @params_class = params_class @param_depth_limit = param_depth_limit end |
Instance Attribute Details
#param_depth_limit ⇒ Object (readonly)
Returns the value of attribute param_depth_limit.
25 26 27 |
# File 'lib/rack/query_parser.rb', line 25 def param_depth_limit @param_depth_limit end |
Class Method Details
.make_default(param_depth_limit) ⇒ Object
21 22 23 |
# File 'lib/rack/query_parser.rb', line 21 def self.make_default(param_depth_limit) new Params, param_depth_limit end |
Instance Method Details
#make_params ⇒ Object
159 160 161 |
# File 'lib/rack/query_parser.rb', line 159 def make_params @params_class.new end |
#new_depth_limit(param_depth_limit) ⇒ Object
163 164 165 |
# File 'lib/rack/query_parser.rb', line 163 def new_depth_limit(param_depth_limit) self.class.new @params_class, param_depth_limit end |
#normalize_params(params, name, v, _depth = nil) ⇒ Object
normalize_params recursively expands parameters into structural types. If the structural types represented by two different parameter names are in conflict, a ParameterTypeError is raised. The depth argument is deprecated and should no longer be used, it is kept for backwards compatibility with earlier versions of rack.
85 86 87 |
# File 'lib/rack/query_parser.rb', line 85 def normalize_params(params, name, v, _depth=nil) _normalize_params(params, name, v, 0) end |
#parse_nested_query(qs, separator = nil) ⇒ Object
parse_nested_query expands a query string into structural types. Supported types are Arrays, Hashes and basic value types. It is possible to supply query strings with parameters of conflicting types, in this case a ParameterTypeError is raised. Users are encouraged to return a 400 in this case.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rack/query_parser.rb', line 64 def parse_nested_query(qs, separator = nil) params = make_params unless qs.nil? || qs.empty? (qs || '').split(separator ? (COMMON_SEP[separator] || /[#{separator}] */n) : DEFAULT_SEP).each do |p| k, v = p.split('=', 2).map! { |s| unescape(s) } _normalize_params(params, k, v, 0) end end return params.to_h rescue ArgumentError => e raise InvalidParameterError, e., e.backtrace end |
#parse_query(qs, separator = nil, &unescaper) ⇒ Object
Stolen from Mongrel, with some small modifications: Parses a query string by breaking it up at the '&'. You can also use this to parse cookies by changing the characters used in the second parameter (which defaults to '&').
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rack/query_parser.rb', line 36 def parse_query(qs, separator = nil, &unescaper) unescaper ||= method(:unescape) params = make_params (qs || '').split(separator ? (COMMON_SEP[separator] || /[#{separator}] */n) : DEFAULT_SEP).each do |p| next if p.empty? k, v = p.split('=', 2).map!(&unescaper) if cur = params[k] if cur.class == Array params[k] << v else params[k] = [cur, v] end else params[k] = v end end return params.to_h end |