data Bezel::Conn
A connection: one request and the response being made for it, flowing
through the plugs of an endpoint (Bezel::Plug). A Conn is a value:
every change makes a new one (with, or the methods below), which a
plug returns. A plug that answers (send_resp and friends) and stops
the rest of the pipeline also calls halt.
Not Shareable: the request's body is read from its connection.
conn = Bezel::Conn.build(HTTP::Server::Request.get("/users?page=2"))
conn.params.int("page") # => 2
conn = conn.put_resp_header("x-frame-options", "DENY").text("hi")
conn.status # => 200
conn.to_response.headers["x-frame-options"] # => "DENY"
Class methods
def self.build(req: HTTP::Server::Request, app: Bezel::App = …) -> Bezel::Conn
A connection for req in app, its parameters those of the query
(Bezel::BadRequest when it does not decode).
def self.bare(req: HTTP::Server::Request, app: Bezel::App) -> Bezel::Conn
A connection for req in app, without parameters.
def self.status_of(error: Exception) -> Int
The status an exception is answered with: an HTTP::Error's own,
else 500.
Methods
def request -> HTTP::Server::Request
The request, as the plugs left it (Plug::MethodOverride changes its
method).
def params -> Bezel::Params
The parameters: query, body (once Plug::Parsers ran), path (once the
router matched), merged.
def body_params -> Bezel::Params
The body's parameters alone (Bezel::Plug::Parsers).
def assigns -> Hash[Symbol, Object]
Values the plugs and the action pass on (the current user, say).
def session -> Bezel::Session
The session (empty without the session plug).
def flash -> Bezel::Flash
The flash.
def status -> Int
The response's status (200 until set).
def resp_headers -> HTTP::Headers
The response's header fields.
def resp_body -> HTTP::Content?
The response's body, or nil while none is set.
def halted -> Bool
Whether a plug stopped the pipeline: the plugs after it do not run.
def request_id -> String?
The request's id (Bezel::Plug::RequestId), or nil.
def app -> Bezel::App
The application.
def started -> Duration
When the endpoint took the request (Time.monotonic).
def error -> StandardError?
The exception a plug raised, which stopped the pipeline
(Plug.run): the endpoint answers it with Bezel::ErrorPage.
def format -> Symbol?
The response's format Plug::Accepts chose (:html, :json), or
nil.
def to_h -> Hash[Symbol, Object]
The fields as a Hash, by name.
def with(*, request: HTTP::Server::Request = …, params: Bezel::Params = …, body_params: Bezel::Params = …, assigns: Hash[Symbol, Object] = …, session: Bezel::Session = …, flash: Bezel::Flash = …, status: Int = …, resp_headers: HTTP::Headers = …, resp_body: HTTP::Content? = …, halted: Bool = …, request_id: String? = …, app: Bezel::App = …, started: Duration = …, error: StandardError? = …, format: Symbol? = …) -> Bezel::Conn
A copy with the fields given replaced.
def method -> String
The request's method ("GET").
def path -> String
The request's path, percent-decoded ("/users/1").
def header(name: String) -> String?
The request's header name, or nil.
def config -> Bezel::Config
The configuration.
def csrf_token -> String?
The CSRF token forms of this request carry, or nil: the session's
CSRF secret (Plug::CSRF makes one), masked afresh at each call, so
that it follows a renew or drop in this request; else the
:csrf_token assign.
def url(path: String) -> String
path (a route helper's) as a URL of this request's scheme and host:
Conn#url(Router.user_path(id: 1)). The host is the request's Host header
(localhost without one), which the proxy in front sets: behind
none, a client chooses it, so a URL meant for elsewhere (an email's
link) should use a host of the configuration's instead.
A Host header that is not a host and an optional port (letters,
digits, ., -, :, [ and ]) is Bezel::BadRequest, so that it
cannot bring a path or a query of its own.
def repo -> Rotor::Repo
A repository over the application's database pool: Bezel::ConfigError
when the application has none (no DATABASE_URL). Cheap: the pool is
the application's, the repository a view of it.
def halted? -> Bool
Whether a plug stopped the pipeline.
def sent? -> Bool
Whether a response's body is set.
def final_status -> Int
The status the client gets: the error's (an HTTP::Error's own,
500 for another exception) when a plug raised, else 404 when no
plug answered, else status.
def halt -> Bezel::Conn
The connection with the pipeline stopped after this plug.
def assign(key: Symbol, value: Object) -> Bezel::Conn
The connection with value assigned to key.
def assign?(key: Symbol) -> Object?
The value assigned to key, or nil (conn.assign?(:user).as?(User)).
def assign(key: Symbol) -> Object
The value assigned to key: KeyError when none is.
def put_status(status: Int) -> Bezel::Conn
The connection with the status status.
def put_resp_header(name: String, value: String) -> Bezel::Conn
The connection with the response header name set to value
(replacing any value it had).
def add_resp_header(name: String, value: String) -> Bezel::Conn
The connection with the response header name added (Set-Cookie).
def delete_resp_header(name: String) -> Bezel::Conn
The connection without the response header name.
def put_session(session: Bezel::Session) -> Bezel::Conn
The connection with the session session.
def put_session(key: String, value: String) -> Bezel::Conn
The connection with the session's key set to value.
def put_flash(kind: Symbol, message: String) -> Bezel::Conn
The connection with message in the flash for the next request.
def merge_params(more: Bezel::Params) -> Bezel::Conn
The connection with params merged over its parameters.
def send_resp(status: Int, body: HTTP::Content, content_type: String? = …) -> Bezel::Conn
The connection answered with status and body (its content type
content_type unless one is set), and halted.
def text(body: String, status: Int = …) -> Bezel::Conn
The connection answered with the text body (text/plain), and
halted.
def html(page: Bezel::Html, status: Int = …) -> Bezel::Conn
The connection answered with the page page (text/html), and
halted.
def html(body: String, status: Int = …) -> Bezel::Conn
The connection answered with the HTML body (text/html), and
halted.
def json(value: JSON::Value, status: Int = …) -> Bezel::Conn
The connection answered with value as JSON (application/json),
and halted.
def redirect(to: String, status: Int = …) -> Bezel::Conn
The connection answered with a redirect to to (302 Found, or
status), and halted. A to that is a full URL must be one the
application chose: redirecting to a URL from the request is an open
redirect.
def to_response -> HTTP::Server::Response
The response: the status, the headers and the body (empty when none is set).