lib/httpx/response.rb in httpx-0.9.0 vs lib/httpx/response.rb in httpx-0.10.0
- old
+ new
@@ -73,15 +73,15 @@
def no_data?
@status < 200 ||
@status == 204 ||
@status == 205 ||
@status == 304 || begin
- content_length = @headers["content-length"]
- return false if content_length.nil?
+ content_length = @headers["content-length"]
+ return false if content_length.nil?
- content_length == "0"
- end
+ content_length == "0"
+ end
end
class Body
def initialize(response, threshold_size:, window_size: 1 << 14)
@response = response
@@ -93,10 +93,12 @@
@buffer = nil
@state = :idle
end
def write(chunk)
+ return if @state == :closed
+
@length += chunk.bytesize
transition
@buffer.write(chunk)
end
@@ -114,11 +116,11 @@
def each
return enum_for(__method__) unless block_given?
begin
- unless @state == :idle
+ if @buffer
rewind
while (chunk = @buffer.read(@window_size))
yield(chunk.force_encoding(@encoding))
end
end
@@ -153,24 +155,23 @@
rewind
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
# closes/cleans the buffer, resets everything
def close
- return if @state == :idle
-
- @buffer.close
- @buffer.unlink if @buffer.respond_to?(:unlink)
- @buffer = nil
+ if @buffer
+ @buffer.close
+ @buffer.unlink if @buffer.respond_to?(:unlink)
+ @buffer = nil
+ end
@length = 0
- @state = :idle
+ @state = :closed
end
def ==(other)
to_s == other.to_s
end
@@ -184,11 +185,11 @@
# :nocov:
private
def rewind
- return if @state == :idle
+ return unless @buffer
@buffer.rewind
end
def transition
@@ -265,28 +266,20 @@
def status
@error.message
end
- def reason
- @error.class.name
- end
-
- def headers
- {}
- end
-
- def body
+ def to_s
@error.backtrace.join("\n")
end
def raise_for_status
raise @error
end
# rubocop:disable Style/MissingRespondToMissing
def method_missing(meth, *, &block)
- raise NoMethodError, "undefined response method `#{meth}' for error response" if Response.public_method_defined?(meth)
+ raise NoMethodError, "undefined response method `#{meth}' for error response" if @options.response_class.public_method_defined?(meth)
super
end
# rubocop:enable Style/MissingRespondToMissing
end