# frozen_string_literal: truemoduleHTTPartyclassResponse<Objectdefself.underscore(string)string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcaseenddefself._load(data)req,resp,parsed_resp,resp_body=Marshal.load(data)new(req,resp,->{parsed_resp},body: resp_body)endattr_reader:request,:response,:body,:headersdefinitialize(request,response,parsed_block,options={})@request=request@response=response@body=options[:body]||response.body@parsed_block=parsed_block@headers=Headers.new(response.to_hash)ifrequest.options[:logger]logger=::HTTParty::Logger.build(request.options[:logger],request.options[:log_level],request.options[:log_format])logger.format(request,self)endthrow_exceptionenddefparsed_response@parsed_response||=@parsed_block.callenddefcoderesponse.code.to_ienddefhttp_versionresponse.http_versionenddeftapyieldselfselfenddefinspectinspect_id=::Kernel::format'%x',(object_id*2)%(#<#{self.class}:0x#{inspect_id} parsed_response=#{parsed_response.inspect}, @response=#{response.inspect}, @headers=#{headers.inspect}>)endCODES_TO_OBJ=::Net::HTTPResponse::CODE_CLASS_TO_OBJ.merge::Net::HTTPResponse::CODE_TO_OBJCODES_TO_OBJ.eachdo|response_code,klass|name=klass.name.sub('Net::HTTP','')name="#{underscore(name)}?".to_symdefine_method(name)doklass===responseendend# Support old multiple_choice? method from pre 2.0.0 era.if::RUBY_PLATFORM!='java'alias_method:multiple_choice?,:multiple_choices?end# Support old status codes method from pre 2.6.0 era.if::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?enddefnil?warn_about_nil_deprecationresponse.nil?||response.body.nil?||response.body.empty?enddefto_sif!response.nil?&&!response.body.nil?&&response.body.respond_to?(:to_s)response.body.to_selseinspectendenddefpretty_print(pp)if!parsed_response.nil?&&parsed_response.respond_to?(:pretty_print)parsed_response.pretty_print(pp)elsesuperendenddefdisplay(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)elseport.write(inspect)endenddefrespond_to_missing?(name,*args)returntrueifsuperparsed_response.respond_to?(name)||response.respond_to?(name)enddef_dump(_level)Marshal.dump([request,response,parsed_response,body])endprotecteddefmethod_missing(name,*args,&block)ifparsed_response.respond_to?(name)parsed_response.send(name,*args,&block)elsifresponse.respond_to?(name)response.send(name,*args,&block)elsesuperendenddefthrow_exceptionif@request.options[:raise_on].to_a.detect{|c|code.to_s.match(/#{c.to_s}/)}::Kernel.raise::HTTParty::ResponseError.new(@response),"Code #{code} - #{body}"endendprivatedefwarn_about_nil_deprecationtrace_line=caller.reject{|line|line.include?('httparty')}.firstwarning="[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)endendendrequire'httparty/response/headers'