class Instapaper::HTTP::Response

Attributes

path[R]
raw_format[R]
response[R]

Public Class Methods

new(response, path, raw_format = false) click to toggle source
# File lib/instapaper/http/response.rb, line 8
def initialize(response, path, raw_format = false)
  @response = response
  @path = path
  @raw_format = raw_format
end

Public Instance Methods

body() click to toggle source
# File lib/instapaper/http/response.rb, line 14
def body
  raw_format ? response.to_s : parsed
end
error?() click to toggle source
# File lib/instapaper/http/response.rb, line 22
def error?
  fail_if_body_unparseable unless raw_format
  fail_if_body_contains_error
  fail_if_http_error
end
valid?() click to toggle source
# File lib/instapaper/http/response.rb, line 18
def valid?
  !error?
end

Private Instance Methods

fail_if_body_contains_error() click to toggle source
# File lib/instapaper/http/response.rb, line 54
def fail_if_body_contains_error
  return unless parsed.is_a?(Array)
  return if parsed.empty?
  return unless parsed.first['type'] == 'error'

  raise Instapaper::Error.from_response(parsed.first['error_code'], @path)
end
fail_if_body_unparseable() click to toggle source
# File lib/instapaper/http/response.rb, line 48
def fail_if_body_unparseable
  response.parse(:json)
rescue JSON::ParserError
  raise Instapaper::Error::ServiceUnavailableError
end
fail_if_http_error() click to toggle source
# File lib/instapaper/http/response.rb, line 38
def fail_if_http_error
  return if response.status.ok?

  if Instapaper::Error::CODES.include?(response.status.code) # rubocop:disable Style/GuardClause
    raise Instapaper::Error.from_response(response.status.code, path)
  else
    raise Instapaper::Error, 'Unknown Error'
  end
end
parsed() click to toggle source
# File lib/instapaper/http/response.rb, line 30
def parsed
  @parsed_response ||= begin
    response.parse(:json)
  rescue
    response.body
  end
end