Skip to content

Commit 51d6eba

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

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-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

test/protocol/http/body/reader.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ def initialize(body)
2727
end
2828
end
2929

30+
with '#buffered!' do
31+
it 'buffers the body' do
32+
expect(reader.buffered!).to be == body
33+
end
34+
end
35+
3036
with '#close' do
3137
it 'closes the underlying body' do
3238
expect(body).to receive(:close)

0 commit comments

Comments
 (0)