class Responsible::Response
- Responsible::Response
- Reference
- Object
Overview
Wrapper object for lifted responses. Provides extensions need for defining locally scoped error behaviors and parse tools for extraction of returned data.
Defined in:
responsible/response.crConstructors
-
.new(response : Responsible::ResponseInterface)
Creates a new
Response
by wrapping a supported response type.
Instance Method Summary
-
#>>(x : T.class) : T forall T
Parses the contents of this response to the type x.
-
#on_client_error(& : Action) : self
Execute the passed block if this is a client_error response.
-
#on_informational(& : Action) : self
Execute the passed block if this is a informational response.
-
#on_redirection(& : Action) : self
Execute the passed block if this is a redirection response.
-
#on_server_error(& : Action) : self
Execute the passed block if this is a server_error response.
-
#on_success(& : Action) : self
Execute the passed block if this is a success response.
-
#parse_to(x : T.class, ignore_response_code = @in_handler, & : Exception -> U) : T | U forall T, U
Reads the contents of the body into the specified type x.
-
#parse_to(x : T.class, ignore_response_code = @in_handler) : T forall T
Parses the contents of this response to the type x, or raises an
Responsible::Error
if this is not possible. -
#parse_to?(x : T.class, ignore_response_code = @in_handler) : T | Nil forall T
Parses the contents of this response to the type x, or nil if this is not possible.
Macro Summary
Constructor Detail
Creates a new Response
by wrapping a supported response type. response
must by a type that includes ResponsibleInterface
. This can be inserted
manually, or by using the Responsible.support
macro.
Instance Method Detail
Parses the contents of this response to the type x.
If x is nilable, a parser error will result in nil
being returned,
otherwise a Responsible::Error
will be raised if parsing is not possible.
Execute the passed block if this is a client_error response.
Execute the passed block if this is a informational response.
Execute the passed block if this is a redirection response.
Execute the passed block if this is a server_error response.
Execute the passed block if this is a success response.
Reads the contents of the body into the specified type x.
If the response cannot be parsed, the block will be yielded to to provide an alternative parser.
T.from_json
must exist and be capable of deserializing a JSON string.
This works out-of-the-box for most types where attributes map directly. The
JSON::Serializable
module provides tools for more complex parsing.
Parses the contents of this response to the type x, or raises an
Responsible::Error
if this is not possible.
Parses the contents of this response to the type x, or nil if this is not possible.