Skip to content

Commit

Permalink
Add a convenient Reader#buffered! to buffer the body.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 19, 2024
1 parent ebbeee3 commit 6f89cc1
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/protocol/http/body/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6f89cc1

Please sign in to comment.