Class: Async::HTTP::Body::Buffered
- Defined in:
- lib/async/http/body/buffered.rb
Overview
A body which buffers all it’s contents.
Defined Under Namespace
Modules: Reader
Class Method Summary collapse
- .for(body) ⇒ Object
-
.wrap(body) ⇒ Object
Wraps an array into a buffered body.
Instance Method Summary collapse
- #bytesize ⇒ Object
- #close ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(chunks) ⇒ Buffered
constructor
A new instance of Buffered.
- #inspect ⇒ 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 @bytesize = 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
#bytesize ⇒ Object
56 57 58 |
# File 'lib/async/http/body/buffered.rb', line 56 def bytesize @bytesize ||= @chunks.inject(0) {|sum, chunk| sum + chunk.bytesize} end |
#close ⇒ Object
64 65 66 |
# File 'lib/async/http/body/buffered.rb', line 64 def close self end |
#each ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/async/http/body/buffered.rb', line 68 def each return to_enum unless block_given? while @index < @chunks.count yield @chunks[@index] @index += 1 end end |
#empty? ⇒ Boolean
60 61 62 |
# File 'lib/async/http/body/buffered.rb', line 60 def empty? @chunks.empty? end |
#inspect ⇒ Object
89 90 91 |
# File 'lib/async/http/body/buffered.rb', line 89 def inspect "\#<#{self.class} #{@chunks.count} chunks, #{self.bytesize} bytes>" end |
#read ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/async/http/body/buffered.rb', line 77 def read if chunk = @chunks[@index] @index += 1 end return chunk end |
#rewind ⇒ Object
85 86 87 |
# File 'lib/async/http/body/buffered.rb', line 85 def rewind @index = 0 end |