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.
- Loading branch information
Showing
10 changed files
with
106 additions
and
77 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,52 @@ | ||
require 'forwardable' | ||
|
||
module Reel | ||
# Represents the bodies of Requests | ||
# TODO: this is an initial attempt at adding a standardized body object | ||
# Much of the logic that belongs here is in Reel::Request and should be | ||
# extracted. | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,4 +77,4 @@ | |
|
||
raise ex if ex | ||
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