class InputWrapper
Wraps a Rack input object into a readable body.
This class provides a consistent interface for reading from Rack input streams,
which may be any IO-like object that responds to read
and close
.
Definitions
BLOCK_SIZE = 1024*4
The default block size for reading from the input stream.
def initialize(io, block_size: BLOCK_SIZE)
Initialize the input wrapper.
Signature
-
parameter
io
Object
The input object that responds to
read
andclose
.-
parameter
block_size
Integer
The size of chunks to read at a time.
Implementation
def initialize(io, block_size: BLOCK_SIZE)
@io = io
@block_size = block_size
super()
end
def close(error = nil)
Close the input stream.
If the input object responds to close
, it will be called.
Signature
-
parameter
error
Exception
Optional error that occurred during processing.
Implementation
def close(error = nil)
if @io
@io.close
@io = nil
end
end
def read
Read the next chunk from the input stream. Returns nil when there is no more data to read.
Signature
-
returns
String | Nil
The next chunk of data or nil if there is no more data.
Implementation
def read
@io&.read(@block_size)
end