lib/httpx/response.rb in httpx-0.3.1 vs lib/httpx/response.rb in httpx-0.4.0
- old
+ new
@@ -19,14 +19,14 @@
def_delegator :@body, :close
def_delegator :@request, :uri
- def initialize(request, status, version, headers, options = {})
- @options = Options.new(options)
- @version = version
+ def initialize(request, status, version, headers)
@request = request
+ @options = request.options
+ @version = version
@status = Integer(status)
@headers = @options.headers_class.new(headers)
@body = @options.response_body_class.new(self, threshold_size: @options.body_threshold_size,
window_size: @options.window_size)
end
@@ -56,10 +56,11 @@
"#<Response:#{object_id} @status=#{@status} @headers=#{@headers}>"
end
def raise_for_status
return if @status < 400
+
raise HTTPError, self
end
private
@@ -68,10 +69,11 @@
@status == 204 ||
@status == 205 ||
@status == 304 || begin
content_length = @headers["content-length"]
return false if content_length.nil?
+
content_length == "0"
end
end
class Body
@@ -92,19 +94,21 @@
@buffer.write(chunk)
end
def read(*args)
return unless @buffer
+
@buffer.read(*args)
end
def bytesize
@length
end
def each
return enum_for(__method__) unless block_given?
+
begin
unless @state == :idle
rewind
while (chunk = @buffer.read(@window_size))
yield(chunk)
@@ -135,10 +139,11 @@
@length.zero?
end
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)
@@ -146,10 +151,11 @@
end
# closes/cleans the buffer, resets everything
def close
return if @state == :idle
+
@buffer.close
@buffer.unlink if @buffer.respond_to?(:unlink)
@buffer = nil
@length = 0
@state = :idle
@@ -161,10 +167,11 @@
private
def rewind
return if @state == :idle
+
@buffer.rewind
end
def transition
case @state
@@ -180,14 +187,13 @@
if @length > @threshold_size
aux = @buffer
@buffer = Tempfile.new("httpx", encoding: @encoding, mode: File::RDWR)
aux.rewind
::IO.copy_stream(aux, @buffer)
- # TODO: remove this if/when minor ruby is 2.3
- # (this looks like a bug from older versions)
- @buffer.pos = aux.pos #######################
- #############################################
+ # (this looks like a bug from Ruby < 2.3
+ @buffer.pos = aux.pos ##################
+ ########################################
aux.close
@state = :buffer
end
end
@@ -195,12 +201,12 @@
end
end
end
class ContentType
- MIME_TYPE_RE = %r{^([^/]+/[^;]+)(?:$|;)}
- CHARSET_RE = /;\s*charset=([^;]+)/i
+ MIME_TYPE_RE = %r{^([^/]+/[^;]+)(?:$|;)}.freeze
+ CHARSET_RE = /;\s*charset=([^;]+)/i.freeze
attr_reader :mime_type, :charset
def initialize(mime_type, charset)
@mime_type = mime_type
@@ -235,17 +241,25 @@
attr_reader :error
def initialize(error, options)
@error = error
@options = Options.new(options)
- log { "#{error.class}: #{error}" }
+ log_exception(@error)
end
def status
@error.message
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)
+
+ super
+ end
+ # rubocop:enable Style/MissingRespondToMissing
end
end