Class: Grist::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data: nil, error: nil, code: nil, type: nil) ⇒ Response



7
8
9
10
11
12
# File 'lib/grist/response.rb', line 7

def initialize(data: nil, error: nil, code: nil, type: nil)
  @data = data
  @error = error
  @type = type
  @code = code&.to_i
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/grist/response.rb', line 5

def code
  @code
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/grist/response.rb', line 5

def data
  @data
end

#errorObject (readonly)

Returns the value of attribute error.



5
6
7
# File 'lib/grist/response.rb', line 5

def error
  @error
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/grist/response.rb', line 5

def type
  @type
end

Instance Method Details

#error?Boolean



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

def error?
  !error.nil? || !(@code >= 200 && @code < 400)
end

#log_errorObject



33
34
35
36
37
# File 'lib/grist/response.rb', line 33

def log_error
  return unless error?

  Grist.logger.error("Grist API Error: #{@type} (code: #{@code})- #{@error}")
end

#not_found?Boolean



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

def not_found?
  @code == 404
end


26
27
28
29
30
31
# File 'lib/grist/response.rb', line 26

def print_error
  err = "#{@type}: #{@error}"
  err += " (code: #{@code})" if @code
  err += "\n#{@type.help}" if @type.respond_to?(:help) && !@type.help.nil?
  err
end

#success?Boolean



14
15
16
# File 'lib/grist/response.rb', line 14

def success?
  error.nil? && (@code >= 200 && @code < 400)
end