Class: ServiceResponse
- Inherits:
-
Object
- Object
- ServiceResponse
- Defined in:
- app/services/service_response.rb
Instance Attribute Summary collapse
-
#http_status ⇒ Object
readonly
Returns the value of attribute http_status.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#reason ⇒ Object
readonly
Returns the value of attribute reason.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
- .error(message:, payload: {}, http_status: nil, reason: nil) ⇒ Object
- .success(message: nil, payload: {}, http_status: :ok) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #deconstruct_keys(keys) ⇒ Object
- #error? ⇒ Boolean
- #errors ⇒ Object
-
#initialize(status:, message: nil, payload: {}, http_status: nil, reason: nil) ⇒ ServiceResponse
constructor
A new instance of ServiceResponse.
- #log_and_raise_exception(as: StandardError, **extra_data) ⇒ Object
- #success? ⇒ Boolean
- #to_h ⇒ Object
- #track_and_raise_exception(as: StandardError, **extra_data) ⇒ Object
- #track_exception(as: StandardError, **extra_data) ⇒ Object
Constructor Details
#initialize(status:, message: nil, payload: {}, http_status: nil, reason: nil) ⇒ ServiceResponse
Returns a new instance of ServiceResponse.
25 26 27 28 29 30 31 |
# File 'app/services/service_response.rb', line 25 def initialize(status:, message: nil, payload: {}, http_status: nil, reason: nil) self.status = status self. = self.payload = payload self.http_status = http_status self.reason = reason end |
Instance Attribute Details
#http_status ⇒ Object
Returns the value of attribute http_status.
23 24 25 |
# File 'app/services/service_response.rb', line 23 def http_status @http_status end |
#message ⇒ Object
Returns the value of attribute message.
23 24 25 |
# File 'app/services/service_response.rb', line 23 def @message end |
#payload ⇒ Object
Returns the value of attribute payload.
23 24 25 |
# File 'app/services/service_response.rb', line 23 def payload @payload end |
#reason ⇒ Object
Returns the value of attribute reason.
23 24 25 |
# File 'app/services/service_response.rb', line 23 def reason @reason end |
#status ⇒ Object
Returns the value of attribute status.
23 24 25 |
# File 'app/services/service_response.rb', line 23 def status @status end |
Class Method Details
.error(message:, payload: {}, http_status: nil, reason: nil) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'app/services/service_response.rb', line 13 def self.error(message:, payload: {}, http_status: nil, reason: nil) new( status: :error, message: , payload: payload, http_status: http_status, reason: reason ) end |
.success(message: nil, payload: {}, http_status: :ok) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'app/services/service_response.rb', line 4 def self.success(message: nil, payload: {}, http_status: :ok) new( status: :success, message: , payload: payload, http_status: http_status ) end |
Instance Method Details
#[](key) ⇒ Object
51 52 53 |
# File 'app/services/service_response.rb', line 51 def [](key) to_h[key] end |
#deconstruct_keys(keys) ⇒ Object
63 64 65 |
# File 'app/services/service_response.rb', line 63 def deconstruct_keys(keys) to_h.slice(*keys) end |
#error? ⇒ Boolean
71 72 73 |
# File 'app/services/service_response.rb', line 71 def error? status == :error end |
#errors ⇒ Object
75 76 77 78 79 |
# File 'app/services/service_response.rb', line 75 def errors return [] unless error? Array.wrap() end |
#log_and_raise_exception(as: StandardError, **extra_data) ⇒ Object
33 34 35 36 37 |
# File 'app/services/service_response.rb', line 33 def log_and_raise_exception(as: StandardError, **extra_data) error_tracking(as) do |ex| Gitlab::ErrorTracking.log_and_raise_exception(ex, extra_data) end end |
#success? ⇒ Boolean
67 68 69 |
# File 'app/services/service_response.rb', line 67 def success? status == :success end |
#to_h ⇒ Object
55 56 57 58 59 60 61 |
# File 'app/services/service_response.rb', line 55 def to_h (payload || {}).merge( status: status, message: , http_status: http_status, reason: reason) end |
#track_and_raise_exception(as: StandardError, **extra_data) ⇒ Object
45 46 47 48 49 |
# File 'app/services/service_response.rb', line 45 def track_and_raise_exception(as: StandardError, **extra_data) error_tracking(as) do |ex| Gitlab::ErrorTracking.track_and_raise_exception(ex, extra_data) end end |
#track_exception(as: StandardError, **extra_data) ⇒ Object
39 40 41 42 43 |
# File 'app/services/service_response.rb', line 39 def track_exception(as: StandardError, **extra_data) error_tracking(as) do |ex| Gitlab::ErrorTracking.track_exception(ex, extra_data) end end |