class Faraday::Response

Public Instance Methods

assert_2xx!() click to toggle source
# File lib/better-faraday.rb, line 40
def assert_2xx!
  return self if status_2xx?

  klass = if status_4xx?
    "Faraday::HTTP#{status}".safe_constantize || Faraday::HTTP4xx
  else
    Faraday::Error
  end

  error = klass.new("\n#{describe}")
  error.instance_variable_set(:@response, self)
  raise error
end
Also aliased as: ok!, assert_success!
assert_success!()
Alias for: assert_2xx!
describe() click to toggle source
# File lib/better-faraday.rb, line 73
def describe
  request_headers = __protect_data(env.request_headers.deep_dup)

  if env.request_headers["Content-Type"].to_s.match?(/\bapplication\/json\b/)
    request_json = __protect_data(__parse_json(env.request_body.dup))
  end

  if env.response_headers
    response_headers = __protect_data(env.response_headers.deep_dup)
  end

  if env.response_headers && env.response_headers["Content-Type"].to_s.match?(/\bapplication\/json\b/)
    response_json = __protect_data(__parse_json(env.body.dup))
  end

  lines = [
    "",
    "-- #{status} #{reason_phrase} --".upcase,
    "",
    "-- Request URL --",
    env.url.to_s,
    "",
    "-- Request method --",
    env.method.to_s.upcase,
    "",
    "-- Request headers --",
    ::JSON.generate(request_headers).yield_self { |t| t.truncate(2048, omission: "... (truncated, full length: #{t.length})") },
    "",

    "-- Request body --",
    if request_json
      ::JSON.generate(request_json)
    else
      body = env.request_body.to_s.dup
      if body.encoding.name == "ASCII-8BIT"
        "Binary (#{body.size} bytes)"
      else
        body
      end
    end.yield_self { |t| t.truncate(1024, omission: "... (truncated, full length: #{t.length})") },
    "",

    "-- Request sent at --",
    env.request_sent_at.strftime("%Y-%m-%d %H:%M:%S.%2N") + " UTC",
    "",

    "-- Response headers --",
    if response_headers
      ::JSON.generate(response_headers)
    else
      env.response_headers.to_s
    end.yield_self { |t| t.truncate(2048, omission: "... (truncated, full length: #{t.length})") },
    "",

    "-- Response body --",
    if response_json
      ::JSON.generate(response_json)
    else
      body = env.body.to_s.dup
      if body.encoding.name == "ASCII-8BIT"
        "Binary (#{body.size} bytes)"
      else
        body
      end
    end.yield_self { |t| t.truncate(2048, omission: "... (truncated, full length: #{t.length})") }
  ]

  if env.response_received_at
    lines.concat [
      "",
      "-- Response received at --",
      env.response_received_at.strftime("%Y-%m-%d %H:%M:%S.%2N") + " UTC",
      "",
      "-- Response received in --",
      "#{((env.response_received_at.to_f - env.request_sent_at.to_f) * 1000.0).round(2)}ms"
    ]
  end

  lines.join("\n") + "\n"
end
Also aliased as: inspect
inspect()
Alias for: describe
ok!()
Alias for: assert_2xx!
status_2xx?() click to toggle source
# File lib/better-faraday.rb, line 57
def status_2xx?
  status >= 200 && status <= 299
end
status_3xx?() click to toggle source
# File lib/better-faraday.rb, line 61
def status_3xx?
  status >= 300 && status <= 399
end
status_4xx?() click to toggle source
# File lib/better-faraday.rb, line 65
def status_4xx?
  status >= 400 && status <= 499
end
status_5xx?() click to toggle source
# File lib/better-faraday.rb, line 69
def status_5xx?
  status >= 500 && status <= 599
end

Private Instance Methods

__parse_json(json) click to toggle source
# File lib/better-faraday.rb, line 158
def __parse_json(json)
  return nil unless ::String === json
  data = ::JSON.parse(json)
  data if ::Hash === data || ::Array === data
rescue ::JSON::ParserError
  nil
end
__protect_data(data) click to toggle source
# File lib/better-faraday.rb, line 166
def __protect_data(data)
  return data.map(&method(:__protect_data)) if ::Array === data
  return data unless ::Hash === data
  data.each_with_object({}) do |(key, value), memo|
    memo[key] = if key.to_s.underscore.tr("_", " ").yield_self { |k| Faraday::Inspection.secrets.any? { |s| k.match?(s) } }
      "SECRET"
    else
      __protect_data(value)
    end
  end
end