Class: Protocol::HTTP::Body::Digestable
- 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
Class Method Summary collapse
Instance Method Summary collapse
- #digest ⇒ Object
- #etag ⇒ Object
-
#initialize(body, digest = Digest::SHA256.new, callback = nil) ⇒ Digestable
constructor
A new instance of Digestable.
- #read ⇒ Object
Methods inherited from Wrapper
#close, #empty?, #finish, #inspect, #length
Methods inherited from Readable
#close, #each, #empty?, #finish, #join, #length
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(, digest = Digest::SHA256.new, &block) if body = &.body and !body.empty? .body = self.new(.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
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/protocol/http/body/digestable.rb', line 53 def read if chunk = super @digest.update(chunk) return chunk else @callback&.call(self) return nil end end |