Class: Protocol::HTTP::Body::Writable
- Defined in:
- lib/protocol/http/body/writable.rb
Overview
A dynamic body which you can write to and read from.
Defined Under Namespace
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
- #The length of the response body if known.(lengthoftheresponsebody) ⇒ Object readonly
Instance Method Summary collapse
-
#close(error = nil) ⇒ Object
Stop generating output; cause the next call to write to fail with the given error.
-
#close_write(error = nil) ⇒ Object
Signal that no more data will be written to the body.
-
#closed? ⇒ Boolean
Whether the body is closed.
-
#empty? ⇒ Boolean
Indicates whether the body is empty.
-
#initialize(length = nil, queue: Thread::Queue.new) ⇒ Writable
constructor
Initialize the writable body.
-
#inspect ⇒ Object
Inspect the body.
-
#output ⇒ Object
Create an output wrapper which can be used to write chunks to the body.
-
#read ⇒ Object
Read the next available chunk.
- #ready? ⇒ Boolean
-
#write(chunk) ⇒ Object
Write a single chunk to the body.
Methods inherited from Readable
#as_json, #buffered, #call, #discard, #each, #finish, #join, #rewind, #rewindable?, #stream?, #to_json
Constructor Details
#initialize(length = nil, queue: Thread::Queue.new) ⇒ Writable
Initialize the writable body.
21 22 23 24 25 26 |
# File 'lib/protocol/http/body/writable.rb', line 21 def initialize(length = nil, queue: Thread::Queue.new) @length = length @queue = queue @count = 0 @error = nil end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
29 30 31 |
# File 'lib/protocol/http/body/writable.rb', line 29 def length @length end |
#The length of the response body if known.(lengthoftheresponsebody) ⇒ Object (readonly)
29 |
# File 'lib/protocol/http/body/writable.rb', line 29 attr :length |
Instance Method Details
#close(error = nil) ⇒ Object
Stop generating output; cause the next call to write to fail with the given error. Does not prevent existing chunks from being read. In other words, this indicates both that no more data will be or should be written to the body.
34 35 36 37 38 39 40 41 |
# File 'lib/protocol/http/body/writable.rb', line 34 def close(error = nil) @error ||= error @queue.clear @queue.close super end |
#close_write(error = nil) ⇒ Object
Signal that no more data will be written to the body.
98 99 100 101 |
# File 'lib/protocol/http/body/writable.rb', line 98 def close_write(error = nil) @error ||= error @queue.close end |
#closed? ⇒ Boolean
Whether the body is closed. A closed body can not be written to or read from.
46 47 48 |
# File 'lib/protocol/http/body/writable.rb', line 46 def closed? @queue.closed? end |
#empty? ⇒ Boolean
Indicates whether the body is empty. This can occur if the body has been closed, or if the producer has invoked #close_write and the reader has consumed all available chunks.
58 59 60 |
# File 'lib/protocol/http/body/writable.rb', line 58 def empty? @queue.empty? && @queue.closed? end |
#inspect ⇒ Object
Inspect the body.
167 168 169 170 171 172 173 |
# File 'lib/protocol/http/body/writable.rb', line 167 def inspect if @error "\#<#{self.class} #{@count} chunks written, #{status}, error=#{@error}>" else "\#<#{self.class} #{@count} chunks written, #{status}>" end end |
#output ⇒ Object
Create an output wrapper which can be used to write chunks to the body.
If a block is given, and the block raises an error, the error will used to close the body by invoking #close with the error.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/protocol/http/body/writable.rb', line 148 def output output = Output.new(self) unless block_given? return output end begin yield output rescue => error raise error ensure output.close(error) end end |
#read ⇒ Object
Read the next available chunk.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/protocol/http/body/writable.rb', line 66 def read if @error raise @error end # This operation may result in @error being set. chunk = @queue.pop if @error raise @error end return chunk end |
#ready? ⇒ Boolean
51 52 53 |
# File 'lib/protocol/http/body/writable.rb', line 51 def ready? !@queue.empty? || @queue.closed? end |
#write(chunk) ⇒ Object
Write a single chunk to the body. Signal completion by calling #close_write.
86 87 88 89 90 91 92 93 |
# File 'lib/protocol/http/body/writable.rb', line 86 def write(chunk) if @queue.closed? raise(@error || Closed) end @queue.push(chunk) @count += 1 end |