Class: HTTPX::Request::Body

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/httpx/request.rb

Overview

:nocov:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, options) ⇒ Body

Returns a new instance of Body.



165
166
167
168
169
170
171
172
173
# File 'lib/httpx/request.rb', line 165

def initialize(headers, options)
  @headers = headers
  @body = initialize_body(options)
  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



158
159
160
161
162
# File 'lib/httpx/request.rb', line 158

def new(_, options)
  return options.body if options.body.is_a?(self)

  super
end

Instance Method Details

#bytesizeObject



202
203
204
205
206
# File 'lib/httpx/request.rb', line 202

def bytesize
  return 0 if @body.nil?

  @body.bytesize
end

#chunk!Object



224
225
226
# File 'lib/httpx/request.rb', line 224

def chunk!
  @headers.add("transfer-encoding", "chunked")
end

#chunked?Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/httpx/request.rb', line 220

def chunked?
  @headers["transfer-encoding"] == "chunked"
end

#each(&block) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/httpx/request.rb', line 175

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

Returns:

  • (Boolean)


195
196
197
198
199
200
# File 'lib/httpx/request.rb', line 195

def empty?
  return true if @body.nil?
  return false if chunked?

  @body.bytesize.zero?
end

#inspectObject

:nocov:



229
230
231
232
# File 'lib/httpx/request.rb', line 229

def inspect
  "#<HTTPX::Request::Body:#{object_id} " \
    "#{unbounded_body? ? "stream" : "@bytesize=#{bytesize}"}>"
end

#rewindObject



189
190
191
192
193
# File 'lib/httpx/request.rb', line 189

def rewind
  return if empty?

  @body.rewind if @body.respond_to?(:rewind)
end

#stream(body) ⇒ Object



208
209
210
211
212
# File 'lib/httpx/request.rb', line 208

def stream(body)
  encoded = body
  encoded = Transcoder::Chunker.encode(body.enum_for(:each)) if chunked?
  encoded
end

#unbounded_body?Boolean

Returns:

  • (Boolean)


214
215
216
217
218
# File 'lib/httpx/request.rb', line 214

def unbounded_body?
  return @unbounded_body if defined?(@unbounded_body)

  @unbounded_body = !@body.nil? && (chunked? || @body.bytesize == Float::INFINITY)
end