Class: Protocol::Rack::Body::Enumerable
- Inherits:
-
HTTP::Body::Readable
- Object
- HTTP::Body::Readable
- Protocol::Rack::Body::Enumerable
- Defined in:
- lib/protocol/rack/body/enumerable.rb
Overview
Wraps the rack response body.
The `rack` body must respond to `each` and must only yield `String` values. If the body responds to `close`, it will be called after iteration.
Constant Summary collapse
- CONTENT_LENGTH =
'content-length'.freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
The rack response body.
-
#length ⇒ Object
readonly
The content length of the rack response body.
Class Method Summary collapse
-
.wrap(body, length = nil) ⇒ Object
Wraps an array into a buffered body.
Instance Method Summary collapse
- #call(stream) ⇒ Object
-
#close(error = nil) ⇒ Object
Close the response body.
-
#each(&block) ⇒ Object
Enumerate the response body.
-
#empty? ⇒ Boolean
Whether the body is empty.
-
#initialize(body, length) ⇒ Enumerable
constructor
Initialize the output wrapper.
- #inspect ⇒ Object
-
#read ⇒ Object
Read the next chunk from the response body.
-
#ready? ⇒ Boolean
Whether the body can be read immediately.
- #stream? ⇒ Boolean
Constructor Details
#initialize(body, length) ⇒ Enumerable
Initialize the output wrapper.
32 33 34 35 36 37 |
# File 'lib/protocol/rack/body/enumerable.rb', line 32 def initialize(body, length) @length = length @body = body @chunks = nil end |
Instance Attribute Details
#body ⇒ Object (readonly)
The rack response body.
40 41 42 |
# File 'lib/protocol/rack/body/enumerable.rb', line 40 def body @body end |
#length ⇒ Object (readonly)
The content length of the rack response body.
43 44 45 |
# File 'lib/protocol/rack/body/enumerable.rb', line 43 def length @length end |
Class Method Details
.wrap(body, length = nil) ⇒ Object
Wraps an array into a buffered body.
20 21 22 23 24 25 26 27 |
# File 'lib/protocol/rack/body/enumerable.rb', line 20 def self.wrap(body, length = nil) if body.is_a?(Array) length ||= body.sum(&:bytesize) return self.new(body, length) else return self.new(body, length) end end |
Instance Method Details
#call(stream) ⇒ Object
80 81 82 83 84 |
# File 'lib/protocol/rack/body/enumerable.rb', line 80 def call(stream) @body.call(stream) ensure self.close($!) end |
#close(error = nil) ⇒ Object
Close the response body.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/protocol/rack/body/enumerable.rb', line 56 def close(error = nil) if @body and @body.respond_to?(:close) @body.close end @body = nil @chunks = nil super end |
#each(&block) ⇒ Object
Enumerate the response body.
70 71 72 73 74 |
# File 'lib/protocol/rack/body/enumerable.rb', line 70 def each(&block) @body.each(&block) ensure self.close($!) end |
#empty? ⇒ Boolean
Whether the body is empty.
46 47 48 |
# File 'lib/protocol/rack/body/enumerable.rb', line 46 def empty? @length == 0 or (@body.respond_to?(:empty?) and @body.empty?) end |
#inspect ⇒ Object
96 97 98 |
# File 'lib/protocol/rack/body/enumerable.rb', line 96 def inspect "\#<#{self.class} length=#{@length.inspect} body=#{@body.class}>" end |
#read ⇒ Object
Read the next chunk from the response body.
88 89 90 91 92 93 94 |
# File 'lib/protocol/rack/body/enumerable.rb', line 88 def read @chunks ||= @body.to_enum(:each) return @chunks.next rescue StopIteration return nil end |
#ready? ⇒ Boolean
Whether the body can be read immediately.
51 52 53 |
# File 'lib/protocol/rack/body/enumerable.rb', line 51 def ready? body.is_a?(Array) or body.respond_to?(:to_ary) end |
#stream? ⇒ Boolean
76 77 78 |
# File 'lib/protocol/rack/body/enumerable.rb', line 76 def stream? !@body.respond_to?(:each) end |