Class: Appfuel::Response
- Inherits:
-
Object
- Object
- Appfuel::Response
- 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
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#ok ⇒ Object
readonly
Returns the value of attribute ok.
Class Method Summary collapse
-
.error(data) ⇒ Object
Convience method for creating an error response.
- .error_data(data) ⇒ Object
- .error_key?(data) ⇒ Boolean
- .format_result_hash(data, default_key:) ⇒ Object
-
.ok(result = nil) ⇒ Object
Convience method for creating a successfull response.
- .ok_data(data) ⇒ Object
- .ok_key?(data) ⇒ Boolean
Instance Method Summary collapse
- #error_messages ⇒ Object
- #errors? ⇒ Boolean (also: #failure?)
- #initialize(data = {}) ⇒ Response constructor
- #ok? ⇒ Boolean (also: #success?)
- #to_h ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(data = {}) ⇒ Response
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
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
61 62 63 |
# File 'lib/appfuel/response.rb', line 61 def errors @errors end |
#ok ⇒ Object (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
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
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
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
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_messages ⇒ Object
85 86 87 88 89 |
# File 'lib/appfuel/response.rb', line 85 def return {} if ok? errors. end |
#errors? ⇒ Boolean Also known as: failure?
80 81 82 |
# File 'lib/appfuel/response.rb', line 80 def errors? !ok? end |
#ok? ⇒ Boolean Also known as: success?
91 92 93 |
# File 'lib/appfuel/response.rb', line 91 def ok? errors.nil? end |
#to_h ⇒ Object
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_json ⇒ Object
104 105 106 |
# File 'lib/appfuel/response.rb', line 104 def to_json to_h.to_json end |