Class: Reel::RequestBody
- Inherits:
-
Object
- Object
- Reel::RequestBody
- Includes:
- Enumerable
- Defined in:
- lib/reel/request_body.rb
Overview
Represents the bodies of Requests
Instance Method Summary collapse
-
#each ⇒ Object
Iterate over the body, allowing it to be enumerable.
-
#initialize(request) ⇒ RequestBody
constructor
A new instance of RequestBody.
-
#read(length) ⇒ Object
Read exactly the given amount of data.
-
#readpartial(length = nil) ⇒ Object
Read up to length bytes, but return any data that’s available.
-
#stream! ⇒ Object
Assert that the body is actively being streamed.
-
#to_s ⇒ Object
Eagerly consume the entire body as a string.
Constructor Details
#initialize(request) ⇒ RequestBody
Returns a new instance of RequestBody.
6 7 8 9 10 |
# File 'lib/reel/request_body.rb', line 6 def initialize(request) @request = request @streaming = nil @contents = nil end |
Instance Method Details
#each ⇒ Object
Iterate over the body, allowing it to be enumerable
25 26 27 28 29 |
# File 'lib/reel/request_body.rb', line 25 def each while chunk = readpartial yield chunk end end |
#read(length) ⇒ Object
Read exactly the given amount of data
13 14 15 16 |
# File 'lib/reel/request_body.rb', line 13 def read(length) stream! @request.read(length) end |
#readpartial(length = nil) ⇒ Object
Read up to length bytes, but return any data that’s available
19 20 21 22 |
# File 'lib/reel/request_body.rb', line 19 def readpartial(length = nil) stream! @request.readpartial(length) end |
#stream! ⇒ Object
Assert that the body is actively being streamed
51 52 53 54 |
# File 'lib/reel/request_body.rb', line 51 def stream! raise StateError, "body has already been consumed" if @streaming == false @streaming = true end |
#to_s ⇒ Object
Eagerly consume the entire body as a string
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/reel/request_body.rb', line 32 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 |