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

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

Overview

A body which buffers all its 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.



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

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

Instance Attribute Details

#chunksObject (readonly)

Returns the value of attribute chunks.



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

def chunks
  @chunks
end

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



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

attr :chunks

Class Method Details

.read(body) ⇒ Object

Read the entire body into a buffered representation.



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

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)`.



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

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.



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

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

#clearObject

Clear the buffered chunks.



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

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

#close(error = nil) ⇒ Object

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



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

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

#close_write(error) ⇒ Object

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



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

def close_write(error)
	# Nothing to do.
end

#discardObject

Discard the body. Invokes #close.



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

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

#finishObject

Finish the body, this is a no-op.



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

def finish
	self
end

#inspectObject

Inspect the buffered body.



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

def inspect
	if @chunks and @chunks.size > 0
		"#<#{self.class} #{@index}/#{@chunks.size} chunks, #{self.length} bytes>"
	else
		"#<#{self.class} empty>"
	end
end

#lengthObject

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



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

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

#readObject

Read the next chunk from the buffered body.



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

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)


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

def ready?
	true
end

#rewindObject

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



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

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

#rewindable?Boolean

Whether the body can be rewound.

Returns:

  • (Boolean)


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

def rewindable?
	@chunks != nil
end

#write(chunk) ⇒ Object

Write a chunk to the buffered body.



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

def write(chunk)
	@chunks << chunk
end