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
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
- #The length of the response body if known.(lengthoftheresponsebody) ⇒ Object readonly
- #The number of chunks written to the body.(numberofchunkswrittentothebody.) ⇒ 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
#count ⇒ Object (readonly)
Returns the value of attribute count.
32 33 34 |
# File 'lib/protocol/http/body/writable.rb', line 32 def count @count end |
#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 |
#The number of chunks written to the body.(numberofchunkswrittentothebody.) ⇒ Object (readonly)
32 |
# File 'lib/protocol/http/body/writable.rb', line 32 attr :count |
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.
37 38 39 40 41 42 43 44 |
# File 'lib/protocol/http/body/writable.rb', line 37 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.
101 102 103 104 |
# File 'lib/protocol/http/body/writable.rb', line 101 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.
49 50 51 |
# File 'lib/protocol/http/body/writable.rb', line 49 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.
61 62 63 |
# File 'lib/protocol/http/body/writable.rb', line 61 def empty? @queue.empty? && @queue.closed? end |
#inspect ⇒ Object
Inspect the body.
170 171 172 173 174 175 176 |
# File 'lib/protocol/http/body/writable.rb', line 170 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.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/protocol/http/body/writable.rb', line 151 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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/protocol/http/body/writable.rb', line 69 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
54 55 56 |
# File 'lib/protocol/http/body/writable.rb', line 54 def ready? !@queue.empty? || @queue.closed? end |
#write(chunk) ⇒ Object
Write a single chunk to the body. Signal completion by calling #close_write.
89 90 91 92 93 94 95 96 |
# File 'lib/protocol/http/body/writable.rb', line 89 def write(chunk) if @queue.closed? raise(@error || Closed) end @queue.push(chunk) @count += 1 end |