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. read
  8. to_s
  9. 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
80 def initialize(response, threshold_size:, window_size: 1 << 14)
81   @response = response
82   @headers = response.headers
83   @threshold_size = threshold_size
84   @window_size = window_size
85   @encoding = response.content_type.charset || Encoding::BINARY
86   @length = 0
87   @buffer = nil
88   @state = :idle
89 end

Public Instance methods

== (other)
[show source]
    # File lib/httpx/response.rb
164 def ==(other)
165   to_s == other.to_s
166 end
bytesize ()
[show source]
    # File lib/httpx/response.rb
103 def bytesize
104   @length
105 end
close ()

closes/cleans the buffer, resets everything

[show source]
    # File lib/httpx/response.rb
154 def close
155   return if @state == :idle
156 
157   @buffer.close
158   @buffer.unlink if @buffer.respond_to?(:unlink)
159   @buffer = nil
160   @length = 0
161   @state = :idle
162 end
copy_to (dest)
[show source]
    # File lib/httpx/response.rb
142 def copy_to(dest)
143   return unless @buffer
144 
145   if dest.respond_to?(:path) && @buffer.respond_to?(:path)
146     FileUtils.mv(@buffer.path, dest.path)
147   else
148     @buffer.rewind
149     ::IO.copy_stream(@buffer, dest)
150   end
151 end
each ()
[show source]
    # File lib/httpx/response.rb
107 def each
108   return enum_for(__method__) unless block_given?
109 
110   begin
111     unless @state == :idle
112       rewind
113       while (chunk = @buffer.read(@window_size))
114         yield(chunk.force_encoding(@encoding))
115       end
116     end
117   ensure
118     close
119   end
120 end
empty? ()
[show source]
    # File lib/httpx/response.rb
138 def empty?
139   @length.zero?
140 end
read (*args)
[show source]
    # File lib/httpx/response.rb
 97 def read(*args)
 98   return unless @buffer
 99 
100   @buffer.read(*args)
101 end
to_s ()
[show source]
    # File lib/httpx/response.rb
122 def to_s
123   rewind
124   if @buffer
125     content = @buffer.read
126     begin
127       return content.force_encoding(@encoding)
128     rescue ArgumentError # ex: unknown encoding name - utf
129       return content
130     end
131   end
132   "".b
133 ensure
134   close
135 end
write (chunk)
[show source]
   # File lib/httpx/response.rb
91 def write(chunk)
92   @length += chunk.bytesize
93   transition
94   @buffer.write(chunk)
95 end