Class: Notification
- Inherits:
-
Object
- Object
- Notification
- Includes:
- StandardModel
- Defined in:
- lib/app/models/notification.rb
Direct Known Subclasses
Constant Summary collapse
- STATE_INVALID =
Constants
'invalid'.freeze
- STATE_NEW =
'new'.freeze
- STATE_PROCESSED =
'processed'.freeze
- STATE_PROCESSING =
'processing'.freeze
- STATE_RESUBMITTED =
'resubmitted'.freeze
- STATE_RETRYING =
'retrying'.freeze
- STATE_SUBMITTED =
'submitted'.freeze
- STATE_VIEWED =
'viewed'.freeze
- ALL_STATES =
[STATE_INVALID, STATE_NEW, STATE_PROCESSED, STATE_PROCESSING, STATE_RESUBMITTED, STATE_RETRYING, STATE_SUBMITTED, STATE_VIEWED].freeze
- DELIVERY_EMAIL =
Channels
'email'.freeze
- DELIVERY_SLACK =
'slack'.freeze
- DELIVERY_SMS =
'sms'.freeze
Instance Method Summary collapse
-
#account_message_template(template_name) ⇒ Object
Retrieve the template from the account.
-
#default_message_template(template_name) ⇒ Object
Get the default template stored in the database that is not associated with an account.
-
#deletable? ⇒ Boolean
If this notification can be deleted, we don’t want to delete one that is currently being processed.
- #deliver_message ⇒ Object
- #deliver_message! ⇒ Object
-
#delivery_channel ⇒ Object
Default delivery channel is email, override for sms, SMTP or other channels.
-
#finish_processing(processing_message = nil) ⇒ Object
Finish processing the notification successfully.
- #from_template(template_name, locals = {}) ⇒ Object
- #message_from_haml_file(file_name, locals) ⇒ Object
- #message_from_haml_text(haml_text, locals) ⇒ Object
- #message_from_liquid_text(liquid_text, locals) ⇒ Object
- #message_from_template(template_name, locals) ⇒ Object
-
#render_liquid_text(liquid_text, locals = {}) ⇒ Object
Render the given liquid text.
-
#retry_delivery(message) ⇒ Object
Set to retrying.
-
#send_notification ⇒ Object
Send the notification.
-
#sendable? ⇒ Boolean
If this notification can be resent, we don’t want to resend one that is currently being processed.
-
#start_processing ⇒ Object
Start processing the notification.
-
#stringify_all(obj) ⇒ Object
Convert all keys and values to strings for liquid sanity.
-
#successful? ⇒ Boolean
Was this notification successful sent.
-
#template_from_file(template_name, format: 'liquid', prefix: nil) ⇒ Object
Retrieve the template out of the project.
-
#viewed ⇒ Object
Mark this as viewed.
Methods included from StandardModel
#clear_cache, included, #remove_blank_secure_fields, #secure_fields, #update, #update!
Methods included from App47Logger
#clean_params, #delete_parameter_keys, #log_controller_error, #log_debug, log_debug, log_error, #log_error, log_exception, #log_message, log_message, #log_warn, log_warn, #mask_parameter_keys, #update_flash_messages
Instance Method Details
#account_message_template(template_name) ⇒ Object
Retrieve the template from the account
If the account does exists or the template in the account does exist, we catch the error and return nil
159 160 161 162 163 |
# File 'lib/app/models/notification.rb', line 159 def (template_name) account.templates.find_by(name: template_name.to_s, _type: "Account#{delivery_channel.humanize}Template").template rescue StandardError nil end |
#default_message_template(template_name) ⇒ Object
Get the default template stored in the database that is not associated with an account
168 169 170 171 172 |
# File 'lib/app/models/notification.rb', line 168 def (template_name) Template.find_by(account: nil, name: template_name.to_s).template rescue StandardError nil end |
#deletable? ⇒ Boolean
If this notification can be deleted, we don’t want to delete one that is currently being processed.
65 66 67 |
# File 'lib/app/models/notification.rb', line 65 def deletable? [STATE_PROCESSED, STATE_INVALID, STATE_NEW, STATE_VIEWED].include? state end |
#deliver_message ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/app/models/notification.rb', line 131 def start_processing finish_processing rescue StandardError => error if retries > 10 log_error "Unable to process notification id: #{id}, done retrying", error finish_processing "Failed final attempt: #{error.}" notify_failure(error) else log_error "Unable to process notification id: #{id}, retrying!!", error retry_delivery("Failed attempt # #{retries}: #{error.}") delay(run_at: 10.minutes.from_now).deliver end end |
#deliver_message! ⇒ Object
127 128 129 |
# File 'lib/app/models/notification.rb', line 127 def raise 'Incomplete class, concrete implementation should implment #deliver_message!' end |
#delivery_channel ⇒ Object
Default delivery channel is email, override for sms, SMTP or other channels
191 192 193 |
# File 'lib/app/models/notification.rb', line 191 def delivery_channel DELIVERY_EMAIL end |
#finish_processing(processing_message = nil) ⇒ Object
Finish processing the notification successfully
100 101 102 103 104 105 106 |
# File 'lib/app/models/notification.rb', line 100 def finish_processing( = nil) if .present? set state: STATE_INVALID, error_message: else set state: STATE_PROCESSED, error_message: '' end end |
#from_template(template_name, locals = {}) ⇒ Object
149 150 151 152 |
# File 'lib/app/models/notification.rb', line 149 def from_template(template_name, locals = {}) locals[:base_url] = SystemConfiguration.base_url self. = (template_name, locals) end |
#message_from_haml_file(file_name, locals) ⇒ Object
209 210 211 |
# File 'lib/app/models/notification.rb', line 209 def (file_name, locals) (File.read(file_name), locals) end |
#message_from_haml_text(haml_text, locals) ⇒ Object
203 204 205 206 207 |
# File 'lib/app/models/notification.rb', line 203 def (haml_text, locals) locals[:base_url] = SystemConfiguration.base_url engine = Haml::Engine.new(haml_text) self. = engine.render(Object.new, stringify_all(locals)) end |
#message_from_liquid_text(liquid_text, locals) ⇒ Object
213 214 215 |
# File 'lib/app/models/notification.rb', line 213 def (liquid_text, locals) self. = render_liquid_text(liquid_text, locals) end |
#message_from_template(template_name, locals) ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/app/models/notification.rb', line 195 def (template_name, locals) template = (template_name) || template_from_file(template_name) return (template, locals) if template.present? template = template_from_file(template_name, format: 'haml') (template, locals) if template.present? end |
#render_liquid_text(liquid_text, locals = {}) ⇒ Object
Render the given liquid text
220 221 222 223 |
# File 'lib/app/models/notification.rb', line 220 def render_liquid_text(liquid_text, locals = {}) locals[:base_url] = SystemConfiguration.base_url Liquid::Template.parse(liquid_text).render(stringify_all(locals)) end |
#retry_delivery(message) ⇒ Object
Set to retrying
93 94 95 |
# File 'lib/app/models/notification.rb', line 93 def retry_delivery() set error_message: , retries: retries + 1, state: STATE_RETRYING end |
#send_notification ⇒ Object
Send the notification
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/app/models/notification.rb', line 111 def send_notification if state.eql? STATE_NEW self.state = STATE_SUBMITTED else self.retries = 0 self.state = STATE_RESUBMITTED self. = 'Retrying' end begin if save! rescue StandardError => error finish_processing error. end end |
#sendable? ⇒ Boolean
If this notification can be resent, we don’t want to resend one that is currently being processed.
72 73 74 |
# File 'lib/app/models/notification.rb', line 72 def sendable? [STATE_PROCESSED, STATE_INVALID, STATE_NEW, STATE_VIEWED].include? state end |
#start_processing ⇒ Object
Start processing the notification
86 87 88 |
# File 'lib/app/models/notification.rb', line 86 def start_processing set state: STATE_PROCESSING end |
#stringify_all(obj) ⇒ Object
Convert all keys and values to strings for liquid sanity
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/app/models/notification.rb', line 228 def stringify_all(obj) case obj when Hash result = {} obj.each { |key, value| result[key.to_s] = stringify_all(value) } when Array result = [] obj.each { |value| result << stringify_all(value) } when FalseClass result = false when TrueClass result = true else result = obj.to_s end result end |
#successful? ⇒ Boolean
Was this notification successful sent
58 59 60 |
# File 'lib/app/models/notification.rb', line 58 def successful? [STATE_PROCESSED, STATE_VIEWED].include? state end |
#template_from_file(template_name, format: 'liquid', prefix: nil) ⇒ Object
Retrieve the template out of the project
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/app/models/notification.rb', line 177 def template_from_file(template_name, format: 'liquid', prefix: nil) file_name = [template_name, prefix, format].compact.join('.') if File.exist?(Rails.root.join('lib', 'templates', delivery_channel, file_name)) File.open(Rails.root.join('lib', 'templates', delivery_channel, file_name)) else File.read(File.join(__dir__, '../../lib', 'templates', delivery_channel, file_name)) end.read rescue StandardError nil end |
#viewed ⇒ Object
Mark this as viewed
79 80 81 |
# File 'lib/app/models/notification.rb', line 79 def viewed set(state: STATE_VIEWED, last_viewed_at: Time.now.utc, viewed_count: viewed_count + 1) end |