lib/httpx/response.rb in httpx-0.22.5 vs lib/httpx/response.rb in httpx-0.23.0

- old
+ new

@@ -54,16 +54,16 @@ @finished = true @headers.freeze end def bodyless? - @request.verb == :head || + @request.verb == "HEAD" || no_data? end def complete? - bodyless? || (@request.verb == :connect && @status == 200) + bodyless? || (@request.verb == "CONNECT" && @status == 200) end # :nocov: def inspect "#<Response:#{object_id} " \ @@ -85,36 +85,31 @@ raise err end def json(*args) - decode("json", *args) + decode(Transcoder::JSON, *args) end def form - decode("form") + decode(Transcoder::Form) end def xml - decode("xml") + decode(Transcoder::Xml) end private - def decode(format, *args) + def decode(transcoder, *args) # TODO: check if content-type is a valid format, i.e. "application/json" for json parsing - transcoder = Transcoder.registry(format) - raise Error, "no decoder available for \"#{format}\"" unless transcoder.respond_to?(:decode) - decoder = transcoder.decode(self) - raise Error, "no decoder available for \"#{format}\"" unless decoder + raise Error, "no decoder available for \"#{transcoder}\"" unless decoder decoder.call(self, *args) - rescue Registry::Error - raise Error, "no decoder available for \"#{format}\"" end def no_data? @status < 200 || # informational response @status == 204 || @@ -201,14 +196,12 @@ begin content.force_encoding(@encoding) rescue ArgumentError # ex: unknown encoding name - utf content end - when nil - "".b else - @buffer + "".b end end alias_method :to_str, :to_s def empty? @@ -332,16 +325,17 @@ class ErrorResponse include Loggable extend Forwardable - attr_reader :request, :error + attr_reader :request, :response, :error def_delegator :@request, :uri def initialize(request, error, options) @request = request + @response = request.response if request.response.is_a?(Response) @error = error @options = Options.new(options) log_exception(@error) end @@ -357,9 +351,13 @@ else def to_s "#{@error.message} (#{@error.class})\n" \ "#{@error.backtrace.join("\n") if @error.backtrace}" end + end + + def close + @response.close if @response.respond_to?(:close) end def finished? true end