Class: HTTP::Request::Body

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

Direct Known Subclasses

Features::AutoDeflate::CompressedBody

Defined Under Namespace

Classes: ProcIO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Body

Returns a new instance of Body.



8
9
10
11
12
# File 'lib/http/request/body.rb', line 8

def initialize(source)
  @source = source

  validate_source_type!
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

#==(other) ⇒ Object

Request bodies are equivalent when they have the same source.



48
49
50
# File 'lib/http/request/body.rb', line 48

def ==(other)
  self.class == other.class && self.source == other.source # rubocop:disable Style/RedundantSelf
end

#each {|| ... } ⇒ Object

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

Yield Parameters:

  • (String)


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/http/request/body.rb', line 34

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

  self
end

#sizeInteger

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

Returns:

  • (Integer)


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

def size
  if @source.is_a?(String)
    @source.bytesize
  elsif @source.respond_to?(:read)
    raise RequestError, "IO object must respond to #size" unless @source.respond_to?(:size)

    @source.size
  elsif @source.nil?
    0
  else
    raise RequestError, "cannot determine size of body: #{@source.inspect}"
  end
end