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. ==
  2. close
  3. initialize_dup
  4. size
  5. to_s
  6. write

Protected Instance

  1. buffer

Attributes

buffer [R]

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
15 def initialize(threshold_size:, bytesize: 0, encoding: Encoding::BINARY)
16   @threshold_size = threshold_size
17   @bytesize = bytesize
18   @encoding = encoding
19   @buffer = StringIO.new("".b)
20   super(@buffer)
21 end

Public Instance methods

==(other)
[show source]
   # File lib/httpx/response/buffer.rb
76 def ==(other)
77   super || begin
78     return false unless other.is_a?(Response::Buffer)
79 
80     buffer_pos = @buffer.pos
81     other_pos = other.buffer.pos
82     @buffer.rewind
83     other.buffer.rewind
84     begin
85       FileUtils.compare_stream(@buffer, other.buffer)
86     ensure
87       @buffer.pos = buffer_pos
88       other.buffer.pos = other_pos
89     end
90   end
91 end
close()

closes the buffer.

[show source]
   # File lib/httpx/response/buffer.rb
71 def close
72   @buffer.close
73   @buffer.unlink if @buffer.respond_to?(:unlink)
74 end
initialize_dup(other)
[show source]
   # File lib/httpx/response/buffer.rb
23 def initialize_dup(other)
24   super
25 
26   # create new descriptor in READ-ONLY mode
27   @buffer =
28     case other.buffer
29     when StringIO
30       StringIO.new(other.buffer.string, mode: File::RDONLY)
31     else
32       other.buffer.class.new(other.buffer.path, encoding: Encoding::BINARY, mode: File::RDONLY).tap do |temp|
33         FileUtils.copy_file(other.buffer.path, temp.path)
34       end
35     end
36 end
size()

size in bytes of the buffered content.

[show source]
   # File lib/httpx/response/buffer.rb
39 def size
40   @bytesize
41 end
to_s()

returns the buffered content as a string.

[show source]
   # File lib/httpx/response/buffer.rb
51 def to_s
52   case @buffer
53   when StringIO
54     begin
55       @buffer.string.force_encoding(@encoding)
56     rescue ArgumentError
57       @buffer.string
58     end
59   when Tempfile
60     rewind
61     content = @buffer.read
62     begin
63       content.force_encoding(@encoding)
64     rescue ArgumentError # ex: unknown encoding name - utf
65       content
66     end
67   end
68 end
write(chunk)

writes the chunk into the buffer.

[show source]
   # File lib/httpx/response/buffer.rb
44 def write(chunk)
45   @bytesize += chunk.bytesize
46   try_upgrade_buffer
47   @buffer.write(chunk)
48 end