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 initialize(client)
def initialize(client) @client = client @streaming = nil @contents = nil end
def inspect
def inspect "#<#{self.class}:#{object_id.to_s(16)} @streaming=#{!!@streaming}>" end
def readpartial(*args)
- See: HTTP::Client#readpartial -
def readpartial(*args) stream! @client.readpartial(*args) end
def stream!
def stream! fail StateError, 'body has already been consumed' if @streaming == false @streaming = true end
def to_s
def to_s return @contents if @contents fail StateError, 'body is being streamed' unless @streaming.nil? begin @streaming = false @contents = '' while (chunk = @client.readpartial) @contents << chunk end rescue @contents = nil raise end @contents end