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.
49 50 51 52 53 54 |
# File 'lib/protocol/rack/body/enumerable.rb', line 49 def initialize(body, length) @length = length @body = body @chunks = nil end |
Instance Attribute Details
#body ⇒ Object (readonly)
The rack response body.
57 58 59 |
# File 'lib/protocol/rack/body/enumerable.rb', line 57 def body @body end |
#length ⇒ Object (readonly)
The content length of the rack response body.
60 61 62 |
# File 'lib/protocol/rack/body/enumerable.rb', line 60 def length @length end |
Class Method Details
.wrap(body, length = nil) ⇒ Object
Wraps an array into a buffered body.
37 38 39 40 41 42 43 44 |
# File 'lib/protocol/rack/body/enumerable.rb', line 37 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
97 98 99 100 101 |
# File 'lib/protocol/rack/body/enumerable.rb', line 97 def call(stream) @body.call(stream) ensure self.close($!) end |
#close(error = nil) ⇒ Object
Close the response body.
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/protocol/rack/body/enumerable.rb', line 73 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.
87 88 89 90 91 |
# File 'lib/protocol/rack/body/enumerable.rb', line 87 def each(&block) @body.each(&block) ensure self.close($!) end |
#empty? ⇒ Boolean
Whether the body is empty.
63 64 65 |
# File 'lib/protocol/rack/body/enumerable.rb', line 63 def empty? @length == 0 or (@body.respond_to?(:empty?) and @body.empty?) end |
#inspect ⇒ Object
113 114 115 |
# File 'lib/protocol/rack/body/enumerable.rb', line 113 def inspect "\#<#{self.class} length=#{@length.inspect} body=#{@body.class}>" end |
#read ⇒ Object
Read the next chunk from the response body.
105 106 107 108 109 110 111 |
# File 'lib/protocol/rack/body/enumerable.rb', line 105 def read @chunks ||= @body.to_enum(:each) return @chunks.next rescue StopIteration return nil end |
#ready? ⇒ Boolean
Whether the body can be read immediately.
68 69 70 |
# File 'lib/protocol/rack/body/enumerable.rb', line 68 def ready? body.is_a?(Array) or body.respond_to?(:to_ary) end |
#stream? ⇒ Boolean
93 94 95 |
# File 'lib/protocol/rack/body/enumerable.rb', line 93 def stream? !@body.respond_to?(:each) end |