Parent

Included Modules

HTTP::Response::Body

A streamable response body, also easily converted into a string

Public Class Methods

new(client) click to toggle source
# File lib/http/response/body.rb, line 12
def initialize(client)
  @client    = client
  @streaming = nil
  @contents  = nil
end

Public Instance Methods

each() click to toggle source

Iterate over the body, allowing it to be enumerable

# File lib/http/response/body.rb, line 26
def each
  while (chunk = readpartial)
    yield chunk
  end
end
inspect() click to toggle source

Easier to interpret string inspect

# File lib/http/response/body.rb, line 59
def inspect
  "#<#{self.class}:#{object_id.to_s(16)} @streaming=#{!!@streaming}>"
end
readpartial(*args) click to toggle source

Read up to length bytes, but return any data that’s available @see HTTP::Client#readpartial

# File lib/http/response/body.rb, line 20
def readpartial(*args)
  stream!
  @client.readpartial(*args)
end
stream!() click to toggle source

Assert that the body is actively being streamed

# File lib/http/response/body.rb, line 53
def stream!
  fail StateError, 'body has already been consumed' if @streaming == false
  @streaming = true
end
to_s() click to toggle source

Eagerly consume the entire body as a string

# File lib/http/response/body.rb, line 33
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
Also aliased as: to_str
to_str() click to toggle source
Alias for: to_s

[Validate]

Generated with the Darkfish Rdoc Generator 2.