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 as it is ‘#read`.

Instance Attribute Summary

Attributes inherited from Wrapper

#body

Instance Method Summary collapse

Methods inherited from Wrapper

#close, #empty?, #finish, #length

Methods inherited from Readable

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

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



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

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

#readObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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
	
	# We dup them on the way out, so that if someone modifies the string, it won't modify the rewindability.
	return chunk&.dup
end

#rewindObject



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

def rewind
	@index = 0
end