This repository has been archived by the owner on Dec 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulate request bodies in their own object. This allows easy conversion to a string in cases where streaming isn't desired, or streaming as an IO-alike through #read and #readpartial This is a breaking change from the previous API. People who were using request.body will now need to use request.body.to_s. The previous callback-based API for reading the body is completely removed.
- Loading branch information
Showing
9 changed files
with
100 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module Reel | ||
# Represents the bodies of Requests | ||
class RequestBody | ||
def initialize(request) | ||
@request = request | ||
@streaming = nil | ||
@contents = nil | ||
end | ||
|
||
# Read exactly the given amount of data | ||
def read(length) | ||
stream! | ||
@request.read(length) | ||
end | ||
|
||
# Read up to length bytes, but return any data that's available | ||
def readpartial(length = nil) | ||
stream! | ||
@request.readpartial(length) | ||
end | ||
|
||
# Eagerly consume the entire body as a string | ||
def to_s | ||
return @contents if @contents | ||
raise StateError, "body is being streamed" unless @streaming.nil? | ||
|
||
begin | ||
@streaming = false | ||
@contents = "" | ||
while chunk = @request.readpartial | ||
@contents << chunk | ||
end | ||
rescue | ||
@contents = nil | ||
raise | ||
end | ||
|
||
@contents | ||
end | ||
|
||
# Assert that the body is actively being streamed | ||
def stream! | ||
raise StateError, "body has already been consumed" if @streaming == false | ||
@streaming = true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters