class Athena::Routing::Response
- Athena::Routing::Response
- Reference
- Object
Overview
Represents an HTTP response that should be returned to the client.
Contains the content, status, and headers that should be applied to the actual HTTP::Server::Response.
This type is used to allow the content, status, and headers to be mutated by ART::Listeners before being returned to the client.
The content is stored in a proc that gets called when self is being written to the response IO.
How the output gets written can be customized via an ART::Response::Writer.
Direct Known Subclasses
Defined in:
response.crConstructors
-
.new(status : HTTP::Status | Int32 = HTTP::Status::OK, headers : HTTP::Headers = HTTP::Headers.new, &block : IO -> Nil)
Creates a new response with optional status, and headers arguments.
-
.new(content : String | Nil = nil, status : HTTP::Status | Int32 = HTTP::Status::OK, headers : HTTP::Headers = HTTP::Headers.new)
Creates a new response with optional content, status, and headers arguments.
-
.new(content_callback : Proc(IO, Nil), status : HTTP::Status | Int32 = HTTP::Status::OK, headers : HTTP::Headers = HTTP::Headers.new)
Creates a new response with the provided content_callback and optional status, and headers arguments.
Instance Method Summary
-
#content : String
Returns the content of
selfas aString. -
#content=(content_callback : Proc(IO, Nil))
Updates the content of
self. -
#content=(content : String | Nil = nil) : Nil
Updates the content of
self. -
#headers : HTTP::Headers
The response headers on
self. -
#status : HTTP::Status
The
HTTP::Statusofself. -
#status=(code : HTTP::Status | Int32) : Nil
The
HTTP::Statusofself. -
#write(output : IO) : Nil
Writes content of
selfto the provided output. - #writer=(writer : ART::Response::Writer)
Constructor Detail
Creates a new response with optional status, and headers arguments.
The block is captured and called when self is being written to the response IO.
This can be useful to reduce memory overhead when needing to return large responses.
require "athena"
class ExampleController < ART::Controller
@[ART::Get("/users")]
def users : ART::Response
ART::Response.new headers: HTTP::Headers{"content-type" => "application/json"} do |io|
User.all.to_json io
end
end
end
ART.run
# GET /users # => [{"id":1,...},...]
Creates a new response with optional content, status, and headers arguments.
A proc is created that will print the given content to the response IO.
Creates a new response with the provided content_callback and optional status, and headers arguments.
The proc is called when self is being written to the response IO.
Instance Method Detail
Returns the content of self as a String.
The content string is cached to avoid unnecessarily regenerating the same string multiple times.
The cached string is cleared when changing self's content via #content=.
Updates the content of self.
Resets the cached content string.
Updates the content of self.
Resets the cached content string.
Writes content of self to the provided output.
How the output gets written can be customized via an ART::Response::Writer.