module Streamable
A body that invokes a block that can read and write to a stream.
In some cases, it's advantageous to directly read and write to the underlying stream if possible. For example, HTTP/1 upgrade requests, WebSockets, and similar. To handle that case, response bodies can implement stream?
and return true
. When stream?
returns true, the body should be consumed by calling call(stream)
. Server implementations may choose to always invoke call(stream)
if it's efficient to do so. Bodies that don't support it will fall back to using each
.
When invoking call(stream)
, the stream can be read from and written to, and closed. However, the stream is only guaranteed to be open for the duration of the call(stream)
call. Once the method returns, the stream should be closed by the server.
Nested
Definitions
def self.request(&block)
Generate a new streaming request body using the given block to generate the body.
Signature
-
parameter
block
Proc
The block that generates the body.
-
returns
RequestBody
The streaming request body.
Implementation
def self.request(&block)
RequestBody.new(block)
end
def self.response(request, &block)
Generate a new streaming response body using the given block to generate the body.
Signature
-
parameter
request
Request
The request.
-
parameter
block
Proc
The block that generates the body.
-
returns
ResponseBody
The streaming response body.
Implementation
def self.response(request, &block)
ResponseBody.new(block, request.body)
end