class Alerter::MessageDispatcher

Attributes

message[R]
recipients[R]

Public Class Methods

new(message, recipients) click to toggle source
# File lib/alerter/message_dispatcher.rb, line 12
def initialize(message, recipients)
  @message, @recipients = message, recipients
end

Public Instance Methods

call() click to toggle source
# File lib/alerter/message_dispatcher.rb, line 16
def call
  return false unless (Alerter.notification_method - Alerter.available_notification_methods).empty? # array subtraction to see if notification methods are in the available list
  Alerter.notification_method.each do |method|
    case method
      when 'email'
        if Alerter.mailer_wants_array
          send_email(filtered_recipients(method))
        else
          filtered_recipients(method).each do |recipient|
            send_email(recipient) if recipient.notification_methods(message.notification_type).include?(method) && recipient.email.present?
          end
        end
      when 'none', 'ios_push', 'android_push', 'sms', 'twitter'

      else
        raise MethodNotImplemented.new(method)
    end
  end
end

Private Instance Methods

filtered_recipients(method) click to toggle source

recipients can be filtered on a notification type basis

# File lib/alerter/message_dispatcher.rb, line 45
def filtered_recipients(method)
  recipients.each_with_object([]) do |recipient, array|
    pref = recipient.preferences.find_by(notification_type: message.notification_type)
    array << recipient if pref && recipient.notification_methods(message.notification_type).include?(method)
  end
end
mailer() click to toggle source
# File lib/alerter/message_dispatcher.rb, line 38
def mailer
  klass = message.class.name.demodulize
  method = "#{klass.downcase}_mailer".to_sym
  Alerter.methods.include?(method) ? Alerter.send(method) : "#{message.class}Mailer".constantize
end
send_email(recipient) click to toggle source
# File lib/alerter/message_dispatcher.rb, line 53
def send_email(recipient)
  if Alerter.custom_email_delivery_proc
    Alerter.custom_email_delivery_proc.call(mailer, message, recipient)
  else
    email = mailer.send_email(message, recipient)
    email.respond_to?(:deliver_now) ? email.deliver_now : email.deliver
  end
end