Class: Protocol::HTTP::Body::Rewindable

Inherits:
Wrapper show all
Defined in:
lib/protocol/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

#call, #close, #finish, #length, #stream?, wrap

Methods inherited from Readable

#call, #close, #each, #finish, #join, #length, #stream?

Constructor Details

#initialize(body) ⇒ Rewindable

Returns a new instance of Rewindable.


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

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

Instance Method Details

#bufferedObject

A rewindable body wraps some other body. Convert it to a buffered body


47
48
49
# File 'lib/protocol/http/body/rewindable.rb', line 47

def buffered
	Buffered.new(@chunks)
end

#empty?Boolean

Returns:

  • (Boolean)

38
39
40
# File 'lib/protocol/http/body/rewindable.rb', line 38

def empty?
	(@index >= @chunks.size) && super
end

#inspectObject


70
71
72
# File 'lib/protocol/http/body/rewindable.rb', line 70

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

#readObject


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/protocol/http/body/rewindable.rb', line 51

def read
	if @index < @chunks.size
		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

#ready?Boolean

Returns:

  • (Boolean)

42
43
44
# File 'lib/protocol/http/body/rewindable.rb', line 42

def ready?
	(@index < @chunks.size) || super
end

#rewindObject


66
67
68
# File 'lib/protocol/http/body/rewindable.rb', line 66

def rewind
	@index = 0
end