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?, #stream?

Methods inherited from Readable

#call, #close, #each, #empty?, #finish, #join, #length, #ready?, #stream?

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

#digestObject


45
46
47
# File 'lib/protocol/http/body/digestable.rb', line 45

def digest
	@digest
end

#etagObject


49
50
51
# File 'lib/protocol/http/body/digestable.rb', line 49

def etag
	@digest.hexdigest.dump
end

#readObject


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