Sha256: 02de542c5eb56e39d2e230fa2aa3477d07137d4b2e79672850cc0872a9f7c19a
Contents?: true
Size: 1.45 KB
Versions: 1
Compression:
Stored size: 1.45 KB
Contents
require 'json' require 'instapaper/error' module Instapaper module HTTP class Response attr_reader :response, :raw_format, :path def initialize(response, path, raw_format = false) @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_response ||= begin response.parse(:json) rescue 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.0 | lib/instapaper/http/response.rb |