Class: CampaignHQ::API::Response

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

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.

Raises:



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

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

Instance Method Details

#bodyObject



34
35
36
# File 'lib/campaign_h_q/api/response.rb', line 34

def body
  JSON.parse @raw.body
end

#error?Boolean



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

def error?
  !success?
end

#error_classObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/campaign_h_q/api/response.rb', line 38

def error_class
  return unless error?
  case body['error']
  when 'unprocessable_entity'
    Error::InvalidParameters
  when 'unauthorized'
    Error::Unauthorized
  when 'not_found'
    Error::NotFound
  else
    Error
  end
end

#headersObject



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

def headers
  @raw.headers
end

#statusObject



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

def status
  @raw.status
end

#success?Boolean



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

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