debian-mirror-gitlab/app/services/service_response.rb

39 lines
842 B
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
class ServiceResponse
2019-12-04 20:38:33 +05:30
def self.success(message: nil, payload: {}, http_status: :ok)
new(status: :success, message: message, payload: payload, http_status: http_status)
2019-07-31 22:56:46 +05:30
end
2019-09-30 21:07:59 +05:30
def self.error(message:, payload: {}, http_status: nil)
new(status: :error, message: message, payload: payload, http_status: http_status)
2019-07-31 22:56:46 +05:30
end
2019-09-30 21:07:59 +05:30
attr_reader :status, :message, :http_status, :payload
2019-07-31 22:56:46 +05:30
2019-09-30 21:07:59 +05:30
def initialize(status:, message: nil, payload: {}, http_status: nil)
2019-07-31 22:56:46 +05:30
self.status = status
self.message = message
2019-09-30 21:07:59 +05:30
self.payload = payload
2019-07-31 22:56:46 +05:30
self.http_status = http_status
end
def success?
status == :success
end
def error?
status == :error
end
2020-06-23 00:09:42 +05:30
def errors
return [] unless error?
Array.wrap(message)
end
2019-07-31 22:56:46 +05:30
private
2019-09-30 21:07:59 +05:30
attr_writer :status, :message, :http_status, :payload
2019-07-31 22:56:46 +05:30
end