class Bezel::Plug::Accepts

include Bezel::Plug

Chooses the response's format among formats (["html", "json"]) and sets Conn#format (:html): the _format parameter when there is one, else the request's Accept (its media ranges by their quality; */* or no Accept takes the first of formats). A request that accepts none of them, or names another _format, is Bezel::NotAcceptable (406).

The formats and their media types: html (text/html, and application/xhtml+xml when named exactly, so that application/* is no HTML), json (application/json), text (text/plain), xml (application/xml and text/xml), csv (text/csv).

req = HTTP::Server::Request.get("/", HTTP::Headers.of({"Accept" => "application/json, text/html;q=0.5"}))
Bezel::Plug::Accepts.new(["html", "json"]).call(Bezel::Conn.build(req)).format   # => :json

src/plug/accepts.qz:18

Constants

TYPES: Hash[String, Array[String]]

The media types of the formats known.

src/plug/accepts.qz:24

EXACT: Hash[String, Array[String]]

Media types a format takes only when the header names them exactly.

src/plug/accepts.qz:51

Class methods

def self.new(formats: Array[String]) -> Bezel::Plug::Accepts

The plug, accepting formats (preferred first).

src/plug/accepts.qz:30

Methods

def call(conn: Bezel::Conn) -> Bezel::Conn

The connection changed by this plug.

from Bezel::Plug

src/plug/accepts.qz:37