Class: Whatsapp::Notifier
- Inherits:
-
Object
- Object
- Whatsapp::Notifier
- Defined in:
- lib/whatsapp/notifier.rb
Overview
Simple notification service with template support and bulk sending.
Instance Attribute Summary collapse
-
#template ⇒ String?
readonly
Message template with named placeholders.
Instance Method Summary collapse
-
#bulk(messages) ⇒ Hash
Send notifications to multiple recipients.
-
#initialize(template: nil, async: false) ⇒ Notifier
constructor
A new instance of Notifier.
-
#notify(to, text_or_vars = nil, **vars) ⇒ Message, void
Send a notification to a single recipient.
Constructor Details
#initialize(template: nil, async: false) ⇒ Notifier
Returns a new instance of Notifier.
26 27 28 29 |
# File 'lib/whatsapp/notifier.rb', line 26 def initialize(template: nil, async: false) @template = template @async = async end |
Instance Attribute Details
#template ⇒ String? (readonly)
Returns message template with named placeholders.
22 23 24 |
# File 'lib/whatsapp/notifier.rb', line 22 def template @template end |
Instance Method Details
#bulk(messages) ⇒ Hash
Send notifications to multiple recipients.
Stops on rate limit errors and continues on other errors.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/whatsapp/notifier.rb', line 53 def bulk() results = {sent: 0, failed: 0, errors: []} .each do |msg| to = msg[:to] text = msg[:text] || build_text(nil, msg.except(:to)) begin notify(to, text) results[:sent] += 1 rescue Whatsapp::RateLimited => e results[:failed] += .size - results[:sent] - results[:failed] results[:errors] << {to: to, error: e.} break rescue Whatsapp::Error => e results[:failed] += 1 results[:errors] << {to: to, error: e.} end end results end |
#notify(to, text_or_vars = nil, **vars) ⇒ Message, void
Send a notification to a single recipient.
37 38 39 40 41 42 43 44 45 |
# File 'lib/whatsapp/notifier.rb', line 37 def notify(to, text_or_vars = nil, **vars) text = build_text(text_or_vars, vars) if @async && defined?(::WhatsappSendJob) WhatsappSendJob.perform_later(to: to, text: text) else Whatsapp.client.(to: to, text: text) end end |