Class: HTTPX::Request::Body
- Inherits:
-
Object
- Object
- HTTPX::Request::Body
- Defined in:
- lib/httpx/request.rb
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.
- #stream(body) ⇒ Object
- #unbounded_body? ⇒ Boolean
Constructor Details
#initialize(headers, options) ⇒ Body
Returns a new instance of Body.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/httpx/request.rb', line 129 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? end |
Class Method Details
.new(options) ⇒ Object
122 123 124 125 126 |
# File 'lib/httpx/request.rb', line 122 def new(*, ) return .body if .body.is_a?(self) super end |
Instance Method Details
#bytesize ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/httpx/request.rb', line 165 def bytesize return 0 if @body.nil? if @body.respond_to?(:bytesize) @body.bytesize elsif @body.respond_to?(:size) @body.size else raise Error, "cannot determine size of body: #{@body.inspect}" end end |
#chunk! ⇒ Object
191 192 193 |
# File 'lib/httpx/request.rb', line 191 def chunk! @headers.add("transfer-encoding", "chunked") end |
#chunked? ⇒ Boolean
187 188 189 |
# File 'lib/httpx/request.rb', line 187 def chunked? @headers["transfer-encoding"] == "chunked" end |
#each(&block) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/httpx/request.rb', line 144 def each(&block) return enum_for(__method__) unless block_given? 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
158 159 160 161 162 163 |
# File 'lib/httpx/request.rb', line 158 def empty? return true if @body.nil? return false if chunked? bytesize.zero? end |
#stream(body) ⇒ Object
177 178 179 180 181 |
# File 'lib/httpx/request.rb', line 177 def stream(body) encoded = body encoded = Transcoder.registry("chunker").encode(body) if chunked? encoded end |
#unbounded_body? ⇒ Boolean
183 184 185 |
# File 'lib/httpx/request.rb', line 183 def unbounded_body? chunked? || @body.bytesize == Float::INFINITY end |