Class: HTTP::Response::IoBody

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/http/response/io_body.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#eachObject

Iterate over the body, allowing it to be enumerable



19
20
21
22
23
# File 'lib/http/response/io_body.rb', line 19

def each
  while (part = readpartial)
    yield part
  end
end

#inspectObject

Easier to interpret string inspect



40
41
42
# 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) ⇒ String?

body, or nil if whole body has already been read.

Returns:

  • (String, nil)

    the next size octets part of the



11
12
13
14
15
16
# 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!Object

Assert that the body is actively being streamed



34
35
36
37
# 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_sString Also known as: to_str

Returns eagerly consume the entire body as a string.

Returns:

  • (String)

    eagerly consume the entire body as a string



26
27
28
# File 'lib/http/response/io_body.rb', line 26

def to_s
  @contents ||= readall
end