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
-
.from_legacy_hash(response) ⇒ Object
This is used to help wrap old service responses that were just hashes.
- .success(message: nil, payload: {}, http_status: :ok) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #cause ⇒ 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.
33 34 35 36 37 38 39 |
# File 'app/services/service_response.rb', line 33 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.
31 32 33 |
# File 'app/services/service_response.rb', line 31 def http_status @http_status end |
#message ⇒ Object
Returns the value of attribute message.
31 32 33 |
# File 'app/services/service_response.rb', line 31 def @message end |
#payload ⇒ Object
Returns the value of attribute payload.
31 32 33 |
# File 'app/services/service_response.rb', line 31 def payload @payload end |
#reason ⇒ Object
Returns the value of attribute reason.
31 32 33 |
# File 'app/services/service_response.rb', line 31 def reason @reason end |
#status ⇒ Object
Returns the value of attribute status.
31 32 33 |
# File 'app/services/service_response.rb', line 31 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 |
.from_legacy_hash(response) ⇒ Object
This is used to help wrap old service responses that were just hashes
24 25 26 27 28 29 |
# File 'app/services/service_response.rb', line 24 def self.from_legacy_hash(response) return response if response.is_a?(ServiceResponse) return ServiceResponse.new(**response) if response.is_a?(Hash) raise ArgumentError, "argument must be a ServiceResponse or a Hash" 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
59 60 61 |
# File 'app/services/service_response.rb', line 59 def [](key) to_h[key] end |
#cause ⇒ Object
89 90 91 |
# File 'app/services/service_response.rb', line 89 def cause ActiveSupport::StringInquirer.new(reason.to_s) end |
#deconstruct_keys(keys) ⇒ Object
71 72 73 |
# File 'app/services/service_response.rb', line 71 def deconstruct_keys(keys) to_h.slice(*keys) end |
#error? ⇒ Boolean
79 80 81 |
# File 'app/services/service_response.rb', line 79 def error? status == :error end |
#errors ⇒ Object
83 84 85 86 87 |
# File 'app/services/service_response.rb', line 83 def errors return [] unless error? Array.wrap() end |
#log_and_raise_exception(as: StandardError, **extra_data) ⇒ Object
41 42 43 44 45 |
# File 'app/services/service_response.rb', line 41 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
75 76 77 |
# File 'app/services/service_response.rb', line 75 def success? status == :success end |
#to_h ⇒ Object
63 64 65 66 67 68 69 |
# File 'app/services/service_response.rb', line 63 def to_h (payload || {}).merge( status: status, message: , http_status: http_status, reason: reason) end |
#track_and_raise_exception(as: StandardError, **extra_data) ⇒ Object
53 54 55 56 57 |
# File 'app/services/service_response.rb', line 53 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
47 48 49 50 51 |
# File 'app/services/service_response.rb', line 47 def track_exception(as: StandardError, **extra_data) error_tracking(as) do |ex| Gitlab::ErrorTracking.track_exception(ex, extra_data) end end |