Class: HTTP::Response::IoBody
- Inherits:
-
Object
- Object
- HTTP::Response::IoBody
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/http/response/io_body.rb
Overview
A Body class that wraps an IO, rather than a the client object.
Instance Method Summary collapse
-
#each ⇒ Object
Iterate over the body, allowing it to be enumerable.
-
#inspect ⇒ Object
Easier to interpret string inspect.
-
#readpartial(size = HTTP::Connection::BUFFER_SIZE) ⇒ String?
body, or nil if whole body has already been read.
-
#stream! ⇒ Object
Assert that the body is actively being streamed.
-
#to_s ⇒ String
(also: #to_str)
Eagerly consume the entire body as a string.
Instance Method Details
#each ⇒ Object
Iterate over the body, allowing it to be enumerable
19 20 21 22 23 |
# File 'lib/http/response/io_body.rb', line 19 def each while (part = readpartial) yield part end end |
#inspect ⇒ Object
Easier to interpret string inspect
40 41 42 |
# File 'lib/http/response/io_body.rb', line 40 def inspect "#<#{self.class}:#{object_id.to_s(16)} @streaming=#{!!@streaming}>" end |
#readpartial(size = HTTP::Connection::BUFFER_SIZE) ⇒ String?
body, or nil if whole body has already been read.
11 12 13 14 15 16 |
# File 'lib/http/response/io_body.rb', line 11 def readpartial(size = HTTP::Connection::BUFFER_SIZE) stream! return nil if stream.eof? stream.readpartial(size) end |
#stream! ⇒ Object
Assert that the body is actively being streamed
34 35 36 37 |
# File 'lib/http/response/io_body.rb', line 34 def stream! fail StateError, "body has already been consumed" if @streaming == false @streaming = true end |
#to_s ⇒ String Also known as: to_str
Returns eagerly consume the entire body as a string.
26 27 28 |
# File 'lib/http/response/io_body.rb', line 26 def to_s @contents ||= readall end |