Class: Notifun::Notification
- Inherits:
-
Object
- Object
- Notifun::Notification
- Defined in:
- lib/notifun/notification.rb
Class Method Summary collapse
-
.notify(models, key, merge_hash = {}, options = {}) ⇒ Object
model passed in must have following methods: notifun_uuid or uuid: unique identifier notifun_email or email: email to send to notifun_notify_via or notify_via(method) returns if it should send message using one of the message types: [“email”, “push”, “text”].
- .send_via_email(message_template, model, merge_hash, options) ⇒ Object
- .send_via_push(message_template, model, merge_hash, options) ⇒ Object
- .send_via_text(message_template, model, merge_hash, options) ⇒ Object
Class Method Details
.notify(models, key, merge_hash = {}, options = {}) ⇒ Object
model passed in must have following methods: notifun_uuid or uuid: unique identifier notifun_email or email: email to send to notifun_notify_via or notify_via(method) returns if it should send message using one of the message types: [“email”, “push”, “text”]
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/notifun/notification.rb', line 6 def self.notify(models, key, merge_hash={}, ={}) = Notifun::MessageTemplate.find_by_key(key) raise "Unable to find message_template with key #{key}" if .nil? models = [models].flatten status = true models.each do |model| primary_sent = false backup_sent = false preference = Notifun::Preference.where(preferable: model).where(message_template_key: key).first if .category.present? preference ||= Notifun::Preference.where(preferable: model).where(category: .category).first end if preference sent = false preference.notification_methods.each do |notification_method| next if [:notification_methods] && ![:notification_methods].include?(notification_method) sent = self.send("send_via_#{notification_method}", , model, merge_hash, ) end return sent else .default_notification_methods.each do |notification_method| next if [:notification_methods] && ![:notification_methods].include?(notification_method) notify_via = model.try(:notifun_notify_via, notification_method) notify_via = model.notify_via(notification_method) if notify_via.nil? if notify_via if self.send("send_via_#{notification_method}", , model, merge_hash, ) primary_sent = true end end end if !primary_sent .backup_notification_methods.each do |notification_method| next if [:notification_methods] && ![:notification_methods].include?(notification_method) notify_via = model.try(:notifun_notify_via, notification_method) notify_via = model.notify_via(notification_method) if notify_via.nil? if notify_via if self.send("send_via_#{notification_method}", , model, merge_hash, ) backup_sent = true break end end end end if status == true status = primary_sent || backup_sent end end end return status end |
.send_via_email(message_template, model, merge_hash, options) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/notifun/notification.rb', line 128 def self.send_via_email(, model, merge_hash, ) if [:subject].present? subject = [:subject] else subject = .merged_email_subject(merge_hash) end if [:message].present? text = html = [:message] else text = .merged_email_text(merge_hash) html = .merged_email_html(merge_hash) end email = model.try(:notifun_email).presence || model.try(:email).presence if email email_notifier = Notifun::Notifier.email_notifier.new email_notifier.notify!(email, subject, html, text, , ) success = email_notifier.success = email_notifier. else success = false = "No email." end uuid = model.try(:notifun_uuid).presence || model.try(:uuid).presence Notifun::Message.create({ message_template_key: .key, uuid: uuid, recipient: email, notification_method: "email", message_text: html, success: success, message_title: subject, error_message: }) return success end |
.send_via_push(message_template, model, merge_hash, options) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/notifun/notification.rb', line 91 def self.send_via_push(, model, merge_hash, ) if [:message].present? text = [:message] else text = .merged_push_body(merge_hash) end if [:title].present? title = [:title] else title = .merged_push_title(merge_hash) end uuid = model.try(:notifun_uuid).presence || model.try(:uuid).presence if uuid push_notifier = Notifun::Notifier.push_notifier.new push_notifier.notify!(text, title, uuid, ) success = push_notifier.success = push_notifier. else success = false = "No uuid." end data = [:push_data].presence || {} Notifun::Message.create({ message_template_key: .key, uuid: uuid, recipient: uuid, notification_method: "push", message_text: text, success: success, message_title: title, data: data, error_message: }) return success end |
.send_via_text(message_template, model, merge_hash, options) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/notifun/notification.rb', line 60 def self.send_via_text(, model, merge_hash, ) if [:message].present? text = [:message] else text = .merged_text_body(merge_hash) end phone = model.try(:notifun_phone).presence || model.try(:phone).presence if phone text_notifier = Notifun::Notifier.text_notifier.new text_notifier.notify!(text, phone, ) success = text_notifier.success = text_notifier. else success = false = "No phone number." end uuid = model.try(:notifun_uuid).presence || model.try(:uuid).presence Notifun::Message.create({ message_template_key: .key, uuid: uuid, recipient: phone, notification_method: "text", message_text: text, success: success, error_message: }) return success end |