lib/httpx/response.rb in httpx-0.20.5 vs lib/httpx/response.rb in httpx-0.21.0

- old
+ new

@@ -29,29 +29,39 @@ @options = request.options @version = version @status = Integer(status) @headers = @options.headers_class.new(headers) @body = @options.response_body_class.new(self, @options) + @finished = complete? end def merge_headers(h) @headers = @headers.merge(h) end def <<(data) @body.write(data) end + def content_type + @content_type ||= ContentType.new(@headers["content-type"]) + end + + def finished? + @finished + end + + def finish! + @finished = true + @headers.freeze + end + def bodyless? @request.verb == :head || no_data? end - def content_type - @content_type ||= ContentType.new(@headers["content-type"]) - end - def complete? bodyless? || (@request.verb == :connect && @status == 200) end # :nocov: @@ -74,37 +84,41 @@ return self unless (err = error) raise err end - def json(options = nil) - decode("json", options) + def json(*args) + decode("json", *args) end def form decode("form") end + def xml + decode("xml") + end + private - def decode(format, options = nil) + def decode(format, *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 - decoder.call(self, options) + decoder.call(self, *args) rescue Registry::Error raise Error, "no decoder available for \"#{format}\"" end def no_data? - @status < 200 || + @status < 200 || # informational response @status == 204 || @status == 205 || @status == 304 || begin content_length = @headers["content-length"] return false if content_length.nil? @@ -335,9 +349,13 @@ else def to_s "#{@error.message} (#{@error.class})\n" \ "#{@error.backtrace.join("\n") if @error.backtrace}" end + end + + def finished? + true end def raise_for_status raise @error end