Class: HTTP::Response::IoBody

### Home Classes Methods

### In Files

- lib/http/response/io_body.rb

### Parent

Object

### Methods

- ::new
- #each
- #inspect
- #readall
- #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::IoBody

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

### Attributes

contents[R]
stream[R]

### Public Class Methods

new(an_io) click to toggle source

    # File lib/http/response/io_body.rb, line 46
    def initialize(an_io)
      @streaming = nil
      @stream = an_io
    end

### Public Instance Methods

each() click to toggle source

Iterate over the body, allowing it to be enumerable

    # File lib/http/response/io_body.rb, line 19
    def each
      while (part = readpartial)
        yield part
      end
    end

inspect() click to toggle source

Easier to interpret string inspect

    # File lib/http/response/io_body.rb, line 40
    def inspect
      "#<#{self.class}:#{object_id.to_s(16)} @streaming=#{!!@streaming}>"
    end

readpartial(size = HTTP::Connection::BUFFER_SIZE) click to toggle source

@return [String,nil] the next `size` octets part of the body, or nil if whole body has already been read.

    # File lib/http/response/io_body.rb, line 11
    def readpartial(size = HTTP::Connection::BUFFER_SIZE)
      stream!
      return nil if stream.eof?

      stream.readpartial(size)
    end

stream!() click to toggle source

Assert that the body is actively being streamed

    # File lib/http/response/io_body.rb, line 34
    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/io_body.rb, line 26
    def to_s
      @contents ||= readall
    end

Also aliased as: to_str
to_str() click to toggle source
Alias for: to_s

### Protected Instance Methods

readall() click to toggle source

    # File lib/http/response/io_body.rb, line 53
    def readall
      fail StateError, "body is being streamed" unless @streaming.nil?

      @streaming = false
      "".tap do |buf|
        buf << stream.read until stream.eof?
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.