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

Inherits:
Object
  • Object
show all
Defined in:
lib/tikkie/api/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.



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

def initialize(response)
  if response.respond_to?(:body)
    @response = response
    @data = JSON.parse(response.body, symbolize_names: true)
  else
    @data = response
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


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

def error?
  !success?
end

#errorsObject



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

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

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

    errors
  end
end

#response_codeObject



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

def response_code
  response.code.to_i if response
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  response_code == 200 || response_code == 201
end

#trace_idObject



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

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