class HTTPX::Response::Buffer

  1. lib/httpx/response/buffer.rb
Superclass: SimpleDelegator

wraps and delegates to an internal buffer, which can be a StringIO or a Tempfile.

Methods

Public Class

  1. new

Public Instance

  1. close
  2. initialize_dup
  3. size
  4. to_s
  5. write

Public Class methods

new(threshold_size:, bytesize: 0, encoding: Encoding::BINARY)

initializes buffer with the threshold_size over which the payload gets buffer to a tempfile, the initial bytesize, and the encoding.

[show source]
   # File lib/httpx/response/buffer.rb
12 def initialize(threshold_size:, bytesize: 0, encoding: Encoding::BINARY)
13   @threshold_size = threshold_size
14   @bytesize = bytesize
15   @encoding = encoding
16   @buffer = StringIO.new("".b)
17   super(@buffer)
18 end

Public Instance methods

close()

closes the buffer.

[show source]
   # File lib/httpx/response/buffer.rb
59 def close
60   @buffer.close
61   @buffer.unlink if @buffer.respond_to?(:unlink)
62 end
initialize_dup(other)
[show source]
   # File lib/httpx/response/buffer.rb
20 def initialize_dup(other)
21   super
22 
23   @buffer = other.instance_variable_get(:@buffer).dup
24 end
size()

size in bytes of the buffered content.

[show source]
   # File lib/httpx/response/buffer.rb
27 def size
28   @bytesize
29 end
to_s()

returns the buffered content as a string.

[show source]
   # File lib/httpx/response/buffer.rb
39 def to_s
40   case @buffer
41   when StringIO
42     begin
43       @buffer.string.force_encoding(@encoding)
44     rescue ArgumentError
45       @buffer.string
46     end
47   when Tempfile
48     rewind
49     content = _with_same_buffer_pos { @buffer.read }
50     begin
51       content.force_encoding(@encoding)
52     rescue ArgumentError # ex: unknown encoding name - utf
53       content
54     end
55   end
56 end
write(chunk)

writes the chunk into the buffer.

[show source]
   # File lib/httpx/response/buffer.rb
32 def write(chunk)
33   @bytesize += chunk.bytesize
34   try_upgrade_buffer
35   @buffer.write(chunk)
36 end