Class: Protocol::HTTP::Body::Buffered
- Inherits:
-
Readable
- Object
- Readable
- Protocol::HTTP::Body::Buffered
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
#call, #close, #each, #join, #stream?
Constructor Details
#initialize(chunks = [], length = nil) ⇒ Buffered
Returns a new instance of Buffered.
38
39
40
41
42
43
|
# File 'lib/protocol/http/body/buffered.rb', line 38
def initialize(chunks = [], length = nil)
@chunks = chunks
@length = length
@index = 0
end
|
Instance Attribute Details
#chunks ⇒ Object
Returns the value of attribute chunks.
45
46
47
|
# File 'lib/protocol/http/body/buffered.rb', line 45
def chunks
@chunks
end
|
Class Method Details
.for(body) ⇒ Object
28
29
30
31
32
33
34
35
36
|
# File 'lib/protocol/http/body/buffered.rb', line 28
def self.for(body)
chunks = []
body.each do |chunk|
chunks << chunk
end
self.new(chunks)
end
|
Wraps an array into a buffered body.
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/protocol/http/body/buffered.rb', line 16
def self.wrap(body)
if body.is_a?(Readable)
return body
elsif body.is_a?(Array)
return self.new(body)
elsif body.is_a?(String)
return self.new([body])
elsif body
return self.for(body)
end
end
|
Instance Method Details
#empty? ⇒ Boolean
55
56
57
|
# File 'lib/protocol/http/body/buffered.rb', line 55
def empty?
@index >= @chunks.length
end
|
#finish ⇒ Object
47
48
49
|
# File 'lib/protocol/http/body/buffered.rb', line 47
def finish
self
end
|
#inspect ⇒ Object
80
81
82
|
# File 'lib/protocol/http/body/buffered.rb', line 80
def inspect
"\#<#{self.class} #{@chunks.size} chunks, #{self.length} bytes>"
end
|
#length ⇒ Object
51
52
53
|
# File 'lib/protocol/http/body/buffered.rb', line 51
def length
@length ||= @chunks.inject(0) {|sum, chunk| sum + chunk.bytesize}
end
|
#read ⇒ Object
64
65
66
67
68
69
70
|
# File 'lib/protocol/http/body/buffered.rb', line 64
def read
if chunk = @chunks[@index]
@index += 1
return chunk.dup
end
end
|
#ready? ⇒ Boolean
A buffered response is always ready.
60
61
62
|
# File 'lib/protocol/http/body/buffered.rb', line 60
def ready?
true
end
|
#rewind ⇒ Object
76
77
78
|
# File 'lib/protocol/http/body/buffered.rb', line 76
def rewind
@index = 0
end
|
#write(chunk) ⇒ Object
72
73
74
|
# File 'lib/protocol/http/body/buffered.rb', line 72
def write(chunk)
@chunks << chunk
end
|