Class: Appfuel::Response

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

Overview

Every action or command must return a response. A response is either ok or it has errors. You can retrieve the results with the “ok” method or the errors with the “error” method

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Response

Parameters:

  • data (Hash) (defaults to: {})


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/appfuel/response.rb', line 65

def initialize(data = {})
  result = format_result_hash(data)
  # when no ok key and no errors key then assume
  # it is a successfull response
  @ok = nil
  @errors = nil
  if self.class.ok_key?(result)
    @ok = self.class.ok_data(result)
  elsif self.class.error_key?(result)
    @errors = Errors.new(self.class.error_data(result))
  else
    @ok = result
  end
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



61
62
63
# File 'lib/appfuel/response.rb', line 61

def errors
  @errors
end

#okObject (readonly)

Returns the value of attribute ok.



61
62
63
# File 'lib/appfuel/response.rb', line 61

def ok
  @ok
end

Class Method Details

.error(data) ⇒ Object

Convience method for creating an error response. It understands how to handle a SpCore::Error object. Any thing that is not a hash or can’t be converted to a hash is assumed to be a string and converted into a general_error

Parameters:

  • data

    Hash the errors hash



42
43
44
45
46
# File 'lib/appfuel/response.rb', line 42

def error(data)
  result = format_result_hash(data, default_key: :general_error)
  result = result[:errors] if result.key?(:errors)
  self.new(errors: result)
end

.error_data(data) ⇒ Object



31
32
33
# File 'lib/appfuel/response.rb', line 31

def error_data(data)
  data[:errors] || data['errors']
end

.error_key?(data) ⇒ Boolean

Returns:

  • (Boolean)


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

def error_key?(data)
  return false unless data.is_a?(Hash)
  data.key?(:errors) || data.key?("errors")
end

.format_result_hash(data, default_key:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/appfuel/response.rb', line 48

def format_result_hash(data, default_key:)
  if data.is_a?(Hash)
    result = data
  elsif data.respond_to?(:to_h)
    result = data.to_h
  else
    result = {default_key => data.to_s}
  end

  result.symbolize_keys
end

.ok(result = nil) ⇒ Object

Convience method for creating a successfull response

Parameters:

  • result (defaults to: nil)

    Hash the successfull resultset



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

def ok(result = nil)
  result = ok_data(result) if ok_key?(result)
  self.new(ok: result)
end

.ok_data(data) ⇒ Object



27
28
29
# File 'lib/appfuel/response.rb', line 27

def ok_data(data)
  data[:ok] || data['ok']
end

.ok_key?(data) ⇒ Boolean

Returns:

  • (Boolean)


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

def ok_key?(data)
  return false unless data.is_a?(Hash)
  data.key?(:ok) || data.key?("ok")
end

Instance Method Details

#error_messagesObject



85
86
87
88
89
# File 'lib/appfuel/response.rb', line 85

def error_messages
  return {} if ok?

  errors.messages
end

#errors?Boolean Also known as: failure?

Returns:

  • (Boolean)


80
81
82
# File 'lib/appfuel/response.rb', line 80

def errors?
  !ok?
end

#ok?Boolean Also known as: success?

Returns:

  • (Boolean)


91
92
93
# File 'lib/appfuel/response.rb', line 91

def ok?
  errors.nil?
end

#to_hObject



96
97
98
99
100
101
102
# File 'lib/appfuel/response.rb', line 96

def to_h
  if ok?
    {ok: ok}
  else
    errors.to_h
  end
end

#to_jsonObject



104
105
106
# File 'lib/appfuel/response.rb', line 104

def to_json
  to_h.to_json
end