class Protocol::HTTP::Body::Buffered

A body which buffers all it's contents.

Attributes

chunks[R]

Public Class Methods

for(body) click to toggle source
# File lib/protocol/http/body/buffered.rb, line 44
def self.for(body)
        chunks = []
        
        body.each do |chunk|
                chunks << chunk
        end
        
        self.new(chunks)
end
new(chunks = [], length = nil) click to toggle source
# File lib/protocol/http/body/buffered.rb, line 54
def initialize(chunks = [], length = nil)
        @chunks = chunks
        @length = length
        
        @index = 0
        @digest = nil
end
wrap(body) click to toggle source

Wraps an array into a buffered body. @return [Readable, nil] the wrapped body or nil if nil was given.

# File lib/protocol/http/body/buffered.rb, line 32
def self.wrap(body)
        if body.is_a?(Readable)
                return body
        elsif body.is_a?(Array)
                return self.new(body)
        elsif body.is_a?(String)
                return self.new([body])
        elsif body
                return self.for(body)
        end
end

Public Instance Methods

empty?() click to toggle source
# File lib/protocol/http/body/buffered.rb, line 72
def empty?
        @index >= @chunks.length
end
finish() click to toggle source
# File lib/protocol/http/body/buffered.rb, line 64
def finish
        self
end
inspect() click to toggle source
# File lib/protocol/http/body/buffered.rb, line 98
def inspect
        "\#<#{self.class} #{@chunks.size} chunks, #{self.length} bytes>"
end
length() click to toggle source
# File lib/protocol/http/body/buffered.rb, line 68
def length
        @length ||= @chunks.inject(0) {|sum, chunk| sum + chunk.bytesize}
end
read() click to toggle source
# File lib/protocol/http/body/buffered.rb, line 81
def read
        if chunk = @chunks[@index]
                @index += 1
                
                return chunk.dup
        end
end
ready?() click to toggle source

A buffered response is always ready.

# File lib/protocol/http/body/buffered.rb, line 77
def ready?
        true
end
rewind() click to toggle source
# File lib/protocol/http/body/buffered.rb, line 94
def rewind
        @index = 0
end
write(chunk) click to toggle source
# File lib/protocol/http/body/buffered.rb, line 89
def write(chunk)
        @digest&.update(chunk)
        @chunks << chunk
end