class HTTPX::Request::Body

  1. lib/httpx/request.rb
Superclass: Object

:nocov:

Methods

Public Class

  1. new
  2. new

Public Instance

  1. bytesize
  2. chunk!
  3. chunked?
  4. each
  5. empty?
  6. inspect
  7. stream
  8. unbounded_body?

Public Class methods

new (*, options)
[show source]
    # File lib/httpx/request.rb
137 def new(*, options)
138   return options.body if options.body.is_a?(self)
139 
140   super
141 end
new (headers, options)
[show source]
    # File lib/httpx/request.rb
144 def initialize(headers, options)
145   @headers = headers
146   @body = if options.body
147     Transcoder.registry("body").encode(options.body)
148   elsif options.form
149     Transcoder.registry("form").encode(options.form)
150   elsif options.json
151     Transcoder.registry("json").encode(options.json)
152   end
153   return if @body.nil?
154 
155   @headers["content-type"] ||= @body.content_type
156   @headers["content-length"] = @body.bytesize unless unbounded_body?
157 end

Public Instance methods

bytesize ()
[show source]
    # File lib/httpx/request.rb
180 def bytesize
181   return 0 if @body.nil?
182 
183   if @body.respond_to?(:bytesize)
184     @body.bytesize
185   elsif @body.respond_to?(:size)
186     @body.size
187   else
188     raise Error, "cannot determine size of body: #{@body.inspect}"
189   end
190 end
chunk! ()
[show source]
    # File lib/httpx/request.rb
206 def chunk!
207   @headers.add("transfer-encoding", "chunked")
208 end
chunked? ()
[show source]
    # File lib/httpx/request.rb
202 def chunked?
203   @headers["transfer-encoding"] == "chunked"
204 end
each (&block)
[show source]
    # File lib/httpx/request.rb
159 def each(&block)
160   return enum_for(__method__) unless block_given?
161   return if @body.nil?
162 
163   body = stream(@body)
164   if body.respond_to?(:read)
165     ::IO.copy_stream(body, ProcIO.new(block))
166   elsif body.respond_to?(:each)
167     body.each(&block)
168   else
169     block[body.to_s]
170   end
171 end
empty? ()
[show source]
    # File lib/httpx/request.rb
173 def empty?
174   return true if @body.nil?
175   return false if chunked?
176 
177   bytesize.zero?
178 end
inspect ()

:nocov:

[show source]
    # File lib/httpx/request.rb
211 def inspect
212   "#<HTTPX::Request::Body:#{object_id} " \
213   "#{unbounded_body? ? "stream" : "@bytesize=#{bytesize}"}>"
214 end
stream (body)
[show source]
    # File lib/httpx/request.rb
192 def stream(body)
193   encoded = body
194   encoded = Transcoder.registry("chunker").encode(body) if chunked?
195   encoded
196 end
unbounded_body? ()
[show source]
    # File lib/httpx/request.rb
198 def unbounded_body?
199   chunked? || @body.bytesize == Float::INFINITY
200 end