1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
# frozen_string_literal: true
module HTTParty
class Response < Object
def self.underscore(string)
string.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z])([A-Z])/, '\1_\2').downcase
end
def self._load(data)
req, resp, parsed_resp, resp_body = Marshal.load(data)
new(req, resp, -> { parsed_resp }, body: resp_body)
end
attr_reader :request, :response, :body, :headers
def initialize(request, response, parsed_block, options = {})
@request = request
@response = response
@body = options[:body] || response.body
@parsed_block = parsed_block
@headers = Headers.new(response.to_hash)
if request.options[:logger]
logger = ::HTTParty::Logger.build(
request.options[:logger],
request.options[:log_level],
request.options[:log_format]
)
logger.format(request, self)
end
throw_exception
end
def parsed_response
@parsed_response ||= @parsed_block.call
end
def code
response.code.to_i
end
def http_version
response.http_version
end
def tap
yield self
self
end
def inspect
inspect_id = ::Kernel::format '%x', (object_id * 2)
%(#<#{self.class}:0x#{inspect_id} parsed_response=#{parsed_response.inspect}, @response=#{response.inspect}, @headers=#{headers.inspect}>)
end
CODES_TO_OBJ = ::Net::HTTPResponse::CODE_CLASS_TO_OBJ.merge ::Net::HTTPResponse::CODE_TO_OBJ
CODES_TO_OBJ.each do |response_code, klass|
name = klass.name.sub('Net::HTTP', '')
name = "#{underscore(name)}?".to_sym
define_method(name) do
klass === response
end
end
# Support old multiple_choice? method from pre 2.0.0 era.
if ::RUBY_VERSION >= '2.0.0' && ::RUBY_PLATFORM != 'java'
alias_method :multiple_choice?, :multiple_choices?
end
# Support old status codes method from pre 2.6.0 era.
if ::RUBY_VERSION >= '2.6.0' && ::RUBY_PLATFORM != 'java'
alias_method :gateway_time_out?, :gateway_timeout?
alias_method :request_entity_too_large?, :payload_too_large?
alias_method :request_time_out?, :request_timeout?
alias_method :request_uri_too_long?, :uri_too_long?
alias_method :requested_range_not_satisfiable?, :range_not_satisfiable?
end
def nil?
warn_about_nil_deprecation
response.nil? || response.body.nil? || response.body.empty?
end
def to_s
if !response.nil? && !response.body.nil? && response.body.respond_to?(:to_s)
response.body.to_s
else
inspect
end
end
def pretty_print(pp)
if !parsed_response.nil? && parsed_response.respond_to?(:pretty_print)
parsed_response.pretty_print(pp)
else
super
end
end
def display(port=$>)
if !parsed_response.nil? && parsed_response.respond_to?(:display)
parsed_response.display(port)
elsif !response.nil? && !response.body.nil? && response.body.respond_to?(:display)
response.body.display(port)
else
port.write(inspect)
end
end
def respond_to_missing?(name, *args)
return true if super
parsed_response.respond_to?(name) || response.respond_to?(name)
end
def _dump(_level)
Marshal.dump([request, response, parsed_response, body])
end
protected
def method_missing(name, *args, &block)
if parsed_response.respond_to?(name)
parsed_response.send(name, *args, &block)
elsif response.respond_to?(name)
response.send(name, *args, &block)
else
super
end
end
def throw_exception
if @request.options[:raise_on] && @request.options[:raise_on].include?(code)
::Kernel.raise ::HTTParty::ResponseError.new(@response), "Code #{code} - #{body}"
end
end
private
def warn_about_nil_deprecation
trace_line = caller.reject { |line| line.include?('httparty') }.first
warning = "[DEPRECATION] HTTParty will no longer override `response#nil?`. " \
"This functionality will be removed in future versions. " \
"Please, add explicit check `response.body.nil? || response.body.empty?`. " \
"For more info refer to: https://github.com/jnunemaker/httparty/issues/568\n" \
"#{trace_line}"
warn(warning)
end
end
end
require 'httparty/response/headers'
|