Class: HTTP::Response::StringBody

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#each {|@contents| ... } ⇒ Object

Iterate over the body, allowing it to be enumerable

Yields:

  • (@contents)


21
22
23
# File 'lib/http/response/string_body.rb', line 21

def each
  yield @contents
end

#inspectObject

Easier to interpret string inspect



40
41
42
# File 'lib/http/response/string_body.rb', line 40

def inspect
  "#<#{self.class}:#{object_id.to_s(16)}>"
end

#readpartial(size = @contents.length) ⇒ 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
17
18
# 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!Object

Assert that the body is actively being streamed



34
35
36
37
# 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_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/string_body.rb', line 26

def to_s
  @contents
end