Sha256: 6d71dc13a5f2067108a05b52c36286a9e67be23f00f741752171fda60007cf6f
Contents?: true
Size: 1.58 KB
Versions: 1
Compression:
Stored size: 1.58 KB
Contents
require 'json' require 'instapaper/error' module Instapaper module HTTP class Response attr_reader :response, :raw_format, :path # TODO: Change this to a keyword argument (needs a major version bump) def initialize(response, path, raw_format = false) # rubocop:disable Style/OptionalBooleanParameter @response = response @path = path @raw_format = raw_format end def body raw_format ? response.to_s : parsed end def valid? !error? end def error? fail_if_body_unparseable unless raw_format fail_if_body_contains_error fail_if_http_error end private def parsed @parsed ||= begin response.parse(:json) rescue StandardError response.body end end 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 def fail_if_body_unparseable response.parse(:json) rescue JSON::ParserError raise Instapaper::Error::ServiceUnavailableError end 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 end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
instapaper-1.0.1 | lib/instapaper/http/response.rb |