Class: Async::HTTP::Body::Rewindable

Inherits:
Wrapper show all
Defined in:
lib/async/http/body/rewindable.rb

Overview

A body which buffers all it’s contents.

Instance Method Summary collapse

Methods inherited from Wrapper

#close, #empty?, #length, #stop

Methods inherited from Readable

#close, #each, #empty?, #join, #length, #stop

Constructor Details

#initialize(body) ⇒ Rewindable

Returns a new instance of Rewindable.



28
29
30
31
32
33
# File 'lib/async/http/body/rewindable.rb', line 28

def initialize(body)
	super(body)
	
	@chunks = []
	@index = 0
end

Instance Method Details

#inspectObject



53
54
55
# File 'lib/async/http/body/rewindable.rb', line 53

def inspect
	"\#<#{self.class} #{@index}/#{@chunks.count} chunks read>"
end

#readObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/async/http/body/rewindable.rb', line 35

def read
	if @index < @chunks.count
		chunk = @chunks[@index]
		@index += 1
	else
		if chunk = super
			@chunks << chunk
			@index += 1
		end
	end
	
	return chunk
end

#rewindObject



49
50
51
# File 'lib/async/http/body/rewindable.rb', line 49

def rewind
	@index = 0
end