class Athena::Routing::StreamedResponse

Overview

Represents an ART::Response whose content should be streamed to the client as opposed to being written all at once. This can be useful in cases where the response content is too large to fit into memory.

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.

Defined in:

streamed_response.cr

Constructors

Instance Method Summary

Instance methods inherited from class Athena::Routing::Response

content : String content, content=(content : String | Nil) content=, headers : HTTP::Headers headers, status : HTTP::Status status, status=(code : HTTP::Status | Int32) : Nil status=, write(output : IO) : Nil write, writer=(writer : ART::Response::Writer) writer=

Constructor methods inherited from class Athena::Routing::Response

new(status : HTTP::Status | Int32 = HTTP::Status::OK, headers : HTTP::Headers = HTTP::Headers.new, &block : IO -> Nil) : ART::StreamedResponse
new(content : String | Nil = nil, status : HTTP::Status | Int32 = HTTP::Status::OK, headers : HTTP::Headers = HTTP::Headers.new)
new

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's 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::StreamedResponse.new headers: HTTP::Headers{"content-type" => "application/json; charset=UTF-8"} do |io|
      User.all.to_json io
    end
  end
end

ART.run

# GET /users # => [{"id":1,...},...]

[View source]
def self.new(callback : Proc(IO, Nil), status : HTTP::Status | Int32 = HTTP::Status::OK, headers : HTTP::Headers = HTTP::Headers.new) #

Creates a new response with the provided callback and optional status, and headers arguments.

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


[View source]

Instance Method Detail

def content=(callback : Proc(IO, Nil)) #

Updates the callback of self.


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

Streams the data in the stored Proc to the provided output.

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


[View source]