Class: HTTPX::Response::Body
- Inherits:
-
Object
- Object
- HTTPX::Response::Body
- Defined in:
- lib/httpx/response.rb
Instance Method Summary collapse
- #==(other) ⇒ Object
- #bytesize ⇒ Object
-
#close ⇒ Object
closes/cleans the buffer, resets everything.
- #copy_to(dest) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(response, threshold_size:, window_size: 1 << 14) ⇒ Body
constructor
A new instance of Body.
- #read(*args) ⇒ Object
- #to_s ⇒ Object (also: #to_str)
- #write(chunk) ⇒ Object
Constructor Details
#initialize(response, threshold_size:, window_size: 1 << 14) ⇒ Body
Returns a new instance of Body.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/httpx/response.rb', line 64 def initialize(response, threshold_size:, window_size: 1 << 14) @response = response @headers = response.headers @threshold_size = threshold_size @window_size = window_size @encoding = response.content_type.charset || Encoding::BINARY @length = 0 @buffer = nil @state = :idle end |
Instance Method Details
#==(other) ⇒ Object
137 138 139 |
# File 'lib/httpx/response.rb', line 137 def ==(other) to_s == other.to_s end |
#bytesize ⇒ Object
86 87 88 |
# File 'lib/httpx/response.rb', line 86 def bytesize @length end |
#close ⇒ Object
closes/cleans the buffer, resets everything
128 129 130 131 132 133 134 135 |
# File 'lib/httpx/response.rb', line 128 def close return if @state == :idle @buffer.close @buffer.unlink if @buffer.respond_to?(:unlink) @buffer = nil @length = 0 @state = :idle end |
#copy_to(dest) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/httpx/response.rb', line 117 def copy_to(dest) return unless @buffer if dest.respond_to?(:path) && @buffer.respond_to?(:path) FileUtils.mv(@buffer.path, dest.path) else @buffer.rewind ::IO.copy_stream(@buffer, dest) end end |
#each ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/httpx/response.rb', line 90 def each return enum_for(__method__) unless block_given? begin unless @state == :idle rewind while (chunk = @buffer.read(@window_size)) yield(chunk) end end ensure close end end |
#empty? ⇒ Boolean
113 114 115 |
# File 'lib/httpx/response.rb', line 113 def empty? @length.zero? end |
#read(*args) ⇒ Object
81 82 83 84 |
# File 'lib/httpx/response.rb', line 81 def read(*args) return unless @buffer @buffer.read(*args) end |
#to_s ⇒ Object Also known as: to_str
104 105 106 107 108 109 110 |
# File 'lib/httpx/response.rb', line 104 def to_s rewind return @buffer.read.force_encoding(@encoding) if @buffer "" ensure close end |
#write(chunk) ⇒ Object
75 76 77 78 79 |
# File 'lib/httpx/response.rb', line 75 def write(chunk) @length += chunk.bytesize transition @buffer.write(chunk) end |