Class: Protocol::HTTP::Body::Buffered

Inherits:
Readable
  • Object
show all
Defined in:
lib/protocol/http/body/buffered.rb

Overview

A body which buffers all it’s contents.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Readable

#as_json, #call, #each, #join, #stream?, #to_json

Constructor Details

#initialize(chunks = [], length = nil) ⇒ Buffered

Initialize the buffered body with some chunks.



50
51
52
53
54
55
# File 'lib/protocol/http/body/buffered.rb', line 50

def initialize(chunks = [], length = nil)
	@chunks = chunks
	@length = length
	
	@index = 0
end

Instance Attribute Details

#chunksObject (readonly)

Returns the value of attribute chunks.



58
59
60
# File 'lib/protocol/http/body/buffered.rb', line 58

def chunks
  @chunks
end

#chunks the buffered chunks.(thebufferedchunks.) ⇒ Object (readonly)



58
# File 'lib/protocol/http/body/buffered.rb', line 58

attr :chunks

Class Method Details

.read(body) ⇒ Object

Read the entire body into a buffered representation.



36
37
38
39
40
41
42
43
44
# File 'lib/protocol/http/body/buffered.rb', line 36

def self.read(body)
	chunks = []
	
	body.each do |chunk|
		chunks << chunk
	end
	
	self.new(chunks)
end

.wrap(object) ⇒ Object

Tries to wrap an object in a Protocol::HTTP::Body::Buffered instance.

For compatibility, also accepts anything that behaves like an ‘Array(String)`.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/protocol/http/body/buffered.rb', line 20

def self.wrap(object)
	if object.is_a?(Readable)
		return object
	elsif object.is_a?(Array)
		return self.new(object)
	elsif object.is_a?(String)
		return self.new([object])
	elsif object
		return self.read(object)
	end
end

Instance Method Details

#bufferedObject

A rewindable body wraps some other body. Convert it to a buffered body. The buffered body will share the same chunks as the rewindable body.



63
64
65
# File 'lib/protocol/http/body/buffered.rb', line 63

def buffered
	self.class.new(@chunks)
end

#clearObject

Clear the buffered chunks.



84
85
86
87
88
# File 'lib/protocol/http/body/buffered.rb', line 84

def clear
	@chunks = []
	@length = 0
	@index = 0
end

#close(error = nil) ⇒ Object

Ensure that future reads return ‘nil`, but allow for rewinding.



77
78
79
80
81
# File 'lib/protocol/http/body/buffered.rb', line 77

def close(error = nil)
	@index = @chunks.length
	
	return nil
end

#close_write(error) ⇒ Object

Close the body for writing. This is a no-op.



131
132
133
# File 'lib/protocol/http/body/buffered.rb', line 131

def close_write(error)
	# Nothing to do.
end

#discardObject

Discard the body. Invokes #close.



120
121
122
123
# File 'lib/protocol/http/body/buffered.rb', line 120

def discard
	# It's safe to call close here because there is no underlying stream to close:
	self.close
end

#empty?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/protocol/http/body/buffered.rb', line 96

def empty?
	@index >= @chunks.length
end

#finishObject

Finish the body, this is a no-op.



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

def finish
	self
end

#inspectObject

Inspect the buffered body.



154
155
156
157
158
# File 'lib/protocol/http/body/buffered.rb', line 154

def inspect
	if @chunks
		"\#<#{self.class} #{@chunks.size} chunks, #{self.length} bytes>"
	end
end

#lengthObject

The length of the body. Will compute and cache the length of the body, if it was not provided.



91
92
93
# File 'lib/protocol/http/body/buffered.rb', line 91

def length
	@length ||= @chunks.inject(0) {|sum, chunk| sum + chunk.bytesize}
end

#readObject

Read the next chunk from the buffered body.



109
110
111
112
113
114
115
116
117
# File 'lib/protocol/http/body/buffered.rb', line 109

def read
	return nil unless @chunks
	
	if chunk = @chunks[@index]
		@index += 1
		
		return chunk.dup
	end
end

#ready?Boolean

Whether the body is ready to be read.

Returns:

  • (Boolean)


102
103
104
# File 'lib/protocol/http/body/buffered.rb', line 102

def ready?
	true
end

#rewindObject

Rewind the body to the beginning, causing a subsequent read to return the first chunk.



143
144
145
146
147
148
149
# File 'lib/protocol/http/body/buffered.rb', line 143

def rewind
	return false unless @chunks
	
	@index = 0
	
	return true
end

#rewindable?Boolean

Whether the body can be rewound.

Returns:

  • (Boolean)


138
139
140
# File 'lib/protocol/http/body/buffered.rb', line 138

def rewindable?
	@chunks != nil
end

#write(chunk) ⇒ Object

Write a chunk to the buffered body.



126
127
128
# File 'lib/protocol/http/body/buffered.rb', line 126

def write(chunk)
	@chunks << chunk
end