Class: Async::HTTP::Body::Buffered
- Defined in:
- lib/async/http/body/buffered.rb
Overview
A body which buffers all it’s contents.
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
- #rewind ⇒ Object
Methods inherited from Readable
Constructor Details
#initialize(chunks, length = nil) ⇒ Buffered
Returns a new instance of Buffered.
50 51 52 53 54 55 |
# File 'lib/async/http/body/buffered.rb', line 50 def initialize(chunks, length = nil) @chunks = chunks @length = length @index = 0 end |
Class Method Details
.for(body) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/async/http/body/buffered.rb', line 40 def self.for(body) chunks = [] body.each do |chunk| chunks << chunk end self.new(chunks) end |
Instance Method Details
#empty? ⇒ Boolean
65 66 67 |
# File 'lib/async/http/body/buffered.rb', line 65 def empty? @index >= @chunks.length end |
#finish ⇒ Object
57 58 59 |
# File 'lib/async/http/body/buffered.rb', line 57 def finish self end |
#inspect ⇒ Object
81 82 83 |
# File 'lib/async/http/body/buffered.rb', line 81 def inspect "\#<#{self.class} #{@chunks.count} chunks, #{self.length} bytes>" end |
#length ⇒ Object
61 62 63 |
# File 'lib/async/http/body/buffered.rb', line 61 def length @length ||= @chunks.inject(0) {|sum, chunk| sum + chunk.bytesize} end |
#read ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/async/http/body/buffered.rb', line 69 def read if chunk = @chunks[@index] @index += 1 end return chunk&.dup end |
#rewind ⇒ Object
77 78 79 |
# File 'lib/async/http/body/buffered.rb', line 77 def rewind @index = 0 end |