class

Marten::Handlers::Base

Inherits Marten::Handlers::XFrameOptions / Marten::Handlers::ContentSecurityPolicy / Marten::Handlers::Session / Marten::Handlers::RequestForgeryProtection / Marten::Handlers::Flash / Marten::Handlers::Cookies / Marten::Handlers::ExceptionHandling / Marten::Handlers::Callbacks / Marten::Core::DebugModeLoggable / Reference / Object

Base handler implementation.

This class defines the behaviour of a handler. A handler is initialized from an HTTP request and it is responsible for processing a request in order to produce an HTTP response (which can be an HTML content, a redirection, etc).

Constants

HTTP_METHOD_NAMES = [METHOD_GET, METHOD_POST, METHOD_PUT, METHOD_PATCH, METHOD_DELETE, METHOD_HEAD, METHOD_OPTIONS, METHOD_TRACE]

Constructors

new(request : HTTP::Request, params : Routing::MatchParameters)
Source
new(request : HTTP::Request, **kwargs)
Source

Class methods

content_security_policy_block
http_method_names

Returns the HTTP method names that are allowed for the handler.

Source
http_method_names(*method_names : String | Symbol)

Allows to specify the allowed HTTP methods.

Source

Instance methods

context

Returns the global template context.

This context object can be mutated for the lifetime of the handler in order to define which variables will be made available to the template runtime.

Source
delete

Handles a DELETE HTTP request and returns a Marten::HTTP::Response object.

The default implementation will return a 405 (not allowed) response.

Source
dispatch

Triggers the execution of the handler in order to produce an HTTP response.

This method will be called by the Marten server when it comes to produce an HTTP response once a handler has been identified for the considered route. This method will execute the handler method associated with the considered HTTP method (eg. #get for the GET method) in order to return the final HTTP response. A 405 response will be returned if the considered HTTP method is not allowed. The #dispatch method can also be overridden on a per-handler basis in order to implement any other arbitrary logics if necessary.

Source
get

Handles a GET HTTP request and returns a Marten::HTTP::Response object.

The default implementation will return a 405 (not allowed) response.

Source
head(status : ::HTTP::Status | Int32) : HTTP::Response

Returns an empty response associated with a given status code.

Source
head

Handles a HEAD HTTP request and returns a Marten::HTTP::Response object.

The default implementation is to return whatever is returned by the #get method.

Source
json(raw_json : String, status : ::HTTP::Status | Int32 = 200)

Returns an HTTP response containing the passed raw JSON string.

The response will use the application/json content type and the 200 status code (the latest can be set to something else through the use of the status argument).

Source
json(serializable, status : ::HTTP::Status | Int32 = 200)

Returns an HTTP response containing the passed object serialized as JSON.

The response will use the application/json content type and the 200 status code (the latest can be set to something else through the use of the status argument).

Source
options

Handles an OPTIONS HTTP request and returns a Marten::HTTP::Response object.

The default implementation will return an HTTP response that includes an Allow header populated from the configured allowed HTTP methods.

Source
params

Returns the associated route parameters.

Source
patch

Handles a PATCH HTTP request and returns a Marten::HTTP::Response object.

The default implementation will return a 405 (not allowed) response.

Source
post

Handles a POST HTTP request and returns a Marten::HTTP::Response object.

The default implementation will return a 405 (not allowed) response.

Source
put

Handles a PUT HTTP request and returns a Marten::HTTP::Response object.

The default implementation will return a 405 (not allowed) response.

Source
redirect(url : String, permanent = false)

Returns a redirect HTTP response for a specific url.

By default, the HTTP response returned will be a "302 Found", unless the permanent argument is set to true (in which case the response will be a "301 Moved Permanently").

Source
render(template_name : String, context : Hash | NamedTuple | Nil | Marten::Template::Context = nil, content_type = HTTP::Response::DEFAULT_CONTENT_TYPE, status : ::HTTP::Status | Int32 = 200)

Returns an HTTP response whose content is generated by rendering a specific template.

The context of the rendered template can be specified using the context argument, while the content type and status code of the response can be specified using the content_type and status arguments.

Source
request

Returns the associated HTTP request.

Source
respond(streamed_content : Iterator(String), content_type = HTTP::Response::DEFAULT_CONTENT_TYPE, status : ::HTTP::Status | Int32 = 200)

Returns a streamed HTTP response generated from an iterator of strings, content type and status code.

Source
respond(content = "", content_type = HTTP::Response::DEFAULT_CONTENT_TYPE, status : ::HTTP::Status | Int32 = 200)

Returns an HTTP response generated from a content string, content type and status code.

Source
response

Returns the HTTP response.

This method will return the Marten::HTTP::Response object that is returned by the #dispatch method, so that it can be used in the context of #after_dispatch callbacks.

Source
response!

Same as #response but with a nil-safety check.

Source
reverse(*args, **options)

Reverses a URL - returns the URL corresponding to a specific route name and parameters.

This method is a delegate to the Marten::Routing::Map#reverse method. It allows to reverse a URL from a route name and a set of parameters. If no route is found or if the arguments can't be applied to the route, a Marten::Routing::Errors::NoReverseMatch exception is raised.

Source
reverse(*args, **options, &)

Reverses a URL - returns the URL corresponding to a specific route name and parameters.

This method is a delegate to the Marten::Routing::Map#reverse method. It allows to reverse a URL from a route name and a set of parameters. If no route is found or if the arguments can't be applied to the route, a Marten::Routing::Errors::NoReverseMatch exception is raised.

Source
trace

Handles a TRACE HTTP request and returns a Marten::HTTP::Response object.

The default implementation will return a 405 (not allowed) response.

Source
url(name : String | Symbol, **kwargs) : String

Alias for the #reverse method.

Source