Class: AlchemyApi::Base

Inherits:
MonsterMash::Base
  • Object
show all
Defined in:
lib/alchemy_api/base.rb

Class Method Summary collapse

Class Method Details

.check_json_for_errors_and_raise!(json) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/alchemy_api/base.rb', line 16

def self.check_json_for_errors_and_raise!(json)
  if json['status'] == 'ERROR'
    case json['statusInfo']
    when 'invalid-api-key'
      raise InvalidApiKeyError, "The API key you sent (#{AlchemyApi.api_key.inspect}) is invalid! Please set AlchemyApi.api_key!"
    when 'cannot-retrieve'
      raise CannotRetrieveUrlError, "The URL (#{json['url']}) could not be retrieved."
    when 'cannot-retrieve:http-redirect-limit'
      raise RedirectionLimitError, "The URL (#{json['url']}) could not be retrieved, as it reached a redirect limit."
    when 'page-is-not-html'
      raise PageIsNotValidHtmlError, "The page at #{json['url']} is not valid HTML!"
    when 'content-exceeds-size-limit'
      raise ContentExceedsMaxLimitError, "The page at #{json['url']} is larger than 600KB!"
    when 'invalid-html'
      raise InvalidHtmlError, "The HTML sent was invalid!"
    else
      raise UnknownError, "Got an unknown error: #{json['statusInfo']}"
    end
  end
end

.get_json(response) ⇒ Object



10
11
12
13
14
# File 'lib/alchemy_api/base.rb', line 10

def self.get_json(response)
  json = JSON.parse(response.body)
  check_json_for_errors_and_raise!(json)
  json
end