Class: HTTPX::Request::Body
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- HTTPX::Request::Body
- Defined in:
- lib/httpx/request/body.rb
Overview
Implementation of the HTTP Request body as a delegator which iterates (responds to each) payload chunks.
Class Method Summary collapse
-
.initialize_deflater_body(body, encoding) ⇒ Object
returns the
bodywrapped with the correct deflater accordinng to the givenencodisng. - .new(_, options) ⇒ Object
Instance Method Summary collapse
-
#bytesize ⇒ Object
returns the @body payload size in bytes.
-
#chunk! ⇒ Object
sets the chunked transfer encoding header.
-
#chunked? ⇒ Boolean
returns whether the chunked transfer encoding header is set.
-
#each(&block) ⇒ Object
consumes and yields the request payload in chunks.
-
#empty? ⇒ Boolean
return
trueif thebodyhas been fully drained (or does nnot exist). -
#initialize(headers, options) ⇒ Body
constructor
inits the instance with the request
headersandoptions, which contain the payload definition. -
#inspect ⇒ Object
:nocov:.
-
#rewind ⇒ Object
if the @body is rewindable, it rewinnds it.
-
#stream(body) ⇒ Object
sets the body to yield using chunked trannsfer encoding format.
-
#unbounded_body? ⇒ Boolean
returns whether the body yields infinitely.
Constructor Details
#initialize(headers, options) ⇒ Body
inits the instance with the request headers and options, which contain the payload definition.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/httpx/request/body.rb', line 15 def initialize(headers, ) @headers = headers # forego compression in the Range request case if @headers.key?("range") @headers.delete("accept-encoding") else @headers["accept-encoding"] ||= .supported_compression_formats end initialize_body() return if @body.nil? @headers["content-type"] ||= @body.content_type @headers["content-length"] = @body.bytesize unless unbounded_body? super(@body) end |
Class Method Details
.initialize_deflater_body(body, encoding) ⇒ Object
returns the body wrapped with the correct deflater accordinng to the given encodisng.
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/httpx/request/body.rb', line 131 def initialize_deflater_body(body, encoding) case encoding when "gzip" Transcoder::GZIP.encode(body) when "deflate" Transcoder::Deflate.encode(body) when "identity" body else body end end |
.new(_, options) ⇒ Object
7 8 9 10 11 |
# File 'lib/httpx/request/body.rb', line 7 def new(_, ) return .body if .body.is_a?(self) super end |
Instance Method Details
#bytesize ⇒ Object
returns the @body payload size in bytes.
65 66 67 68 69 |
# File 'lib/httpx/request/body.rb', line 65 def bytesize return 0 if @body.nil? @body.bytesize end |
#chunk! ⇒ Object
sets the chunked transfer encoding header.
91 92 93 |
# File 'lib/httpx/request/body.rb', line 91 def chunk! @headers.add("transfer-encoding", "chunked") end |
#chunked? ⇒ Boolean
returns whether the chunked transfer encoding header is set.
86 87 88 |
# File 'lib/httpx/request/body.rb', line 86 def chunked? @headers["transfer-encoding"] == "chunked" end |
#each(&block) ⇒ Object
consumes and yields the request payload in chunks.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/httpx/request/body.rb', line 35 def each(&block) return enum_for(__method__) unless block return if @body.nil? body = stream(@body) if body.respond_to?(:read) ::IO.copy_stream(body, ProcIO.new(block)) elsif body.respond_to?(:each) body.each(&block) else block[body.to_s] end end |
#empty? ⇒ Boolean
return true if the body has been fully drained (or does nnot exist).
57 58 59 60 61 62 |
# File 'lib/httpx/request/body.rb', line 57 def empty? return true if @body.nil? return false if chunked? @body.bytesize.zero? end |
#inspect ⇒ Object
:nocov:
96 97 98 99 |
# File 'lib/httpx/request/body.rb', line 96 def inspect "#<HTTPX::Request::Body:#{object_id} " \ "#{unbounded_body? ? "stream" : "@bytesize=#{bytesize}"}>" end |
#rewind ⇒ Object
if the @body is rewindable, it rewinnds it.
50 51 52 53 54 |
# File 'lib/httpx/request/body.rb', line 50 def rewind return if empty? @body.rewind if @body.respond_to?(:rewind) end |
#stream(body) ⇒ Object
sets the body to yield using chunked trannsfer encoding format.
72 73 74 75 76 |
# File 'lib/httpx/request/body.rb', line 72 def stream(body) return body unless chunked? Transcoder::Chunker.encode(body.enum_for(:each)) end |
#unbounded_body? ⇒ Boolean
returns whether the body yields infinitely.
79 80 81 82 83 |
# File 'lib/httpx/request/body.rb', line 79 def unbounded_body? return @unbounded_body if defined?(@unbounded_body) @unbounded_body = !@body.nil? && (chunked? || @body.bytesize == Float::INFINITY) end |