Class: Reel::Request::Body
- Inherits:
-
Object
- Object
- Reel::Request::Body
- 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) ⇒ Body
constructor
A new instance of Body.
-
#inspect ⇒ Object
Easier to interpret string inspect.
-
#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.
-
#to_str ⇒ Object
(also: #to_s)
Eagerly consume the entire body as a string.
Constructor Details
#initialize(request) ⇒ Body
Returns a new instance of Body.
7 8 9 10 11 |
# File 'lib/reel/request/body.rb', line 7 def initialize(request) @request = request @streaming = nil @contents = nil end |
Instance Method Details
#each ⇒ Object
Iterate over the body, allowing it to be enumerable
26 27 28 29 30 |
# File 'lib/reel/request/body.rb', line 26 def each while chunk = readpartial yield chunk end end |
#inspect ⇒ Object
Easier to interpret string inspect
53 54 55 |
# File 'lib/reel/request/body.rb', line 53 def inspect "#<#{self.class}:#{object_id.to_s(16)} @streaming=#{!!@streaming}>" end |
#read(length) ⇒ Object
Read exactly the given amount of data
14 15 16 17 |
# File 'lib/reel/request/body.rb', line 14 def read(length) stream! @request.read(length) end |
#readpartial(length = nil) ⇒ Object
Read up to length bytes, but return any data that’s available
20 21 22 23 |
# File 'lib/reel/request/body.rb', line 20 def readpartial(length = nil) stream! @request.readpartial(length) end |
#to_str ⇒ Object Also known as: to_s
Eagerly consume the entire body as a string
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/reel/request/body.rb', line 33 def to_str 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 |