Class: ServiceResponse

Inherits:
Object
  • Object
show all
Defined in:
app/services/service_response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.message = message
  self.payload = payload
  self.http_status = http_status
end

Instance Attribute Details

#http_statusObject

Returns the value of attribute http_status.


12
13
14
# File 'app/services/service_response.rb', line 12

def http_status
  @http_status
end

#messageObject

Returns the value of attribute message.


12
13
14
# File 'app/services/service_response.rb', line 12

def message
  @message
end

#payloadObject

Returns the value of attribute payload.


12
13
14
# File 'app/services/service_response.rb', line 12

def payload
  @payload
end

#statusObject

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: 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: 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

Returns:

  • (Boolean)

33
34
35
# File 'app/services/service_response.rb', line 33

def error?
  status == :error
end

#errorsObject


37
38
39
40
41
# File 'app/services/service_response.rb', line 37

def errors
  return [] unless error?

  Array.wrap(message)
end

#success?Boolean

Returns:

  • (Boolean)

29
30
31
# File 'app/services/service_response.rb', line 29

def success?
  status == :success
end

#to_hObject


25
26
27
# File 'app/services/service_response.rb', line 25

def to_h
  (payload || {}).merge(status: status, message: message, http_status: http_status)
end