Class: EmailOctopus::API::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/email_octopus/api/response.rb

Overview

Response object that parses out JSON.

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response

Raises:


9
10
11
12
# File 'lib/email_octopus/api/response.rb', line 9

def initialize(response)
  @raw = response
  raise error_class, self if error?
end

Instance Method Details

#bodyObject


30
31
32
# File 'lib/email_octopus/api/response.rb', line 30

def body
  JSON.parse @raw.body
end

#error?Boolean


26
27
28
# File 'lib/email_octopus/api/response.rb', line 26

def error?
  !success?
end

#error_classObject


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/email_octopus/api/response.rb', line 34

def error_class
  return unless error?
  case body['code']
  when 'INVALID_PARAMETERS'
    Error::InvalidParameters
  when 'API_KEY_INVALID'
    Error::ApiKeyInvalid
  when 'UNAUTHORISED'
    Error::Unauthorized
  when 'NOT_FOUND'
    Error::NotFound
  else
    Error
  end
end

#headersObject


18
19
20
# File 'lib/email_octopus/api/response.rb', line 18

def headers
  @raw.headers
end

#statusObject


14
15
16
# File 'lib/email_octopus/api/response.rb', line 14

def status
  @raw.status
end

#success?Boolean


22
23
24
# File 'lib/email_octopus/api/response.rb', line 22

def success?
  @raw.is_a? Net::HTTPSuccess
end