class Buffered
A body which buffers all it's contents.
Definitions
def self.wrap(object)
Tries to wrap an object in a class Protocol::HTTP::Body::Buffered
instance.
For compatibility, also accepts anything that behaves like an Array(String)
.
Signature
-
parameter
body
String | Array(String) | Readable | nil
the body to wrap.
-
returns
Readable | nil
the wrapped body or nil if nil was given.
Implementation
def self.wrap(object)
if object.is_a?(Readable)
return object
elsif object.is_a?(Array)
return self.new(object)
elsif object.is_a?(String)
return self.new([object])
elsif object
return self.read(object)
end
end
def self.read(body)
Read the entire body into a buffered representation.
Signature
-
parameter
body
Readable
the body to read.
-
returns
Buffered
the buffered body.
Implementation
def self.read(body)
chunks = []
body.each do |chunk|
chunks << chunk
end
self.new(chunks)
end
def buffered
A rewindable body wraps some other body. Convert it to a buffered body. The buffered body will share the same chunks as the rewindable body.
Signature
-
returns
Buffered
the buffered body.
Implementation
def buffered
self.class.new(@chunks)
end
def close(error = nil)
Ensure that future reads return nil, but allow for rewinding.
Implementation
def close(error = nil)
@index = @chunks.length
return nil
end
def ready?
A buffered response is always ready.
Implementation
def ready?
true
end