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.
- .format_result_hash(data, default_key:) ⇒ Object
-
.ok(result = nil) ⇒ Object
Convience method for creating a successfull response.
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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/appfuel/response.rb', line 46 def initialize(data = {}) result = format_result_hash(data) # when no ok key and no errors key the assume # it is a successfull response if !result.key?(:ok) && !result.key?(:errors) result = {ok: result} end @ok = result[:ok] @errors = nil if result.key?(:errors) @ok = nil @errors = Errors.new(result[:errors]) end end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
42 43 44 |
# File 'lib/appfuel/response.rb', line 42 def errors @errors end |
#ok ⇒ Object (readonly)
Returns the value of attribute ok.
42 43 44 |
# File 'lib/appfuel/response.rb', line 42 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
23 24 25 26 27 |
# File 'lib/appfuel/response.rb', line 23 def error(data) result = format_result_hash(data, default_key: :general_error) result = result[:errors] if result.key?(:errors) self.new(errors: result) end |
.format_result_hash(data, default_key:) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/appfuel/response.rb', line 29 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 |
# File 'lib/appfuel/response.rb', line 12 def ok(result = nil) self.new(ok: result) end |
Instance Method Details
#error_messages ⇒ Object
68 69 70 71 72 |
# File 'lib/appfuel/response.rb', line 68 def return {} if ok? errors. end |
#errors? ⇒ Boolean Also known as: failure?
63 64 65 |
# File 'lib/appfuel/response.rb', line 63 def errors? !ok? end |
#ok? ⇒ Boolean Also known as: success?
74 75 76 |
# File 'lib/appfuel/response.rb', line 74 def ok? errors.nil? end |
#to_h ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/appfuel/response.rb', line 79 def to_h if ok? {ok: ok} else errors.to_h end end |
#to_json ⇒ Object
87 88 89 |
# File 'lib/appfuel/response.rb', line 87 def to_json to_h.to_json end |