class Marten::Handlers::Base
- Marten::Handlers::Base
- Reference
- Object
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
- Marten::Core::DebugModeLoggable
- Marten::Handlers::Callbacks
- Marten::Handlers::ContentSecurityPolicy
- Marten::Handlers::Cookies
- Marten::Handlers::Flash
- Marten::Handlers::RequestForgeryProtection
- Marten::Handlers::Session
- Marten::Handlers::XFrameOptions
Extended Modules
- Marten::Handlers::ContentSecurityPolicy::ClassMethods
- Marten::Handlers::RequestForgeryProtection::ClassMethods
- Marten::Handlers::XFrameOptions::ClassMethods
Direct Known Subclasses
- Marten::Handlers::Defaults::Debug::PageNotFound
- Marten::Handlers::Defaults::Debug::ServerError
- Marten::Handlers::Defaults::Development::ServeAsset
- Marten::Handlers::Defaults::Development::ServeMediaFile
- Marten::Handlers::Redirect
- Marten::Handlers::Template
Defined in:
marten/handlers/base.crConstant Summary
-
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)
- .new(request : HTTP::Request, **kwargs)
Class Method Summary
- .content_security_policy_block
-
.http_method_names
Returns the HTTP method names that are allowed for the handler.
-
.http_method_names(*method_names : String | Symbol)
Allows to specify the allowed HTTP methods.
Instance Method Summary
-
#context
Returns the global template context.
-
#delete
Handles a
DELETE
HTTP request and returns aMarten::HTTP::Response
object. -
#dispatch : Marten::HTTP::Response
Triggers the execution of the handler in order to produce an HTTP response.
-
#get
Handles a
GET
HTTP request and returns aMarten::HTTP::Response
object. -
#head(status : ::HTTP::Status | Int32) : HTTP::Response
Returns an empty response associated with a given status code.
-
#head
Handles a
HEAD
HTTP request and returns aMarten::HTTP::Response
object. -
#json(raw_json : String, status : ::HTTP::Status | Int32 = 200)
Returns an HTTP response containing the passed raw JSON string.
-
#json(serializable, status : ::HTTP::Status | Int32 = 200)
Returns an HTTP response containing the passed object serialized as JSON.
-
#options
Handles an
OPTIONS
HTTP request and returns aMarten::HTTP::Response
object. -
#params : Marten::Routing::MatchParameters
Returns the associated route parameters.
-
#patch
Handles a
PATCH
HTTP request and returns aMarten::HTTP::Response
object. -
#post
Handles a
POST
HTTP request and returns aMarten::HTTP::Response
object. -
#put
Handles a
PUT
HTTP request and returns aMarten::HTTP::Response
object. -
#redirect(url : String, permanent = false)
Returns a redirect HTTP response for a specific
#url
. -
#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.
-
#request : Marten::HTTP::Request
Returns the associated HTTP request.
-
#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.
-
#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.
-
#response : Marten::HTTP::Response?
Returns the HTTP response.
-
#response!
Same as
#response
but with a nil-safety check. -
#reverse(*args, **options)
Reverses a URL - returns the URL corresponding to a specific route name and parameters.
-
#reverse(*args, **options, &)
Reverses a URL - returns the URL corresponding to a specific route name and parameters.
-
#trace
Handles a
TRACE
HTTP request and returns aMarten::HTTP::Response
object. -
#url(name : String | Symbol, **kwargs) : String
Alias for the
#reverse
method.
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
Macros inherited from module Marten::Core::DebugModeLoggable
debug_mode_debug_log(message)
debug_mode_debug_log,
debug_mode_info_log(message)
debug_mode_info_log
Constructor Detail
Class Method Detail
Allows to specify the allowed HTTP methods.
Instance Method Detail
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.
Handles a DELETE
HTTP request and returns a Marten::HTTP::Response
object.
The default implementation will return a 405 (not allowed) 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.
Handles a GET
HTTP request and returns a Marten::HTTP::Response
object.
The default implementation will return a 405 (not allowed) response.
Returns an empty response associated with a given status code.
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.
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).
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).
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.
Handles a PATCH
HTTP request and returns a Marten::HTTP::Response
object.
The default implementation will return a 405 (not allowed) response.
Handles a POST
HTTP request and returns a Marten::HTTP::Response
object.
The default implementation will return a 405 (not allowed) response.
Handles a PUT
HTTP request and returns a Marten::HTTP::Response
object.
The default implementation will return a 405 (not allowed) response.
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").
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.
Returns a streamed HTTP response generated from an iterator of strings, content type and status code.
Returns an HTTP response generated from a content string, content type and status code.
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.
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.
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.
Handles a TRACE
HTTP request and returns a Marten::HTTP::Response
object.
The default implementation will return a 405 (not allowed) response.