Class: HTTP::Response::Body
- Inherits:
-
Object
- Object
- HTTP::Response::Body
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/http/response/body.rb
Overview
A streamable response body, also easily converted into a string
Instance Attribute Summary collapse
-
#connection ⇒ HTTP::Connection
readonly
The connection object for the request.
-
#encoding ⇒ Encoding
readonly
The encoding used for the body content.
Instance Method Summary collapse
-
#each {|chunk| ... } ⇒ void
Iterate over the body, allowing it to be enumerable.
-
#initialize(stream, encoding: Encoding::BINARY) ⇒ Body
constructor
Create a new Body instance.
-
#inspect ⇒ String
Easier to interpret string inspect.
-
#loggable? ⇒ Boolean
Whether the body content is suitable for logging.
-
#readpartial ⇒ String
Read a chunk of the body.
-
#stream! ⇒ true
Assert that the body is actively being streamed.
-
#to_s ⇒ String
(also: #to_str)
Eagerly consume the entire body as a string.
Constructor Details
#initialize(stream, encoding: Encoding::BINARY) ⇒ Body
Create a new Body instance
42 43 44 45 46 47 48 |
# File 'lib/http/response/body.rb', line 42 def initialize(stream, encoding: Encoding::BINARY) @stream = stream @connection = stream.respond_to?(:connection) ? stream.connection : stream @streaming = nil @contents = nil @encoding = find_encoding(encoding) end |
Instance Attribute Details
#connection ⇒ HTTP::Connection (readonly)
The connection object for the request
22 23 24 |
# File 'lib/http/response/body.rb', line 22 def connection @connection end |
#encoding ⇒ Encoding (readonly)
The encoding used for the body content
31 32 33 |
# File 'lib/http/response/body.rb', line 31 def encoding @encoding end |
Instance Method Details
#each {|chunk| ... } ⇒ void
This method returns an undefined value.
Iterate over the body, allowing it to be enumerable
74 75 76 77 78 79 |
# File 'lib/http/response/body.rb', line 74 def each loop do yield readpartial end rescue EOFError # rubocop:disable Lint/SuppressedException end |
#inspect ⇒ String
Easier to interpret string inspect
139 140 141 |
# File 'lib/http/response/body.rb', line 139 def inspect "#<#{self.class}:#{object_id.to_s(16)} @streaming=#{!!@streaming}>" end |
#loggable? ⇒ Boolean
Whether the body content is suitable for logging
Returns true when the body encoding is not binary. Binary responses (images, audio, compressed data) produce unreadable log output.
128 129 130 |
# File 'lib/http/response/body.rb', line 128 def loggable? @encoding != Encoding::BINARY end |
#readpartial ⇒ String
Read a chunk of the body
(see HTTP::Client#readpartial)
59 60 61 62 63 |
# File 'lib/http/response/body.rb', line 59 def readpartial(*) stream! chunk = @stream.readpartial(*) String.new(chunk, encoding: @encoding) end |
#stream! ⇒ true
Assert that the body is actively being streamed
112 113 114 115 116 |
# File 'lib/http/response/body.rb', line 112 def stream! raise StateError, "body has already been consumed" if @streaming.eql?(false) @streaming = true end |
#to_s ⇒ String Also known as: to_str
Eagerly consume the entire body as a string
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/http/response/body.rb', line 88 def to_s return @contents if @contents raise StateError, "body is being streamed" unless @streaming.nil? begin @streaming = false @contents = read_contents rescue @contents = nil raise end @contents end |