Class: Protocol::HTTP::Body::Completable

Inherits:
Wrapper show all
Defined in:
lib/protocol/http/body/completable.rb

Overview

Invokes a callback once the body has completed, either successfully or due to an error.

Instance Attribute Summary

Attributes inherited from Wrapper

#body

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Wrapper

#call, #empty?, #inspect, #length, #read, #ready?, #stream?

Methods inherited from Readable

#call, #each, #empty?, #join, #length, #read, #ready?, #stream?

Constructor Details

#initialize(body, callback) ⇒ Completable

Returns a new instance of Completable.



21
22
23
24
25
# File 'lib/protocol/http/body/completable.rb', line 21

def initialize(body, callback)
	super(body)
	
	@callback = callback
end

Class Method Details

.wrap(message, &block) ⇒ Object



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

def self.wrap(message, &block)
	if body = message&.body and !body.empty?
		message.body = self.new(message.body, block)
	else
		yield
	end
end

Instance Method Details

#close(error = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/protocol/http/body/completable.rb', line 39

def close(error = nil)
	if @body
		super
		
		@callback.call(error)
		
		@body = nil
	end
end

#finishObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/protocol/http/body/completable.rb', line 27

def finish
	if @body
		result = super
		
		@callback.call
		
		@body = nil
		
		return result
	end
end