7
7
module Protocol
8
8
module HTTP
9
9
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.
19
13
class Readable
20
- # The consumer can call stop to signal that the stream output has terminated .
14
+ # Close the stream immediately .
21
15
def close ( error = nil )
22
16
end
23
17
@@ -40,6 +34,7 @@ def length
40
34
end
41
35
42
36
# Read the next available chunk.
37
+ # @returns [String | Nil] The chunk of data, or `nil` if the stream has finished.
43
38
def read
44
39
nil
45
40
end
@@ -60,12 +55,16 @@ def call(stream)
60
55
end
61
56
62
57
# Read all remaining chunks into a buffered body and close the underlying input.
58
+ # @returns [Buffered] The buffered body.
63
59
def finish
64
60
# Internally, this invokes `self.each` which then invokes `self.close`.
65
61
Buffered . for ( self )
66
62
end
67
63
68
64
# 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.
69
68
def each
70
69
return to_enum ( :each ) unless block_given?
71
70
@@ -79,6 +78,8 @@ def each
79
78
end
80
79
81
80
# 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).
82
83
def join
83
84
buffer = String . new . force_encoding ( Encoding ::BINARY )
84
85
0 commit comments