Class: Kentaa::Api::Response

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

Constant Summary collapse

SUCCESS_CODES =
[200, 201, 204].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



12
13
14
15
# File 'lib/kentaa/api/response.rb', line 12

def initialize(response)
  @response = response
  @body = response.body ? parse_body(response.body) : {}
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/kentaa/api/response.rb', line 21

def error?
  !success?
end

#errorsObject



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

def errors
  @errors ||= begin
    errors = []

    if body[:errors]
      body[:errors].each do |error|
        errors << Kentaa::Api::Resources::Error.new(error)
      end
    end

    errors
  end
end

#http_codeObject



25
26
27
# File 'lib/kentaa/api/response.rb', line 25

def http_code
  response.code.to_i
end

#messageObject



33
34
35
# File 'lib/kentaa/api/response.rb', line 33

def message
  body[:message]
end

#request_uriObject



29
30
31
# File 'lib/kentaa/api/response.rb', line 29

def request_uri
  response.uri
end

#success?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/kentaa/api/response.rb', line 17

def success?
  SUCCESS_CODES.include?(http_code) && !message
end