Class: HTTP::Response::Body

### Home Classes Methods

### In Files

- lib/http/response/body.rb

### Parent

Object

### Methods

- ::new
- #each
- #inspect
- #readpartial
- #stream!
- #to_s
- #to_str

### Included Modules

- Enumerable

### Class/Module Index

Quicksearch

- HTTP
- HTTP::Cache
- HTTP::Cache::Headers
- HTTP::Cache::NullCache
- HTTP::Cache::NullLogger
- HTTP::CacheError
- HTTP::Chainable
- HTTP::Client
- HTTP::Connection
- HTTP::Error
- HTTP::Headers
- HTTP::Headers::Mixin
- HTTP::InvalidHeaderNameError
- HTTP::MimeType
- HTTP::MimeType::Adapter
- HTTP::MimeType::JSON
- HTTP::Options
- HTTP::Redirector
- HTTP::Redirector::EndlessRedirectError
- HTTP::Redirector::TooManyRedirectsError
- HTTP::Request
- HTTP::Request::Caching
- HTTP::Request::UnsupportedMethodError
- HTTP::Request::UnsupportedSchemeError
- HTTP::Request::Writer
- HTTP::RequestError
- HTTP::Response
- HTTP::Response::Body
- HTTP::Response::Caching
- HTTP::Response::IoBody
- HTTP::Response::Parser
- HTTP::Response::Status
- HTTP::Response::StringBody
- HTTP::ResponseError
- HTTP::StateError
- HTTP::Timeout
- HTTP::Timeout::Global
- HTTP::Timeout::Null
- HTTP::Timeout::PerOperation
- HTTP::TimeoutError
- HTTP::URI
- Object

No matching classes.

# 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 25
    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

(see HTTP::Client#readpartial)

    # File lib/http/response/body.rb, line 19
    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

@return [String] eagerly consume the entire body as a string

    # File lib/http/response/body.rb, line 32
    def to_s
      return @contents if @contents

      fail StateError, "body is being streamed" unless @streaming.nil?

      begin
        @streaming = false
        @contents = "".force_encoding(Encoding::UTF_8)
        while (chunk = @client.readpartial)
          @contents << chunk.force_encoding(Encoding::ASCII_8BIT)
        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.