Class: HTTPX::Response::Buffer

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/httpx/response/buffer.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(threshold_size:, bytesize: 0, encoding: Encoding::BINARY) ⇒ Buffer

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

[View source]

15
16
17
18
19
20
21
# File 'lib/httpx/response/buffer.rb', line 15

def initialize(threshold_size:, bytesize: 0, encoding: Encoding::BINARY)
  @threshold_size = threshold_size
  @bytesize = bytesize
  @encoding = encoding
  @buffer = StringIO.new("".b)
  super(@buffer)
end

Instance Method Details

#==(other) ⇒ Object

[View source]

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/httpx/response/buffer.rb', line 74

def ==(other)
  super || begin
    return false unless other.is_a?(Response::Buffer)

    if @buffer.nil?
      other.buffer.nil?
    elsif @buffer.respond_to?(:read) &&
          other.respond_to?(:read)
      buffer_pos = @buffer.pos
      other_pos = other.buffer.pos
      @buffer.rewind
      other.buffer.rewind
      begin
        FileUtils.compare_stream(@buffer, other.buffer)
      ensure
        @buffer.pos = buffer_pos
        other.buffer.pos = other_pos
      end
    else
      to_s == other.to_s
    end
  end
end

#closeObject

closes the buffer.

[View source]

69
70
71
72
# File 'lib/httpx/response/buffer.rb', line 69

def close
  @buffer.close
  @buffer.unlink if @buffer.respond_to?(:unlink)
end

#initialize_dup(other) ⇒ Object

[View source]

23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/httpx/response/buffer.rb', line 23

def initialize_dup(other)
  super

  # create new descriptor in READ-ONLY mode
  @buffer =
    case other.buffer
    when StringIO
      StringIO.new(other.buffer.string, mode: File::RDONLY)
    else
      other.buffer.class.new(other.buffer.path, encoding: Encoding::BINARY, mode: File::RDONLY)
    end
end

#sizeObject

size in bytes of the buffered content.

[View source]

37
38
39
# File 'lib/httpx/response/buffer.rb', line 37

def size
  @bytesize
end

#to_sObject

returns the buffered content as a string.

[View source]

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/httpx/response/buffer.rb', line 49

def to_s
  case @buffer
  when StringIO
    begin
      @buffer.string.force_encoding(@encoding)
    rescue ArgumentError
      @buffer.string
    end
  when Tempfile
    rewind
    content = @buffer.read
    begin
      content.force_encoding(@encoding)
    rescue ArgumentError # ex: unknown encoding name - utf
      content
    end
  end
end

#write(chunk) ⇒ Object

writes the chunk into the buffer.

[View source]

42
43
44
45
46
# File 'lib/httpx/response/buffer.rb', line 42

def write(chunk)
  @bytesize += chunk.bytesize
  try_upgrade_buffer
  @buffer.write(chunk)
end