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) ⇒ Object
Wraps an array into a buffered body.
Instance Method Summary collapse
- #close ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(chunks) ⇒ Buffered
constructor
A new instance of Buffered.
- #inspect ⇒ Object
- #length ⇒ Object
- #read ⇒ Object
- #rewind ⇒ Object
Methods inherited from Readable
Constructor Details
#initialize(chunks) ⇒ Buffered
Returns a new instance of Buffered.
49 50 51 52 53 54 |
# File 'lib/async/http/body/buffered.rb', line 49 def initialize(chunks) @chunks = chunks @length = nil @index = 0 end |
Class Method Details
.for(body) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/async/http/body/buffered.rb', line 39 def self.for(body) chunks = [] body.each do |chunk| chunks << chunk end self.new(chunks) end |
Instance Method Details
#close ⇒ Object
64 65 66 |
# File 'lib/async/http/body/buffered.rb', line 64 def close self end |
#empty? ⇒ Boolean
60 61 62 |
# File 'lib/async/http/body/buffered.rb', line 60 def empty? @index >= @chunks.length end |
#inspect ⇒ Object
82 83 84 |
# File 'lib/async/http/body/buffered.rb', line 82 def inspect "\#<#{self.class} #{@chunks.count} chunks, #{self.length} bytes>" end |
#length ⇒ Object
56 57 58 |
# File 'lib/async/http/body/buffered.rb', line 56 def length @length ||= @chunks.inject(0) {|sum, chunk| sum + chunk.bytesize} end |
#read ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/async/http/body/buffered.rb', line 68 def read if chunk = @chunks[@index] @index += 1 return chunk.dup end return nil end |
#rewind ⇒ Object
78 79 80 |
# File 'lib/async/http/body/buffered.rb', line 78 def rewind @index = 0 end |