Class: MatViews::ServiceResponse

Inherits:
Object
  • Object
show all
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
    

Examples:

Successful response

MatViews::ServiceResponse.new(
  status: :updated,
  response: { ... }
)

Error response

MatViews::ServiceResponse.new(
  status: :error,
  error: StandardError.new("Something went wrong")
)

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

Instance Method Summary collapse

Constructor Details

#initialize(status:, request: {}, response: {}, error: nil) ⇒ ServiceResponse

Returns a new instance of ServiceResponse.

Parameters:

  • status (Symbol)

    the outcome status

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

    request details

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

    response details

  • error (Exception, String, nil) (defaults to: nil)

    error details if applicable

Raises:

  • (ArgumentError)


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

#errorObject (readonly)

Returns the value of attribute error.



35
36
37
# File 'lib/mat_views/service_response.rb', line 35

def error
  @error
end

#requestObject (readonly)

Returns the value of attribute request.



35
36
37
# File 'lib/mat_views/service_response.rb', line 35

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



35
36
37
# File 'lib/mat_views/service_response.rb', line 35

def response
  @response
end

#statusObject (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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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_hHash

Returns hash representation of the response.

Returns:

  • (Hash)

    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