Class: HTTPX::Request::Body
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- HTTPX::Request::Body
- Defined in:
- lib/httpx/request.rb
Overview
:nocov:
Class Method Summary collapse
Instance Method Summary collapse
- #bytesize ⇒ Object
- #chunk! ⇒ Object
- #chunked? ⇒ Boolean
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(headers, options) ⇒ Body
constructor
A new instance of Body.
-
#inspect ⇒ Object
:nocov:.
- #rewind ⇒ Object
- #stream(body) ⇒ Object
- #unbounded_body? ⇒ Boolean
Constructor Details
#initialize(headers, options) ⇒ Body
Returns a new instance of Body.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/httpx/request.rb', line 167 def initialize(headers, ) @headers = headers @body = if .body Transcoder.registry("body").encode(.body) elsif .form Transcoder.registry("form").encode(.form) elsif .json Transcoder.registry("json").encode(.json) end return if @body.nil? @headers["content-type"] ||= @body.content_type @headers["content-length"] = @body.bytesize unless unbounded_body? super(@body) end |
Class Method Details
.new(_, options) ⇒ Object
160 161 162 163 164 |
# File 'lib/httpx/request.rb', line 160 def new(_, ) return .body if .body.is_a?(self) super end |
Instance Method Details
#bytesize ⇒ Object
210 211 212 213 214 |
# File 'lib/httpx/request.rb', line 210 def bytesize return 0 if @body.nil? @body.bytesize end |
#chunk! ⇒ Object
232 233 234 |
# File 'lib/httpx/request.rb', line 232 def chunk! @headers.add("transfer-encoding", "chunked") end |
#chunked? ⇒ Boolean
228 229 230 |
# File 'lib/httpx/request.rb', line 228 def chunked? @headers["transfer-encoding"] == "chunked" end |
#each(&block) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/httpx/request.rb', line 183 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
203 204 205 206 207 208 |
# File 'lib/httpx/request.rb', line 203 def empty? return true if @body.nil? return false if chunked? @body.bytesize.zero? end |
#inspect ⇒ Object
:nocov:
237 238 239 240 |
# File 'lib/httpx/request.rb', line 237 def inspect "#<HTTPX::Request::Body:#{object_id} " \ "#{unbounded_body? ? "stream" : "@bytesize=#{bytesize}"}>" end |
#rewind ⇒ Object
197 198 199 200 201 |
# File 'lib/httpx/request.rb', line 197 def rewind return if empty? @body.rewind if @body.respond_to?(:rewind) end |
#stream(body) ⇒ Object
216 217 218 219 220 |
# File 'lib/httpx/request.rb', line 216 def stream(body) encoded = body encoded = Transcoder.registry("chunker").encode(body.enum_for(:each)) if chunked? encoded end |
#unbounded_body? ⇒ Boolean
222 223 224 225 226 |
# File 'lib/httpx/request.rb', line 222 def unbounded_body? return @unbounded_body if defined?(@unbounded_body) @unbounded_body = !@body.nil? && (chunked? || @body.bytesize == Float::INFINITY) end |