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.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
- .error(message:, payload: {}, http_status: nil) ⇒ Object
- .success(message: nil, payload: {}, http_status: :ok) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #error? ⇒ Boolean
- #errors ⇒ Object
-
#initialize(status:, message: nil, payload: {}, http_status: nil) ⇒ ServiceResponse
constructor
A new instance of ServiceResponse.
- #success? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(status:, message: nil, payload: {}, http_status: nil) ⇒ ServiceResponse
Returns a new instance of ServiceResponse.
14 15 16 17 18 19 |
# File 'app/services/service_response.rb', line 14 def initialize(status:, message: nil, payload: {}, http_status: nil) self.status = status self. = self.payload = payload self.http_status = http_status end |
Instance Attribute Details
#http_status ⇒ Object
Returns the value of attribute http_status.
12 13 14 |
# File 'app/services/service_response.rb', line 12 def http_status @http_status end |
#message ⇒ Object
Returns the value of attribute message.
12 13 14 |
# File 'app/services/service_response.rb', line 12 def @message end |
#payload ⇒ Object
Returns the value of attribute payload.
12 13 14 |
# File 'app/services/service_response.rb', line 12 def payload @payload end |
#status ⇒ Object
Returns the value of attribute status.
12 13 14 |
# File 'app/services/service_response.rb', line 12 def status @status end |
Class Method Details
.error(message:, payload: {}, http_status: nil) ⇒ Object
8 9 10 |
# File 'app/services/service_response.rb', line 8 def self.error(message:, payload: {}, http_status: nil) new(status: :error, message: , payload: payload, http_status: http_status) end |
.success(message: nil, payload: {}, http_status: :ok) ⇒ Object
4 5 6 |
# 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
21 22 23 |
# File 'app/services/service_response.rb', line 21 def [](key) to_h[key] end |
#error? ⇒ Boolean
33 34 35 |
# File 'app/services/service_response.rb', line 33 def error? status == :error end |
#errors ⇒ Object
37 38 39 40 41 |
# File 'app/services/service_response.rb', line 37 def errors return [] unless error? Array.wrap() end |
#success? ⇒ Boolean
29 30 31 |
# File 'app/services/service_response.rb', line 29 def success? status == :success end |
#to_h ⇒ Object
25 26 27 |
# File 'app/services/service_response.rb', line 25 def to_h (payload || {}).merge(status: status, message: , http_status: http_status) end |