class Athena::Routing::Response

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

Constructors

Instance Method Summary

Constructor Detail

def self.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.

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,...},...]

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

A proc is created that will print the given content to the response IO.


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

The proc is called when self is being written to the response IO.


[View source]

Instance Method Detail

def content : String #

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


[View source]
def content=(content_callback : Proc(IO, Nil)) #

Updates the content of self.

Resets the cached content string.


[View source]
def content=(content : String | Nil = nil) : Nil #

Updates the content of self.

Resets the cached content string.


[View source]
def headers : HTTP::Headers #

The response headers on self.


[View source]
def status : HTTP::Status #

The HTTP::Status of self.


[View source]
def status=(code : HTTP::Status | Int32) : Nil #

The HTTP::Status of self.


[View source]
def write(output : IO) : Nil #

Writes content of self to the provided output.

How the output gets written can be customized via an ART::Response::Writer.


[View source]
def writer=(writer : ART::Response::Writer) #

[View source]