77module Protocol
88 module HTTP
99 module Body
10- # A generic base class for wrapping body instances. Typically you'd override `#read`.
11- # The implementation assumes a sequential unbuffered stream of data.
12- # def each -> yield(String | nil)
13- # def read -> String | nil
14- # def join -> String
15-
16- # def finish -> buffer the stream and close it.
17- # def close(error = nil) -> close the stream immediately.
18- # end
10+ # An interface for reading data from a body.
11+ #
12+ # Typically, you'd override `#read` to return chunks of data.
1913 class Readable
20- # The consumer can call stop to signal that the stream output has terminated .
14+ # Close the stream immediately .
2115 def close ( error = nil )
2216 end
2317
@@ -40,6 +34,7 @@ def length
4034 end
4135
4236 # Read the next available chunk.
37+ # @returns [String | Nil] The chunk of data, or `nil` if the stream has finished.
4338 def read
4439 nil
4540 end
@@ -60,12 +55,16 @@ def call(stream)
6055 end
6156
6257 # Read all remaining chunks into a buffered body and close the underlying input.
58+ # @returns [Buffered] The buffered body.
6359 def finish
6460 # Internally, this invokes `self.each` which then invokes `self.close`.
6561 Buffered . for ( self )
6662 end
6763
6864 # Enumerate all chunks until finished, then invoke `#close`.
65+ #
66+ # @yields {|chunk| ...} The block to call with each chunk of data.
67+ # @parameter chunk [String | Nil] The chunk of data, or `nil` if the stream has finished.
6968 def each
7069 return to_enum ( :each ) unless block_given?
7170
@@ -79,6 +78,8 @@ def each
7978 end
8079
8180 # Read all remaining chunks into a single binary string using `#each`.
81+ #
82+ # @returns [String | Nil] The binary string containing all chunks of data, or `nil` if the stream has finished (or did not contain any data).
8283 def join
8384 buffer = String . new . force_encoding ( Encoding ::BINARY )
8485
0 commit comments