class HTTPX::Request::Body

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

Methods

Public Class

  1. new
  2. new

Public Instance

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

Public Class methods

new (*, options)
[show source]
    # File lib/httpx/request.rb
122 def new(*, options)
123   return options.body if options.body.is_a?(self)
124 
125   super
126 end
new (headers, options)
[show source]
    # File lib/httpx/request.rb
129 def initialize(headers, options)
130   @headers = headers
131   @body = if options.body
132     Transcoder.registry("body").encode(options.body)
133   elsif options.form
134     Transcoder.registry("form").encode(options.form)
135   elsif options.json
136     Transcoder.registry("json").encode(options.json)
137   end
138   return if @body.nil?
139 
140   @headers["content-type"] ||= @body.content_type
141   @headers["content-length"] = @body.bytesize unless unbounded_body?
142 end

Public Instance methods

bytesize ()
[show source]
    # File lib/httpx/request.rb
165 def bytesize
166   return 0 if @body.nil?
167 
168   if @body.respond_to?(:bytesize)
169     @body.bytesize
170   elsif @body.respond_to?(:size)
171     @body.size
172   else
173     raise Error, "cannot determine size of body: #{@body.inspect}"
174   end
175 end
chunk! ()
[show source]
    # File lib/httpx/request.rb
191 def chunk!
192   @headers.add("transfer-encoding", "chunked")
193 end
chunked? ()
[show source]
    # File lib/httpx/request.rb
187 def chunked?
188   @headers["transfer-encoding"] == "chunked"
189 end
each (&block)
[show source]
    # File lib/httpx/request.rb
144 def each(&block)
145   return enum_for(__method__) unless block_given?
146   return if @body.nil?
147 
148   body = stream(@body)
149   if body.respond_to?(:read)
150     ::IO.copy_stream(body, ProcIO.new(block))
151   elsif body.respond_to?(:each)
152     body.each(&block)
153   else
154     block[body.to_s]
155   end
156 end
empty? ()
[show source]
    # File lib/httpx/request.rb
158 def empty?
159   return true if @body.nil?
160   return false if chunked?
161 
162   bytesize.zero?
163 end
stream (body)
[show source]
    # File lib/httpx/request.rb
177 def stream(body)
178   encoded = body
179   encoded = Transcoder.registry("chunker").encode(body) if chunked?
180   encoded
181 end
unbounded_body? ()
[show source]
    # File lib/httpx/request.rb
183 def unbounded_body?
184   chunked? || @body.bytesize == Float::INFINITY
185 end