Class: HTTP::Request::Body

Inherits:
Object
  • Object
show all
Defined in:
lib/http/request/body.rb

Defined Under Namespace

Classes: ProcIO

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Body

Returns a new instance of Body.



6
7
8
9
10
# File 'lib/http/request/body.rb', line 6

def initialize(body)
  @body = body

  validate_body_type!
end

Instance Method Details

#each {|| ... } ⇒ Object

Yields chunks of content to be streamed to the request body.

Yield Parameters:

  • (String)


31
32
33
34
35
36
37
38
39
# File 'lib/http/request/body.rb', line 31

def each(&block)
  if @body.is_a?(String)
    yield @body
  elsif @body.respond_to?(:read)
    IO.copy_stream(@body, ProcIO.new(block))
  elsif @body.is_a?(Enumerable)
    @body.each(&block)
  end
end

#sizeInteger

Returns size which should be used for the "Content-Length" header.

Returns:

  • (Integer)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/http/request/body.rb', line 15

def size
  if @body.is_a?(String)
    @body.bytesize
  elsif @body.respond_to?(:read)
    raise RequestError, "IO object must respond to #size" unless @body.respond_to?(:size)
    @body.size
  elsif @body.nil?
    0
  else
    raise RequestError, "cannot determine size of body: #{@body.inspect}"
  end
end