Class: HTTP::Response::StringBody
- Inherits:
-
Object
- Object
- HTTP::Response::StringBody
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/http/response/string_body.rb
Overview
A Body class that wraps a String, rather than a the client object.
Instance Method Summary collapse
-
#each {|@contents| ... } ⇒ Object
Iterate over the body, allowing it to be enumerable.
-
#inspect ⇒ Object
Easier to interpret string inspect.
-
#readpartial(size = @contents.length) ⇒ 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 {|@contents| ... } ⇒ Object
Iterate over the body, allowing it to be enumerable
21 22 23 |
# File 'lib/http/response/string_body.rb', line 21 def each yield @contents end |
#inspect ⇒ Object
Easier to interpret string inspect
40 41 42 |
# File 'lib/http/response/string_body.rb', line 40 def inspect "#<#{self.class}:#{object_id.to_s(16)}>" end |
#readpartial(size = @contents.length) ⇒ String?
body, or nil if whole body has already been read.
11 12 13 14 15 16 17 18 |
# File 'lib/http/response/string_body.rb', line 11 def readpartial(size = @contents.length) stream! return nil if @streaming_offset >= @contents.length @contents[@streaming_offset, size].tap do |part| @streaming_offset += (part.length + 1) end end |
#stream! ⇒ Object
Assert that the body is actively being streamed
34 35 36 37 |
# File 'lib/http/response/string_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/string_body.rb', line 26 def to_s @contents end |