Class: HTTP::Request::Body
- Inherits:
-
Object
- Object
- HTTP::Request::Body
- Defined in:
- lib/http/request/body.rb
Overview
Represents an HTTP request body with streaming support
Direct Known Subclasses
Defined Under Namespace
Classes: ProcIO
Instance Attribute Summary collapse
-
#source ⇒ String, ...
readonly
The source data for this body.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check equality based on source.
-
#each {|| ... } ⇒ self
Yields chunks of content to be streamed.
-
#empty? ⇒ Boolean
Whether the body is empty.
-
#initialize(source) ⇒ HTTP::Request::Body
constructor
Initialize a new request body.
-
#loggable? ⇒ Boolean
Whether the body content can be accessed for logging.
-
#size ⇒ Integer
Returns size for the “Content-Length” header.
Constructor Details
#initialize(source) ⇒ HTTP::Request::Body
Initialize a new request body
23 24 25 26 27 |
# File 'lib/http/request/body.rb', line 23 def initialize(source) @source = source validate_source_type! end |
Instance Attribute Details
#source ⇒ String, ... (readonly)
The source data for this body
14 15 16 |
# File 'lib/http/request/body.rb', line 14 def source @source end |
Instance Method Details
#==(other) ⇒ Boolean
Check equality based on source
109 110 111 |
# File 'lib/http/request/body.rb', line 109 def ==(other) other.is_a?(self.class) && source == other.source end |
#each {|| ... } ⇒ self
Yields chunks of content to be streamed
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/http/request/body.rb', line 89 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 @source.each(&block) end self end |
#empty? ⇒ Boolean
Whether the body is empty
36 37 38 |
# File 'lib/http/request/body.rb', line 36 def empty? @source.nil? end |
#loggable? ⇒ Boolean
Whether the body content can be accessed for logging
Returns true for String sources (the content can be inspected). Returns false for IO streams and Enumerables (which cannot be read without consuming them), and for nil bodies.
The logging feature checks the string encoding separately to decide whether to log the content as text or format it as binary.
54 55 56 |
# File 'lib/http/request/body.rb', line 54 def loggable? @source.is_a?(String) end |
#size ⇒ Integer
Returns size for the “Content-Length” header
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/http/request/body.rb', line 65 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}; " \ "set the Content-Length header explicitly or use chunked Transfer-Encoding" end end |