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.



22
23
24
25
26
27
# File 'lib/protocol/http/body/digestable.rb', line 22

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



15
16
17
18
19
# File 'lib/protocol/http/body/digestable.rb', line 15

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



29
30
31
# File 'lib/protocol/http/body/digestable.rb', line 29

def digest
	@digest
end

#etag(weak: false) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/protocol/http/body/digestable.rb', line 33

def etag(weak: false)
	if weak
		"W/\"#{digest.hexdigest}\""
	else
		"\"#{digest.hexdigest}\""
	end
end

#readObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/protocol/http/body/digestable.rb', line 45

def read
	if chunk = super
		@digest.update(chunk)
		
		return chunk
	else
		@callback&.call(self)
		
		return nil
	end
end

#stream?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/protocol/http/body/digestable.rb', line 41

def stream?
	false
end