Class: HTTP::Request::Body
- Inherits:
-
Object
- Object
- HTTP::Request::Body
- Defined in:
- lib/http/request/body.rb
Direct Known Subclasses
Defined Under Namespace
Classes: ProcIO
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Request bodies are equivalent when they have the same source.
-
#each {|| ... } ⇒ Object
Yields chunks of content to be streamed to the request body.
-
#initialize(source) ⇒ Body
constructor
A new instance of Body.
-
#size ⇒ Integer
Returns size which should be used for the "Content-Length" header.
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
#source ⇒ Object (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.
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 |
#size ⇒ Integer
Returns size which should be used for the "Content-Length" header.
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 |