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 read
Read the next chunk from the buffered body.
Signature
-
returns
String | Nil
the next chunk or nil if there are no more chunks.
Implementation
def read
return nil unless @chunks
if chunk = @chunks[@index]
@index += 1
return chunk.dup
end
end
def initialize(chunks = [], length = nil)
Initialize the buffered body with some chunks.
Signature
-
parameter
chunks
Array(String)
the chunks to buffer.
-
parameter
length
Integer
the length of the body, if known.
Implementation
def initialize(chunks = [], length = nil)
@chunks = chunks
@length = length
@index = 0
end
attr :chunks
Signature
-
attribute
Array(String)
chunks the buffered chunks.
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 finish
Finish the body, this is a no-op.
Signature
-
returns
Buffered
self.
Implementation
def finish
self
end
def close(error = nil)
Ensure that future reads return nil
, but allow for rewinding.
Signature
-
parameter
error
Exception | Nil
the error that caused the body to be closed, if any.
Implementation
def close(error = nil)
@index = @chunks.length
return nil
end
def clear
Clear the buffered chunks.
Implementation
def clear
@chunks = []
@length = 0
@index = 0
end
def length
The length of the body. Will compute and cache the length of the body, if it was not provided.
Implementation
def length
@length ||= @chunks.inject(0) {|sum, chunk| sum + chunk.bytesize}
end
def empty?
Signature
-
returns
Boolean
if the body is empty.
Implementation
def empty?
@index >= @chunks.length
end
def ready?
Whether the body is ready to be read.
Signature
-
returns
Boolean
a buffered response is always ready.
Implementation
def ready?
true
end
def discard
Discard the body. Invokes #close
.
Implementation
def discard
# It's safe to call close here because there is no underlying stream to close:
self.close
end
def write(chunk)
Write a chunk to the buffered body.
Implementation
def write(chunk)
@chunks << chunk
end
def close_write(error)
Close the body for writing. This is a no-op.
Implementation
def close_write(error)
# Nothing to do.
end
def rewindable?
Whether the body can be rewound.
Signature
-
returns
Boolean
if the body has chunks.
Implementation
def rewindable?
@chunks != nil
end
def rewind
Rewind the body to the beginning, causing a subsequent read to return the first chunk.
Implementation
def rewind
return false unless @chunks
@index = 0
return true
end
def inspect
Inspect the buffered body.
Signature
-
returns
String
a string representation of the buffered body.
Implementation
def inspect
if @chunks
"\#<#{self.class} #{@chunks.size} chunks, #{self.length} bytes>"
end
end