class HTTPX::Response::Body

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

Methods

Public Class

  1. new

Public Instance

  1. ==
  2. bytesize
  3. close
  4. copy_to
  5. each
  6. empty?
  7. inspect
  8. read
  9. to_s
  10. write

Public Instance Aliases

to_str -> to_s

Public Class methods

new (response, threshold_size:, window_size: 1 << 14)
[show source]
   # File lib/httpx/response.rb
86 def initialize(response, threshold_size:, window_size: 1 << 14)
87   @response = response
88   @headers = response.headers
89   @threshold_size = threshold_size
90   @window_size = window_size
91   @encoding = response.content_type.charset || Encoding::BINARY
92   @length = 0
93   @buffer = nil
94   @state = :idle
95 end

Public Instance methods

== (other)
[show source]
    # File lib/httpx/response.rb
174 def ==(other)
175   to_s == other.to_s
176 end
bytesize ()
[show source]
    # File lib/httpx/response.rb
111 def bytesize
112   @length
113 end
close ()

closes/cleans the buffer, resets everything

[show source]
    # File lib/httpx/response.rb
164 def close
165   return if @state == :idle
166 
167   @buffer.close
168   @buffer.unlink if @buffer.respond_to?(:unlink)
169   @buffer = nil
170   @length = 0
171   @state = :idle
172 end
copy_to (dest)
[show source]
    # File lib/httpx/response.rb
150 def copy_to(dest)
151   return unless @buffer
152 
153   rewind
154 
155   if dest.respond_to?(:path) && @buffer.respond_to?(:path)
156     FileUtils.mv(@buffer.path, dest.path)
157   else
158     @buffer.rewind
159     ::IO.copy_stream(@buffer, dest)
160   end
161 end
each ()
[show source]
    # File lib/httpx/response.rb
115 def each
116   return enum_for(__method__) unless block_given?
117 
118   begin
119     unless @state == :idle
120       rewind
121       while (chunk = @buffer.read(@window_size))
122         yield(chunk.force_encoding(@encoding))
123       end
124     end
125   ensure
126     close
127   end
128 end
empty? ()
[show source]
    # File lib/httpx/response.rb
146 def empty?
147   @length.zero?
148 end
inspect ()

:nocov:

[show source]
    # File lib/httpx/response.rb
179 def inspect
180   "#<HTTPX::Response::Body:#{object_id} " \
181   "@state=#{@state} " \
182   "@length=#{@length}>"
183 end
read (*args)
[show source]
    # File lib/httpx/response.rb
103 def read(*args)
104   return unless @buffer
105 
106   rewind
107 
108   @buffer.read(*args)
109 end
to_s ()
[show source]
    # File lib/httpx/response.rb
130 def to_s
131   rewind
132   if @buffer
133     content = @buffer.read
134     begin
135       return content.force_encoding(@encoding)
136     rescue ArgumentError # ex: unknown encoding name - utf
137       return content
138     end
139   end
140   "".b
141 ensure
142   close
143 end
write (chunk)
[show source]
    # File lib/httpx/response.rb
 97 def write(chunk)
 98   @length += chunk.bytesize
 99   transition
100   @buffer.write(chunk)
101 end