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 = [METHOD_GET, METHOD_POST, METHOD_PUT, METHOD_PATCH, METHOD_DELETE, METHOD_HEAD, METHOD_OPTIONS, METHOD_TRACE]

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, before_render(*names) before_render

Constructor Detail

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

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


[View source]
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 : ::HTTP::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 : ::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).


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


[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]

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


[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 : ::HTTP::Status | Int32 = 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 : ::HTTP::Status | Int32 = 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]