Class: MatViews::ServiceResponse
- Inherits:
-
Object
- Object
- MatViews::ServiceResponse
- Defined in:
- lib/mat_views/service_response.rb
Overview
Encapsulates the result of a service operation within MatViews.
Provides a consistent contract for all services by standardizing:
-
‘status`: Symbol representing outcome (`:ok`, `:created`, `:updated`, `:skipped`, `:deleted`, `:error`)
-
‘request`: Request detailed that service was invoked with
-
‘response`: Response detailed that service returned, nil with :error status
-
‘error`: Exception or error message, with :error status
- `message`: String description of the error - `class`: Exception class name - `backtrace`: Array of strings
Constant Summary collapse
- ACCEPTABLE_STATES =
acceptable status values
%i[ok created updated skipped deleted error].freeze
- OK_STATES =
statuses indicating success
%i[ok created updated skipped deleted].freeze
- ERROR_STATES =
statuses indicating error
%i[error].freeze
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#error? ⇒ Boolean
Whether the response represents an error.
-
#initialize(status:, request: {}, response: {}, error: nil) ⇒ ServiceResponse
constructor
A new instance of ServiceResponse.
-
#success? ⇒ Boolean
Whether the response represents a success.
-
#to_h ⇒ Hash
Hash representation of the response.
Constructor Details
#initialize(status:, request: {}, response: {}, error: nil) ⇒ ServiceResponse
Returns a new instance of ServiceResponse.
50 51 52 53 54 55 56 57 58 |
# File 'lib/mat_views/service_response.rb', line 50 def initialize(status:, request: {}, response: {}, error: nil) raise ArgumentError, 'status is required' unless ACCEPTABLE_STATES.include?(status&.to_sym) raise ArgumentError, 'error must be an Exception object' if error && !error.is_a?(Exception) @status = status.to_sym @request = request @response = response @error = error&.mv_serialize_error end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
35 36 37 |
# File 'lib/mat_views/service_response.rb', line 35 def error @error end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
35 36 37 |
# File 'lib/mat_views/service_response.rb', line 35 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
35 36 37 |
# File 'lib/mat_views/service_response.rb', line 35 def response @response end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
35 36 37 |
# File 'lib/mat_views/service_response.rb', line 35 def status @status end |
Instance Method Details
#error? ⇒ Boolean
Returns whether the response represents an error.
66 67 68 |
# File 'lib/mat_views/service_response.rb', line 66 def error? ERROR_STATES.include?(status) end |
#success? ⇒ Boolean
Returns whether the response represents a success.
61 62 63 |
# File 'lib/mat_views/service_response.rb', line 61 def success? OK_STATES.include?(status) end |
#to_h ⇒ Hash
Returns hash representation of the response.
71 72 73 |
# File 'lib/mat_views/service_response.rb', line 71 def to_h { status:, request:, response:, error: }.compact end |