Class: HTTPX::Response::Body
- Inherits:
-
Object
- Object
- HTTPX::Response::Body
- Defined in:
- lib/httpx/response.rb
Instance Attribute Summary collapse
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #bytesize ⇒ Object
-
#close ⇒ Object
closes/cleans the buffer, resets everything.
- #closed? ⇒ Boolean
- #copy_to(dest) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #filename ⇒ Object
-
#initialize(response, options) ⇒ Body
constructor
A new instance of Body.
- #initialize_dup(other) ⇒ Object
-
#inspect ⇒ Object
:nocov:.
- #read(*args) ⇒ Object
-
#rewind ⇒ Object
:nocov:.
- #to_s ⇒ Object (also: #to_str)
- #write(chunk) ⇒ Object
Constructor Details
#initialize(response, options) ⇒ Body
Returns a new instance of Body.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/httpx/response.rb', line 131 def initialize(response, ) @response = response @headers = response.headers @options = @threshold_size = .body_threshold_size @window_size = .window_size @encoding = response.content_type.charset || Encoding::BINARY @length = 0 @buffer = nil @state = :idle end |
Instance Attribute Details
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
129 130 131 |
# File 'lib/httpx/response.rb', line 129 def encoding @encoding end |
Instance Method Details
#==(other) ⇒ Object
250 251 252 253 254 255 256 257 258 |
# File 'lib/httpx/response.rb', line 250 def ==(other) object_id == other.object_id || begin if other.respond_to?(:read) _with_same_buffer_pos { FileUtils.compare_stream(@buffer, other) } else to_s == other.to_s end end end |
#bytesize ⇒ Object
176 177 178 |
# File 'lib/httpx/response.rb', line 176 def bytesize @length end |
#close ⇒ Object
closes/cleans the buffer, resets everything
240 241 242 243 244 245 246 247 248 |
# File 'lib/httpx/response.rb', line 240 def close if @buffer @buffer.close @buffer.unlink if @buffer.respond_to?(:unlink) @buffer = nil end @length = 0 @state = :closed end |
#closed? ⇒ Boolean
149 150 151 |
# File 'lib/httpx/response.rb', line 149 def closed? @state == :closed end |
#copy_to(dest) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/httpx/response.rb', line 227 def copy_to(dest) return unless @buffer rewind if dest.respond_to?(:path) && @buffer.respond_to?(:path) FileUtils.mv(@buffer.path, dest.path) else ::IO.copy_stream(@buffer, dest) end end |
#each ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/httpx/response.rb', line 180 def each return enum_for(__method__) unless block_given? begin if @buffer rewind while (chunk = @buffer.read(@window_size)) yield(chunk.force_encoding(@encoding)) end end ensure close end end |
#empty? ⇒ Boolean
223 224 225 |
# File 'lib/httpx/response.rb', line 223 def empty? @length.zero? end |
#filename ⇒ Object
195 196 197 198 199 |
# File 'lib/httpx/response.rb', line 195 def filename return unless @headers.key?("content-disposition") Utils.get_filename(@headers["content-disposition"]) end |
#initialize_dup(other) ⇒ Object
143 144 145 146 147 |
# File 'lib/httpx/response.rb', line 143 def initialize_dup(other) super @buffer = other.instance_variable_get(:@buffer).dup end |
#inspect ⇒ Object
:nocov:
261 262 263 264 265 |
# File 'lib/httpx/response.rb', line 261 def inspect "#<HTTPX::Response::Body:#{object_id} " \ "@state=#{@state} " \ "@length=#{@length}>" end |
#read(*args) ⇒ Object
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/httpx/response.rb', line 165 def read(*args) return unless @buffer unless @reader rewind @reader = @buffer end @reader.read(*args) end |
#rewind ⇒ Object
:nocov:
268 269 270 271 272 273 274 275 |
# File 'lib/httpx/response.rb', line 268 def rewind return unless @buffer # in case there's some reading going on @reader = nil @buffer.rewind end |
#to_s ⇒ Object Also known as: to_str
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/httpx/response.rb', line 201 def to_s case @buffer when StringIO begin @buffer.string.force_encoding(@encoding) rescue ArgumentError @buffer.string end when Tempfile rewind content = _with_same_buffer_pos { @buffer.read } begin content.force_encoding(@encoding) rescue ArgumentError # ex: unknown encoding name - utf content end else "".b end end |
#write(chunk) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/httpx/response.rb', line 153 def write(chunk) return if @state == :closed size = chunk.bytesize @length += size transition @buffer.write(chunk) @response.emit(:chunk_received, chunk) size end |