Class: Appfuel::ResponseHandler
- Inherits:
-
Object
- Object
- Appfuel::ResponseHandler
- Defined in:
- lib/appfuel/response_handler.rb
Instance Attribute Summary collapse
-
#response_class ⇒ Object
readonly
Returns the value of attribute response_class.
Instance Method Summary collapse
- #create_response(data) ⇒ Object
-
#error(*args) ⇒ Object
Convert a number of different error formats into hash and use that to build the response.
-
#error_data?(data) ⇒ Boolean
Determine if the data given is an error by looking at its class or checking if it is a hash with the key :errors.
-
#initialize(response_class = Response) ⇒ ResponseHandler
constructor
A new instance of ResponseHandler.
-
#ok(value = nil) ⇒ Object
This is used when returning results back to the action handler.
- #response?(data) ⇒ Boolean
Constructor Details
#initialize(response_class = Response) ⇒ ResponseHandler
Returns a new instance of ResponseHandler.
5 6 7 |
# File 'lib/appfuel/response_handler.rb', line 5 def initialize(response_class = Response) @response_class = response_class end |
Instance Attribute Details
#response_class ⇒ Object (readonly)
Returns the value of attribute response_class.
3 4 5 |
# File 'lib/appfuel/response_handler.rb', line 3 def response_class @response_class end |
Instance Method Details
#create_response(data) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/appfuel/response_handler.rb', line 9 def create_response(data) if response?(data) return data end if error_data?(data) if data.is_a?(Hash) data.symbolize_keys! end return error(data) end ok(data) end |
#error(*args) ⇒ Object
Convert a number of different error formats into hash and use that to build the response
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/appfuel/response_handler.rb', line 58 def error(*args) error = args.shift case when error.kind_of?(ActiveModel::Errors) = error. when error.kind_of?(StandardError) key = error.class.to_s.underscore.to_sym backtrace_key = "#{key}_backtrace".to_sym = { errors: { key => [error.], backtrace_key => error.backtrace || [] } } when error.is_a?(Hash) = error.key?(:errors) ? error : {errors: error} when error.is_a?(Errors) = error.to_h when args.length >= 1 = {errors: {error => args}} when error.is_a?(response_class) return error else = {errors: {general_error: [error.to_s]}} end response_class.error() end |
#error_data?(data) ⇒ Boolean
Determine if the data given is an error by looking at its class or checking if it is a hash with the key :errors
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/appfuel/response_handler.rb', line 33 def error_data?(data) case when data.kind_of?(::StandardError) || data.is_a?(Errors) true when data.is_a?(Hash) response_class.error_key?(data) else false end end |
#ok(value = nil) ⇒ Object
This is used when returning results back to the action handler. We use this to indicate it was a successful response
49 50 51 |
# File 'lib/appfuel/response_handler.rb', line 49 def ok(value = nil) response_class.ok(value) end |
#response?(data) ⇒ Boolean
24 25 26 |
# File 'lib/appfuel/response_handler.rb', line 24 def response?(data) data.is_a?(response_class) end |