Class: Protocol::HTTP::Body::Digestable
- Inherits:
-
Wrapper
show all
- Defined in:
- lib/protocol/http/body/digestable.rb
Overview
Invokes a callback once the body has finished reading.
Instance Attribute Summary
Attributes inherited from Wrapper
#body
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Wrapper
#call, #close, #empty?, #finish, #inspect, #length, #ready?
Methods inherited from Readable
#call, #close, #each, #empty?, #finish, #join, #length, #ready?
Constructor Details
#initialize(body, digest = Digest::SHA256.new, callback = nil) ⇒ Digestable
Returns a new instance of Digestable.
38
39
40
41
42
43
|
# File 'lib/protocol/http/body/digestable.rb', line 38
def initialize(body, digest = Digest::SHA256.new, callback = nil)
super(body)
@digest = digest
@callback = callback
end
|
Class Method Details
.wrap(message, digest = Digest::SHA256.new, &block) ⇒ Object
32
33
34
35
36
|
# File 'lib/protocol/http/body/digestable.rb', line 32
def self.wrap(message, digest = Digest::SHA256.new, &block)
if body = message&.body and !body.empty?
message.body = self.new(message.body, digest, block)
end
end
|
Instance Method Details
#digest ⇒ Object
45
46
47
|
# File 'lib/protocol/http/body/digestable.rb', line 45
def digest
@digest
end
|
#etag ⇒ Object
49
50
51
|
# File 'lib/protocol/http/body/digestable.rb', line 49
def etag
@digest.hexdigest.dump
end
|
#read ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/protocol/http/body/digestable.rb', line 57
def read
if chunk = super
@digest.update(chunk)
return chunk
else
@callback&.call(self)
return nil
end
end
|
#stream? ⇒ Boolean
53
54
55
|
# File 'lib/protocol/http/body/digestable.rb', line 53
def stream?
false
end
|