Class: Protocol::HTTP::Body::Buffered
- Defined in:
- lib/protocol/http/body/buffered.rb
Overview
A body which buffers all it’s contents.
Instance Attribute Summary collapse
-
#chunks ⇒ Object
readonly
Returns the value of attribute chunks.
Class Method Summary collapse
- .for(body) ⇒ Object
-
.wrap(body) ⇒ Readable?
Wraps an array into a buffered body.
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #finish ⇒ Object
-
#initialize(chunks = [], length = nil) ⇒ Buffered
constructor
A new instance of Buffered.
- #inspect ⇒ Object
- #length ⇒ Object
- #read ⇒ Object
-
#ready? ⇒ Boolean
A buffered response is always ready.
- #rewind ⇒ Object
- #write(chunk) ⇒ Object
Methods inherited from Readable
Constructor Details
#initialize(chunks = [], length = nil) ⇒ Buffered
Returns a new instance of Buffered.
54 55 56 57 58 59 60 |
# File 'lib/protocol/http/body/buffered.rb', line 54 def initialize(chunks = [], length = nil) @chunks = chunks @length = length @index = 0 @digest = nil end |
Instance Attribute Details
#chunks ⇒ Object (readonly)
Returns the value of attribute chunks.
62 63 64 |
# File 'lib/protocol/http/body/buffered.rb', line 62 def chunks @chunks end |
Class Method Details
.for(body) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# 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 |
.wrap(body) ⇒ Readable?
Wraps an array into a buffered body.
32 33 34 35 36 37 38 39 40 41 42 |
# 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 |
Instance Method Details
#empty? ⇒ Boolean
72 73 74 |
# File 'lib/protocol/http/body/buffered.rb', line 72 def empty? @index >= @chunks.length end |
#finish ⇒ Object
64 65 66 |
# File 'lib/protocol/http/body/buffered.rb', line 64 def finish self end |
#inspect ⇒ Object
98 99 100 |
# File 'lib/protocol/http/body/buffered.rb', line 98 def inspect "\#<#{self.class} #{@chunks.size} chunks, #{self.length} bytes>" end |
#length ⇒ Object
68 69 70 |
# File 'lib/protocol/http/body/buffered.rb', line 68 def length @length ||= @chunks.inject(0) {|sum, chunk| sum + chunk.bytesize} end |
#read ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/protocol/http/body/buffered.rb', line 81 def read if chunk = @chunks[@index] @index += 1 return chunk.dup end end |
#ready? ⇒ Boolean
A buffered response is always ready.
77 78 79 |
# File 'lib/protocol/http/body/buffered.rb', line 77 def ready? true end |
#rewind ⇒ Object
94 95 96 |
# File 'lib/protocol/http/body/buffered.rb', line 94 def rewind @index = 0 end |
#write(chunk) ⇒ Object
89 90 91 92 |
# File 'lib/protocol/http/body/buffered.rb', line 89 def write(chunk) @digest&.update(chunk) @chunks << chunk end |