Class: Tikkie::Api::V1::Responses::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tikkie/api/v1/responses/base.rb

Overview

Base class for all responses.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
19
20
# File 'lib/tikkie/api/v1/responses/base.rb', line 13

def initialize(response)
  if response.respond_to?(:body)
    @response = response
    @data = parse_body(response.body)
  else
    @data = response
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



11
12
13
# File 'lib/tikkie/api/v1/responses/base.rb', line 11

def data
  @data
end

#responseObject (readonly)

Returns the value of attribute response.



11
12
13
# File 'lib/tikkie/api/v1/responses/base.rb', line 11

def response
  @response
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/tikkie/api/v1/responses/base.rb', line 30

def error?
  !success?
end

#errorsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tikkie/api/v1/responses/base.rb', line 38

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

    if data[:errors]
      data[:errors].each do |error|
        errors << Tikkie::Api::V1::Responses::Error.new(error)
      end
    end

    errors
  end
end

#response_codeObject



22
23
24
# File 'lib/tikkie/api/v1/responses/base.rb', line 22

def response_code
  response.code.to_i if response
end

#success?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/tikkie/api/v1/responses/base.rb', line 26

def success?
  (response_code == 200 || response_code == 201) && !@invalid
end

#trace_idObject



34
35
36
# File 'lib/tikkie/api/v1/responses/base.rb', line 34

def trace_id
  response["Trace-Id"] if response
end