class Marten::Handlers::Base

Overview

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

Included Modules

Extended Modules

Direct Known Subclasses

Defined in:

marten/handlers/base.cr

Constant Summary

HTTP_METHOD_NAMES = ["get", "post", "put", "patch", "delete", "head", "options", "trace"] of ::String

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Marten::Handlers::Session

session(*args, **options)
session(*args, **options, &)
session

Instance methods inherited from module Marten::Handlers::RequestForgeryProtection

get_csrf_token get_csrf_token, referer_trusted? referer_trusted?

Instance methods inherited from module Marten::Handlers::Flash

flash(*args, **options)
flash(*args, **options, &)
flash

Instance methods inherited from module Marten::Handlers::Cookies

cookies(*args, **options)
cookies(*args, **options, &)
cookies

Macros inherited from module Marten::Handlers::Callbacks

after_dispatch(*names) after_dispatch, before_dispatch(*names) before_dispatch

Constructor Detail

def self.new(request : HTTP::Request, params : ParamsHash) #

[View source]
def self.new(request : HTTP::Request, **kwargs) #

[View source]

Class Method Detail

def self.content_security_policy_block #

def self.http_method_names #

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


[View source]
def self.http_method_names(*method_names : String | Symbol) #

Allows to specify the allowed HTTP methods.


[View source]

Instance Method Detail

def delete #

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

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


[View source]
def dispatch : Marten::HTTP::Response #

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.


[View source]
def get #

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

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


[View source]
def head(status : Int32) : HTTP::Response #

Returns an empty response associated with a given status code.


[View source]
def 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.


[View source]
def json(raw_json : String, status = 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).


[View source]
def json(serializable, status = 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).


[View source]
def 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.


[View source]
def params : Hash(String, Int16 | Int32 | Int64 | Int8 | String | UInt16 | UInt32 | UInt64 | UInt8 | UUID) #

Returns the associated route parameters.


[View source]
def patch #

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

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


[View source]
def post #

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

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


[View source]
def put #

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

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


[View source]
def 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").


[View source]
def render(template_name : String, context : Hash | NamedTuple | Nil | Marten::Template::Context = nil, content_type = HTTP::Response::DEFAULT_CONTENT_TYPE, status = 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.


[View source]
def request : Marten::HTTP::Request #

Returns the associated HTTP request.


[View source]
def respond(streamed_content : Iterator(String), content_type = HTTP::Response::DEFAULT_CONTENT_TYPE, status = 200) #

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


[View source]
def respond(content = "", content_type = HTTP::Response::DEFAULT_CONTENT_TYPE, status = 200) #

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


[View source]
def response : Marten::HTTP::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.


[View source]
def response! #

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


[View source]
def reverse(*args, **options) #

Convenient helper method to resolve a route name.


[View source]
def reverse(*args, **options, &) #

Convenient helper method to resolve a route name.


[View source]
def trace #

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

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


[View source]