class HTTPX::Response::Body

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

Public Instance Aliases

to_str -> to_s

Attributes

encoding [R]

Public Class methods

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

Public Instance methods

==(other)
[show source]
    # File lib/httpx/response.rb
250 def ==(other)
251   object_id == other.object_id || begin
252     if other.respond_to?(:read)
253       _with_same_buffer_pos { FileUtils.compare_stream(@buffer, other) }
254     else
255       to_s == other.to_s
256     end
257   end
258 end
bytesize()
[show source]
    # File lib/httpx/response.rb
176 def bytesize
177   @length
178 end
close()

closes/cleans the buffer, resets everything

[show source]
    # File lib/httpx/response.rb
240 def close
241   if @buffer
242     @buffer.close
243     @buffer.unlink if @buffer.respond_to?(:unlink)
244     @buffer = nil
245   end
246   @length = 0
247   @state = :closed
248 end
closed?()
[show source]
    # File lib/httpx/response.rb
149 def closed?
150   @state == :closed
151 end
copy_to(dest)
[show source]
    # File lib/httpx/response.rb
227 def copy_to(dest)
228   return unless @buffer
229 
230   rewind
231 
232   if dest.respond_to?(:path) && @buffer.respond_to?(:path)
233     FileUtils.mv(@buffer.path, dest.path)
234   else
235     ::IO.copy_stream(@buffer, dest)
236   end
237 end
each()
[show source]
    # File lib/httpx/response.rb
180 def each
181   return enum_for(__method__) unless block_given?
182 
183   begin
184     if @buffer
185       rewind
186       while (chunk = @buffer.read(@window_size))
187         yield(chunk.force_encoding(@encoding))
188       end
189     end
190   ensure
191     close
192   end
193 end
empty?()
[show source]
    # File lib/httpx/response.rb
223 def empty?
224   @length.zero?
225 end
filename()
[show source]
    # File lib/httpx/response.rb
195 def filename
196   return unless @headers.key?("content-disposition")
197 
198   Utils.get_filename(@headers["content-disposition"])
199 end
initialize_dup(other)
[show source]
    # File lib/httpx/response.rb
143 def initialize_dup(other)
144   super
145 
146   @buffer = other.instance_variable_get(:@buffer).dup
147 end
inspect()

:nocov:

[show source]
    # File lib/httpx/response.rb
261 def inspect
262   "#<HTTPX::Response::Body:#{object_id} " \
263     "@state=#{@state} " \
264     "@length=#{@length}>"
265 end
read(*args)
[show source]
    # File lib/httpx/response.rb
165 def read(*args)
166   return unless @buffer
167 
168   unless @reader
169     rewind
170     @reader = @buffer
171   end
172 
173   @reader.read(*args)
174 end
rewind()

:nocov:

[show source]
    # File lib/httpx/response.rb
268 def rewind
269   return unless @buffer
270 
271   # in case there's some reading going on
272   @reader = nil
273 
274   @buffer.rewind
275 end
to_s()
[show source]
    # File lib/httpx/response.rb
201 def to_s
202   case @buffer
203   when StringIO
204     begin
205       @buffer.string.force_encoding(@encoding)
206     rescue ArgumentError
207       @buffer.string
208     end
209   when Tempfile
210     rewind
211     content = _with_same_buffer_pos { @buffer.read }
212     begin
213       content.force_encoding(@encoding)
214     rescue ArgumentError # ex: unknown encoding name - utf
215       content
216     end
217   else
218     "".b
219   end
220 end
write(chunk)
[show source]
    # File lib/httpx/response.rb
153 def write(chunk)
154   return if @state == :closed
155 
156   size = chunk.bytesize
157   @length += size
158   transition
159   @buffer.write(chunk)
160 
161   @response.emit(:chunk_received, chunk)
162   size
163 end