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.



38
39
40
41
42
# File 'lib/protocol/http/body/completable.rb', line 38

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

Class Method Details

.wrap(message, &block) ⇒ Object



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

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



56
57
58
59
60
61
62
63
64
# File 'lib/protocol/http/body/completable.rb', line 56

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

#finishObject



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

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