Class: Protocol::HTTP::Body::Rewindable
- Inherits:
-
Wrapper
show all
- 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
#body
Instance Method Summary
collapse
Methods inherited from Wrapper
#close, #finish, #length
Methods inherited from Readable
#close, #each, #finish, #join, #length
Constructor Details
Returns a new instance of Rewindable.
31
32
33
34
35
36
37
|
# File 'lib/protocol/http/body/rewindable.rb', line 31
def initialize(body)
super(body)
@chunks = []
@index = 0
@digest = nil
end
|
Instance Method Details
#buffered ⇒ Object
A rewindable body wraps some other body. Convert it to a buffered body
53
54
55
|
# File 'lib/protocol/http/body/rewindable.rb', line 53
def buffered
Buffered.new(@chunks)
end
|
#digest ⇒ Object
39
40
41
42
43
44
45
46
|
# File 'lib/protocol/http/body/rewindable.rb', line 39
def digest
if @digest.nil?
@digest = Digest::SHA256.new
@chunks.each{|chunk| @digest.update(chunk)}
end
@digest.hexdigest
end
|
#empty? ⇒ Boolean
48
49
50
|
# File 'lib/protocol/http/body/rewindable.rb', line 48
def empty?
(@index >= @chunks.size) && super
end
|
#inspect ⇒ Object
77
78
79
|
# File 'lib/protocol/http/body/rewindable.rb', line 77
def inspect
"\#<#{self.class} #{@index}/#{@chunks.size} chunks read>"
end
|
#read ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/protocol/http/body/rewindable.rb', line 57
def read
if @index < @chunks.size
chunk = @chunks[@index]
@index += 1
else
if chunk = super
@chunks << chunk
@digest&.update(chunk)
@index += 1
end
end
return chunk&.dup
end
|
#rewind ⇒ Object
73
74
75
|
# File 'lib/protocol/http/body/rewindable.rb', line 73
def rewind
@index = 0
end
|