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

src/params.qz:37

Constants

MAX_DEPTH: Int

The deepest nesting a parameter's name may have (a[b][c] is 2): deeper is Bezel::BadRequest.

src/params.qz:43

Class methods

def self.empty -> Bezel::Params

No parameters.

src/params.qz:39

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.

src/params.qz:55

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).

src/params.qz:74

def self.of(h: Hash[String, String]) -> Bezel::Params

Parameters from a hash of strings (the router's path parameters).

src/params.qz:104

Methods

def entries -> Hash[String, Bezel::Param]

src/params.qz:37

def with(*, entries: Hash[String, Bezel::Param] = …) -> Bezel::Params

A copy with the fields given replaced.

src/params.qz:39

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.

src/params.qz:110

def key?(key: String) -> Bool

Whether there is a parameter key.

src/params.qz:124

def keys -> Array[String]

The names, in order.

src/params.qz:127

def size -> Int

The number of parameters.

src/params.qz:130

def empty? -> Bool

Whether there are none.

src/params.qz:133

def param?(key: String) -> Bezel::Param?

The raw value of key, or nil.

src/params.qz:136

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.

src/params.qz:140

def []?(key: String) -> Bezel::Params?

The nested parameters under key, or nil when it is missing or not a map.

src/params.qz:146

def fetch(key: String) -> String

The string key: Bezel::BadRequest when it is missing or not a string.

src/params.qz:155

def fetch(key: String, default: String) -> String

The string key, or default when it is missing.

src/params.qz:160

def string?(key: String) -> String?

The string key, or nil when it is missing or not a string.

src/params.qz:163

def int(key: String) -> Int

The integer key: Bezel::BadRequest when it is missing or not an integer.

src/params.qz:172

def int?(key: String) -> Int?

The integer key, nil when it is missing or empty; Bezel::BadRequest when it is not an integer.

src/params.qz:178

def float(key: String) -> Float

The number key: Bezel::BadRequest when it is missing or not a number.

src/params.qz:191

def float?(key: String) -> Float?

The number key, nil when it is missing or empty; Bezel::BadRequest when it is not a number.

src/params.qz:197

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.

src/params.qz:208

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.

src/params.qz:221

def to_h -> Hash[String, Object?]

The parameters as plain values (String, Array, Hash), the shape Rotor::Changeset.cast takes.

src/params.qz:238