Class: Protocol::HTTP::Body::Rewindable
- Defined in:
- lib/protocol/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
-
#buffered ⇒ Object
A rewindable body wraps some other body.
-
#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.
31 32 33 34 35 36 |
# File 'lib/protocol/http/body/rewindable.rb', line 31 def initialize(body) super(body) @chunks = [] @index = 0 end |
Instance Method Details
#buffered ⇒ Object
A rewindable body wraps some other body. Convert it to a buffered body
39 40 41 |
# File 'lib/protocol/http/body/rewindable.rb', line 39 def buffered Buffered.new(@chunks) end |
#inspect ⇒ Object
62 63 64 |
# File 'lib/protocol/http/body/rewindable.rb', line 62 def inspect "\#<#{self.class} #{@index}/#{@chunks.size} chunks read>" end |
#read ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/protocol/http/body/rewindable.rb', line 43 def read if @index < @chunks.size 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
58 59 60 |
# File 'lib/protocol/http/body/rewindable.rb', line 58 def rewind @index = 0 end |