Class: BugBunny::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/bug_bunny/message.rb

Constant Summary collapse

FIELD_ERROR =

API ERROR RESPONSE KEY

:field
SERVER_ERROR =
:server
MISSING_FIELD =

API ERROR RESPONSE CODES

:missing
UNKNOWN_FIELD =
:unknown
NOT_FOUND =
:not_found
NO_ACTION =
:no_action
TIMEOUT =
:timeout
BOMBA =
:bomba

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Message

Returns a new instance of Message.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bug_bunny/message.rb', line 26

def initialize(opts = {})
  @correlation_id = opts[:correlation_id] || SecureRandom.uuid
  @body           = deserialize_body(opts[:body] || opts[:response] || {})
  @errors         = opts[:errors]
  @status         = opts[:status] || :success
  @service_name   = opts[:service_name]
  @service_action = opts[:service_action] # Deberiamos raisear si esto no viene...
  @version        = opts[:version]
  @signature      = opts[:signature]
  @reply_to       = opts[:reply_to]
  @exception      = opts[:exception]
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



15
16
17
# File 'lib/bug_bunny/message.rb', line 15

def body
  @body
end

#correlation_idObject

Returns the value of attribute correlation_id.



15
16
17
# File 'lib/bug_bunny/message.rb', line 15

def correlation_id
  @correlation_id
end

#errorsObject

Returns the value of attribute errors.



15
16
17
# File 'lib/bug_bunny/message.rb', line 15

def errors
  @errors
end

#exceptionObject

Returns the value of attribute exception.



15
16
17
# File 'lib/bug_bunny/message.rb', line 15

def exception
  @exception
end

#reply_toObject

Returns the value of attribute reply_to.



15
16
17
# File 'lib/bug_bunny/message.rb', line 15

def reply_to
  @reply_to
end

#service_actionObject

Returns the value of attribute service_action.



15
16
17
# File 'lib/bug_bunny/message.rb', line 15

def service_action
  @service_action
end

#service_nameObject

Returns the value of attribute service_name.



15
16
17
# File 'lib/bug_bunny/message.rb', line 15

def service_name
  @service_name
end

#signatureObject

Returns the value of attribute signature.



15
16
17
# File 'lib/bug_bunny/message.rb', line 15

def signature
  @signature
end

#statusObject

Returns the value of attribute status.



15
16
17
# File 'lib/bug_bunny/message.rb', line 15

def status
  @status
end

#versionObject

Returns the value of attribute version.



15
16
17
# File 'lib/bug_bunny/message.rb', line 15

def version
  @version
end

Instance Method Details

#build_message(params = {}) ⇒ Object



143
144
145
# File 'lib/bug_bunny/message.rb', line 143

def build_message(params = {})
  Message.new({ version: version, correlation_id: correlation_id, service_action: service_action }.merge(params))
end

#critical?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/bug_bunny/message.rb', line 139

def critical?
  status.to_sym == :critical
end

#deserialize_body(body) ⇒ Object



151
152
153
# File 'lib/bug_bunny/message.rb', line 151

def deserialize_body(body)
  Helpers.utc_values_to_local(body)
end

#error?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/bug_bunny/message.rb', line 135

def error?
  status.to_sym == :error
end

#formattedObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bug_bunny/message.rb', line 82

def formatted
  resp = {
    correlation_id: correlation_id,
    version:        version,
    status:         status,
    service_name:   service_name,
    service_action: service_action,
    signature:      signature,
    errors:         errors,
    body:           serialize_body
  }

  if exception
    # resp[:exception] = exception.backtrace.join("\n") rescue nil
    resp[:exception] = [
      exception.to_s,
      exception.try(:backtrace) || []
    ].flatten.join("\n") rescue exception.to_s

    unless ::BugBunny::Exception::ServiceClasses.include?(exception.class)
      self.exception = Exception::ServiceError.new
    end
    resp[:errors] ||= {}
    unless resp[:errors][SERVER_ERROR]&.any?
      resp[:errors][SERVER_ERROR] ||= []
      resp[:errors][SERVER_ERROR] << exception.to_s
    end
    resp[:status] = self.status = :critical
  end

  resp
end

#invalid_signature?(key) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/bug_bunny/message.rb', line 72

def invalid_signature?(key)
  !valid_signature?(key)
end

#serialize_bodyObject



147
148
149
# File 'lib/bug_bunny/message.rb', line 147

def serialize_body
  Helpers.datetime_values_to_utc(body)
end

#server_error!(errors = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bug_bunny/message.rb', line 51

def server_error!(errors=nil)
  self.status = :error
  self.body = {}
  if errors
    self.errors ||= {}
    self.errors[SERVER_ERROR] ||= []
    self.errors[SERVER_ERROR] += [errors].flatten # just in case
  else
    self.exception = Exception::ServiceError.new
  end
  self
end

#server_no_action!Object



47
48
49
# File 'lib/bug_bunny/message.rb', line 47

def server_no_action!
  server_error! [NO_ACTION]
end

#server_not_found!Object



39
40
41
# File 'lib/bug_bunny/message.rb', line 39

def server_not_found!
  server_error! [NOT_FOUND]
end

#server_timeout!Object



43
44
45
# File 'lib/bug_bunny/message.rb', line 43

def server_timeout!
  server_error! [TIMEOUT]
end

#sign!(key) ⇒ Object



68
69
70
# File 'lib/bug_bunny/message.rb', line 68

def sign!(key)
  self.signature = ::BugBunny::Security.sign_message(key, body.to_json)
end

#signed?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/bug_bunny/message.rb', line 64

def signed?
  signature.present?
end

#success?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/bug_bunny/message.rb', line 131

def success?
  status.to_sym == :success
end

#to_hObject Also known as: original_to_h



123
124
125
126
127
# File 'lib/bug_bunny/message.rb', line 123

def to_h
  formatted
rescue StandardError
  original_to_h
end

#to_jsonObject



115
116
117
# File 'lib/bug_bunny/message.rb', line 115

def to_json
  formatted.to_json
end

#to_sObject



119
120
121
# File 'lib/bug_bunny/message.rb', line 119

def to_s
  to_json # Asegurarse de que siempre se llame al "formatted"
end

#valid_signature?(key) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/bug_bunny/message.rb', line 76

def valid_signature?(key)
  return if signature.blank?

  ::BugBunny::Security.check_sign(key, signature, body.to_json)
end