Class: Async::HTTP::Body::Rewindable
- Defined in:
- lib/async/http/body/rewindable.rb
Overview
A body which buffers all it’s contents as it is ‘#read`.
Instance Attribute Summary
Attributes inherited from Wrapper
Instance Method Summary collapse
-
#initialize(body) ⇒ Rewindable
constructor
A new instance of Rewindable.
- #inspect ⇒ Object
- #read ⇒ Object
- #rewind ⇒ Object
Methods inherited from Wrapper
#close, #empty?, #finish, #length
Methods inherited from Readable
#close, #each, #empty?, #finish, #join, #length
Constructor Details
#initialize(body) ⇒ Rewindable
Returns a new instance of Rewindable.
28 29 30 31 32 33 |
# File 'lib/async/http/body/rewindable.rb', line 28 def initialize(body) super(body) @chunks = [] @index = 0 end |
Instance Method Details
#inspect ⇒ Object
54 55 56 |
# File 'lib/async/http/body/rewindable.rb', line 54 def inspect "\#<#{self.class} #{@index}/#{@chunks.count} chunks read>" end |
#read ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/async/http/body/rewindable.rb', line 35 def read if @index < @chunks.count chunk = @chunks[@index] @index += 1 else if chunk = super @chunks << chunk @index += 1 end end # We dup them on the way out, so that if someone modifies the string, it won't modify the rewindability. return chunk&.dup end |
#rewind ⇒ Object
50 51 52 |
# File 'lib/async/http/body/rewindable.rb', line 50 def rewind @index = 0 end |