class HTTParty::Response

Constants

CODES_TO_OBJ

Attributes

body[R]
headers[R]
request[R]
response[R]

Public Class Methods

_load(data) click to toggle source
# File lib/httparty/response.rb, line 7
def self._load(data)
  req, resp, parsed_resp, resp_body = Marshal.load(data)

  new(req, resp, -> { parsed_resp }, body: resp_body)
end
new(request, response, parsed_block, options = {}) click to toggle source
# File lib/httparty/response.rb, line 15
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
underscore(string) click to toggle source
# File lib/httparty/response.rb, line 3
def self.underscore(string)
  string.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z])([A-Z])/, '\1_\2').downcase
end

Public Instance Methods

_dump(_level) click to toggle source
# File lib/httparty/response.rb, line 117
def _dump(_level)
  Marshal.dump([request, response, parsed_response, body])
end
code() click to toggle source
# File lib/httparty/response.rb, line 38
def code
  response.code.to_i
end
display(port=$>) click to toggle source
# File lib/httparty/response.rb, line 102
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
http_version() click to toggle source
# File lib/httparty/response.rb, line 42
def http_version
  response.http_version
end
inspect() click to toggle source
# File lib/httparty/response.rb, line 51
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
nil?() click to toggle source
# File lib/httparty/response.rb, line 81
def nil?
  warn_about_nil_deprecation
  response.nil? || response.body.nil? || response.body.empty?
end
parsed_response() click to toggle source
# File lib/httparty/response.rb, line 34
def parsed_response
  @parsed_response ||= @parsed_block.call
end
pretty_print(pp) click to toggle source
Calls superclass method
# File lib/httparty/response.rb, line 94
def pretty_print(pp)
  if !parsed_response.nil? && parsed_response.respond_to?(:pretty_print)
    parsed_response.pretty_print(pp)
  else
    super
  end
end
respond_to_missing?(name, *args) click to toggle source
Calls superclass method
# File lib/httparty/response.rb, line 112
def respond_to_missing?(name, *args)
  return true if super
  parsed_response.respond_to?(name) || response.respond_to?(name)
end
tap() { |self| ... } click to toggle source
# File lib/httparty/response.rb, line 46
def tap
  yield self
  self
end
to_s() click to toggle source
# File lib/httparty/response.rb, line 86
def to_s
  if !response.nil? && !response.body.nil? && response.body.respond_to?(:to_s)
    response.body.to_s
  else
    inspect
  end
end

Protected Instance Methods

method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/httparty/response.rb, line 123
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
throw_exception() click to toggle source
# File lib/httparty/response.rb, line 133
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 Instance Methods

warn_about_nil_deprecation() click to toggle source
# File lib/httparty/response.rb, line 141
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