Class: Tikkie::Api::Response

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

Overview

Parses and wraps the response from the Tikkie API.

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/tikkie/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/tikkie/api/response.rb', line 10

def body
  @body
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


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

def error?
  !success?
end

#errorsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tikkie/api/response.rb', line 41

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

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

    errors
  end
end

#http_codeObject



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

def http_code
  response.code.to_i
end

#http_messageObject



37
38
39
# File 'lib/tikkie/api/response.rb', line 37

def http_message
  response.message
end

#invalid?Boolean

Returns:

  • (Boolean)


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

def invalid?
  body.nil?
end

#request_uriObject



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

def request_uri
  response.uri
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  http_code == 200 || http_code == 201 || http_code == 204
end