From 6f89cc129bf64038bf1fae15fec44befff1ac99c Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Fri, 19 Jul 2024 12:28:16 +1200 Subject: [PATCH] Add a convenient `Reader#buffered!` to buffer the body. --- lib/protocol/http/body/reader.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/protocol/http/body/reader.rb b/lib/protocol/http/body/reader.rb index 8805ca2..c2a3f2b 100644 --- a/lib/protocol/http/body/reader.rb +++ b/lib/protocol/http/body/reader.rb @@ -10,7 +10,7 @@ module Body # General operations for interacting with a request or response body. module Reader # Read chunks from the body. - # @yield [String] read chunks from the body. + # @yields {|chunk| ...} chunks from the body. def each(&block) if @body @body.each(&block) @@ -19,7 +19,7 @@ def each(&block) end # Reads the entire request/response body. - # @return [String] the entire body as a string. + # @returns [String] the entire body as a string. def read if @body buffer = @body.join @@ -30,7 +30,7 @@ def read end # Gracefully finish reading the body. This will buffer the remainder of the body. - # @return [Buffered] buffers the entire body. + # @returns [Buffered] buffers the entire body. def finish if @body body = @body.finish @@ -40,6 +40,16 @@ def finish end end + # Buffer the entire request/response body. + # @returns [Readable] the buffered body. + def buffered! + if @body + @body = @body.finish + + return @body + end + end + # Write the body of the response to the given file path. def save(path, mode = ::File::WRONLY|::File::CREAT|::File::TRUNC, **options) if @body