data Bezel::Params
A request's parameters, merged: the query's, then the body's (a form
or a JSON object, Bezel::Plug::Parsers), then the path's (the
router's), a later one replacing an earlier one of the same name.
Values are strings, lists and nested maps (Bezel::Param); the typed
readers raise Bezel::BadRequest (a 400) for a parameter missing or
malformed, so that an action reads what it needs in one call.
ps = Bezel::Params.from_query("id=12&user[email]=a%40b.c&tags[]=x&tags[]=y")
ps.int("id") # => 12
ps["user"].fetch("email") # => "a@b.c"
ps.array("tags") # => ["x", "y"]
ps.string?("missing") # => nil
Constants
MAX_DEPTH: Int
The deepest nesting a parameter's name may have (a[b][c] is 2):
deeper is Bezel::BadRequest.
Class methods
def self.empty -> Bezel::Params
No parameters.
def self.from_query(query: String) -> Bezel::Params
The parameters of a query string or an application/x-www-form-urlencoded
body, nested as Rack nests them: a[b]=1 is a map, a[]=1&a[]=2 a
list, a[][b]=1&a[][c]=2 a list of one map (a new map begins when a
name repeats in the last one). A plain name keeps its last value,
replacing a list or map given before (a[]=1&a=2 is "2"), and a
pair without a name (=1) is ignored. Nesting under a name that holds
another kind of value (a=1&a[]=2, a[]=1&a[b]=2), a list of lists
(a[][]=1, which Rack reads otherwise), a nesting deeper than
MAX_DEPTH or a value that is not UTF-8 once decoded is
Bezel::BadRequest.
def self.from_json(value: JSON::Value) -> Bezel::Params
The parameters of a JSON object; anything else is under "_json".
Strings are kept, null is left out, other scalars are written as
JSON writes them (12, true).
def self.of(h: Hash[String, String]) -> Bezel::Params
Parameters from a hash of strings (the router's path parameters).
Methods
def entries -> Hash[String, Bezel::Param]
def with(*, entries: Hash[String, Bezel::Param] = …) -> Bezel::Params
A copy with the fields given replaced.
def merge(other: Bezel::Params) -> Bezel::Params
These with other's on top: a name in both takes other's value,
except that two maps are merged, as nested forms build them.
def key?(key: String) -> Bool
Whether there is a parameter key.
def keys -> Array[String]
The names, in order.
def size -> Int
The number of parameters.
def empty? -> Bool
Whether there are none.
def param?(key: String) -> Bezel::Param?
The raw value of key, or nil.
def [](key: String) -> Bezel::Params
The nested parameters under key (params["user"] for user[email]):
Bezel::BadRequest when key is missing or not a map.
def []?(key: String) -> Bezel::Params?
The nested parameters under key, or nil when it is missing or not a
map.
def fetch(key: String) -> String
The string key: Bezel::BadRequest when it is missing or not a
string.
def fetch(key: String, default: String) -> String
The string key, or default when it is missing.
def string?(key: String) -> String?
The string key, or nil when it is missing or not a string.
def int(key: String) -> Int
The integer key: Bezel::BadRequest when it is missing or not an
integer.
def int?(key: String) -> Int?
The integer key, nil when it is missing or empty;
Bezel::BadRequest when it is not an integer.
def float(key: String) -> Float
The number key: Bezel::BadRequest when it is missing or not a
number.
def float?(key: String) -> Float?
The number key, nil when it is missing or empty; Bezel::BadRequest
when it is not a number.
def bool(key: String) -> Bool
The boolean key: true, 1, on and yes are true, false, 0,
off, no and the empty string false, a missing one false (as an
unchecked checkbox sends nothing); anything else is
Bezel::BadRequest.
def array(key: String) -> Array[String]
The strings of the list key (a single string is a list of one, a
missing key an empty list): Bezel::BadRequest when an element is not
a string.
def to_h -> Hash[String, Object?]
The parameters as plain values (String, Array, Hash), the shape
Rotor::Changeset.cast takes.