class HTTPX::Response::Body

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

Methods

Public Class

  1. new

Public Instance

  1. ==
  2. bytesize
  3. close
  4. closed?
  5. copy_to
  6. each
  7. empty?
  8. encoding
  9. filename
  10. inspect
  11. read
  12. to_s
  13. write

Public Instance Aliases

to_str -> to_s

Attributes

encoding [R]

Public Class methods

new(response, options)
[show source]
    # File lib/httpx/response.rb
133 def initialize(response, options)
134   @response = response
135   @headers = response.headers
136   @options = options
137   @threshold_size = options.body_threshold_size
138   @window_size = options.window_size
139   @encoding = response.content_type.charset || Encoding::BINARY
140   @length = 0
141   @buffer = nil
142   @state = :idle
143 end

Public Instance methods

==(other)
[show source]
    # File lib/httpx/response.rb
241 def ==(other)
242   object_id == other.object_id || begin
243     if other.respond_to?(:read)
244       _with_same_buffer_pos { FileUtils.compare_stream(@buffer, other) }
245     else
246       to_s == other.to_s
247     end
248   end
249 end
bytesize()
[show source]
    # File lib/httpx/response.rb
165 def bytesize
166   @length
167 end
close()

closes/cleans the buffer, resets everything

[show source]
    # File lib/httpx/response.rb
231 def close
232   if @buffer
233     @buffer.close
234     @buffer.unlink if @buffer.respond_to?(:unlink)
235     @buffer = nil
236   end
237   @length = 0
238   @state = :closed
239 end
closed?()
[show source]
    # File lib/httpx/response.rb
145 def closed?
146   @state == :closed
147 end
copy_to(dest)
[show source]
    # File lib/httpx/response.rb
218 def copy_to(dest)
219   return unless @buffer
220 
221   rewind
222 
223   if dest.respond_to?(:path) && @buffer.respond_to?(:path)
224     FileUtils.mv(@buffer.path, dest.path)
225   else
226     ::IO.copy_stream(@buffer, dest)
227   end
228 end
each()
[show source]
    # File lib/httpx/response.rb
169 def each
170   return enum_for(__method__) unless block_given?
171 
172   begin
173     if @buffer
174       rewind
175       while (chunk = @buffer.read(@window_size))
176         yield(chunk.force_encoding(@encoding))
177       end
178     end
179   ensure
180     close
181   end
182 end
empty?()
[show source]
    # File lib/httpx/response.rb
214 def empty?
215   @length.zero?
216 end
filename()
[show source]
    # File lib/httpx/response.rb
184 def filename
185   return unless @headers.key?("content-disposition")
186 
187   Utils.get_filename(@headers["content-disposition"])
188 end
inspect()

:nocov:

[show source]
    # File lib/httpx/response.rb
252 def inspect
253   "#<HTTPX::Response::Body:#{object_id} " \
254     "@state=#{@state} " \
255     "@length=#{@length}>"
256 end
read(*args)
[show source]
    # File lib/httpx/response.rb
157 def read(*args)
158   return unless @buffer
159 
160   rewind
161 
162   @buffer.read(*args)
163 end
to_s()
[show source]
    # File lib/httpx/response.rb
190 def to_s
191   case @buffer
192   when StringIO
193     begin
194       @buffer.string.force_encoding(@encoding)
195     rescue ArgumentError
196       @buffer.string
197     end
198   when Tempfile
199     rewind
200     content = _with_same_buffer_pos { @buffer.read }
201     begin
202       content.force_encoding(@encoding)
203     rescue ArgumentError # ex: unknown encoding name - utf
204       content
205     end
206   when nil
207     "".b
208   else
209     @buffer
210   end
211 end
write(chunk)
[show source]
    # File lib/httpx/response.rb
149 def write(chunk)
150   return if @state == :closed
151 
152   @length += chunk.bytesize
153   transition
154   @buffer.write(chunk)
155 end