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

Instance Attribute Details

#http_statusObject

Returns the value of attribute http_status.



23
24
25
# File 'app/services/service_response.rb', line 23

def http_status
  @http_status
end

#messageObject

Returns the value of attribute message.



23
24
25
# File 'app/services/service_response.rb', line 23

def message
  @message
end

#payloadObject

Returns the value of attribute payload.



23
24
25
# File 'app/services/service_response.rb', line 23

def payload
  @payload
end

#reasonObject

Returns the value of attribute reason.



23
24
25
# File 'app/services/service_response.rb', line 23

def reason
  @reason
end

#statusObject

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

Returns:

  • (Boolean)


71
72
73
# File 'app/services/service_response.rb', line 71

def error?
  status == :error
end

#errorsObject



75
76
77
78
79
# File 'app/services/service_response.rb', line 75

def errors
  return [] unless error?

  Array.wrap(message)
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

Returns:

  • (Boolean)


67
68
69
# File 'app/services/service_response.rb', line 67

def success?
  status == :success
end

#to_hObject



55
56
57
58
59
60
61
# File 'app/services/service_response.rb', line 55

def to_h
  (payload || {}).merge(
    status: status,
    message: 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