Class: HTTP::Request::Body
- Inherits:
-
Object
- Object
- HTTP::Request::Body
- Defined in:
- lib/http/request/body.rb
Defined Under Namespace
Classes: ProcIO
Instance Method Summary collapse
-
#each {|| ... } ⇒ Object
Yields chunks of content to be streamed to the request body.
-
#initialize(body) ⇒ Body
constructor
A new instance of Body.
-
#size ⇒ Integer
Returns size which should be used for the "Content-Length" header.
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.
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 |
#size ⇒ Integer
Returns size which should be used for the "Content-Length" header.
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 |