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
74 def ==(other)
75   super || begin
76     return false unless other.is_a?(Response::Buffer)
77 
78     if @buffer.nil?
79       other.buffer.nil?
80     elsif @buffer.respond_to?(:read) &&
81           other.respond_to?(:read)
82       buffer_pos = @buffer.pos
83       other_pos = other.buffer.pos
84       @buffer.rewind
85       other.buffer.rewind
86       begin
87         FileUtils.compare_stream(@buffer, other.buffer)
88       ensure
89         @buffer.pos = buffer_pos
90         other.buffer.pos = other_pos
91       end
92     else
93       to_s == other.to_s
94     end
95   end
96 end
close()

closes the buffer.

[show source]
   # File lib/httpx/response/buffer.rb
69 def close
70   @buffer.close
71   @buffer.unlink if @buffer.respond_to?(:unlink)
72 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)
33     end
34 end
size()

size in bytes of the buffered content.

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

returns the buffered content as a string.

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

writes the chunk into the buffer.

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