Skip to content

Commit 6f89cc1

Browse files
committed
Add a convenient Reader#buffered! to buffer the body.
1 parent ebbeee3 commit 6f89cc1

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

lib/protocol/http/body/reader.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Body
1010
# General operations for interacting with a request or response body.
1111
module Reader
1212
# Read chunks from the body.
13-
# @yield [String] read chunks from the body.
13+
# @yields {|chunk| ...} chunks from the body.
1414
def each(&block)
1515
if @body
1616
@body.each(&block)
@@ -19,7 +19,7 @@ def each(&block)
1919
end
2020

2121
# Reads the entire request/response body.
22-
# @return [String] the entire body as a string.
22+
# @returns [String] the entire body as a string.
2323
def read
2424
if @body
2525
buffer = @body.join
@@ -30,7 +30,7 @@ def read
3030
end
3131

3232
# Gracefully finish reading the body. This will buffer the remainder of the body.
33-
# @return [Buffered] buffers the entire body.
33+
# @returns [Buffered] buffers the entire body.
3434
def finish
3535
if @body
3636
body = @body.finish
@@ -40,6 +40,16 @@ def finish
4040
end
4141
end
4242

43+
# Buffer the entire request/response body.
44+
# @returns [Readable] the buffered body.
45+
def buffered!
46+
if @body
47+
@body = @body.finish
48+
49+
return @body
50+
end
51+
end
52+
4353
# Write the body of the response to the given file path.
4454
def save(path, mode = ::File::WRONLY|::File::CREAT|::File::TRUNC, **options)
4555
if @body

0 commit comments

Comments
 (0)