Class: HTTP::Response::StringBody

### Home Classes Methods

### In Files

- lib/http/response/string_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::StringBody

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

### Public Class Methods

new(contents) click to toggle source

    # File lib/http/response/string_body.rb, line 46
    def initialize(contents)
      @contents = contents
      @streaming = nil
      @streaming_offset = 0
    end

### Public Instance Methods

each() click to toggle source

Iterate over the body, allowing it to be enumerable

    # File lib/http/response/string_body.rb, line 21
    def each
      yield @contents
    end

inspect() click to toggle source

Easier to interpret string inspect

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

readpartial(size = @contents.length) 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/string_body.rb, line 11
    def readpartial(size = @contents.length)
      stream!
      return nil if @streaming_offset >= @contents.length

      @contents[@streaming_offset, size].tap do |part|
        @streaming_offset += (part.length + 1)
      end
    end

stream!() click to toggle source

Assert that the body is actively being streamed

    # File lib/http/response/string_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/string_body.rb, line 26
    def to_s
      @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.