class HTTP::Response::Body
A streamable response body, also easily converted into a string
def each
def each while (chunk = readpartial) yield chunk end end
def find_encoding(encoding)
def find_encoding(encoding) Encoding.find encoding rescue ArgumentError Encoding::BINARY end
def initialize(stream, encoding: Encoding::BINARY)
def initialize(stream, encoding: Encoding::BINARY) @stream = stream @connection = stream.is_a?(Inflater) ? stream.connection : stream @streaming = nil @contents = nil @encoding = find_encoding(encoding) end
def inspect
def inspect "#<#{self.class}:#{object_id.to_s(16)} @streaming=#{!!@streaming}>" end
def readpartial(*args)
def readpartial(*args) stream! chunk = @stream.readpartial(*args) String.new(chunk, :encoding => @encoding) if chunk end
def stream!
def stream! raise StateError, "body has already been consumed" if @streaming == false @streaming = true end
def to_s
-
(String)
- eagerly consume the entire body as a string
def to_s return @contents if @contents raise StateError, "body is being streamed" unless @streaming.nil? begin @streaming = false @contents = String.new("", :encoding => @encoding) while (chunk = @stream.readpartial) @contents << String.new(chunk, :encoding => @encoding) chunk = nil # deallocate string end rescue @contents = nil raise end @contents end