class HTTP::Response::IoBody

object.
A Body class that wraps an IO, rather than a the client

def each

Iterate over the body, allowing it to be enumerable
def each
  while (part = readpartial)
    yield part
  end
end

def initialize(an_io)

def initialize(an_io)
  @streaming = nil
  @stream = an_io
end

def inspect

Easier to interpret string inspect
def inspect
  "#<#{self.class}:#{object_id.to_s(16)} @streaming=#{!!@streaming}>"
end

def readall

def readall
  fail StateError, "body is being streamed" unless @streaming.nil?
  @streaming = false
  "".tap do |buf|
    buf << stream.read until stream.eof?
  end
end

def readpartial(size = HTTP::Connection::BUFFER_SIZE)

Returns:
  • (String, nil) - the next `size` octets part of the
def readpartial(size = HTTP::Connection::BUFFER_SIZE)
  stream!
  return nil if stream.eof?
  stream.readpartial(size)
end

def stream!

Assert that the body is actively being streamed
def stream!
  fail StateError, "body has already been consumed" if @streaming == false
  @streaming = true
end

def to_s

Returns:
  • (String) - eagerly consume the entire body as a string
def to_s
  @contents ||= readall
end