Class: Noticent::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/noticent/dispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, alert_name, payload, configuration = {}) ⇒ Dispatcher

Returns a new instance of Dispatcher.



5
6
7
8
9
10
11
12
13
14
# File 'lib/noticent/dispatcher.rb', line 5

def initialize(config, alert_name, payload, configuration = {})
  @config = config
  @alert_name = alert_name
  @payload = payload
  @configuration = configuration

  validate!

  @entity_id = @payload.send("#{scope.name}_id")
end

Instance Method Details

#alertObject



16
17
18
# File 'lib/noticent/dispatcher.rb', line 16

def alert
  @config.alerts[@alert_name]
end

#dispatchObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/noticent/dispatcher.rb', line 43

def dispatch
  notifiers.values.each do |notifier|
    recs = recipients(notifier.recipient)
    notifier.applicable_channels.each do |channel|
      to_send = filter_recipients(recs, channel.name)

      if to_send.count == 0 && @config.skip_alert_with_no_subscribers
        Noticent.configuration.logger.info "Skipping notification of alert #{alert.name} on channel #{channel.name} as there are no subscribers"
        next
      end

      channel_instance = channel.instance(@config, to_send, @payload, @configuration)
      begin
        raise Noticent::BadConfiguration, "channel #{channel.name} (#{channel.klass}) doesn't have a method called #{alert.name}" unless channel_instance.respond_to? alert.name

        channel_instance.send(alert.name)
      rescue StandardError => e
        # log and move on
        raise if @config.halt_on_error

        Noticent.configuration.logger.error e
      end
    end
  end
end

#filter_recipients(recipients, channel) ⇒ Object

only returns recipients that have opted-in for this channel

Raises:

  • (ArgumentError)


36
37
38
39
40
41
# File 'lib/noticent/dispatcher.rb', line 36

def filter_recipients(recipients, channel)
  raise ArgumentError, "channel should be a string or symbol" unless channel.is_a?(String) || channel.is_a?(Symbol)
  raise ArgumentError, "recipients is nil" if recipients.nil?

  recipients.select { |recipient| @config.opt_in_provider.opted_in?(recipient_id: recipient.id, scope: scope.name, entity_id: @entity_id, alert_name: alert.name, channel_name: channel) }
end

#notifiersObject



24
25
26
# File 'lib/noticent/dispatcher.rb', line 24

def notifiers
  alert.notifiers
end

#recipients(notifier_name) ⇒ Object

returns all recipients of a certain notifier unfiltered regardless of “opt-in” and duplicates



29
30
31
32
33
# File 'lib/noticent/dispatcher.rb', line 29

def recipients(notifier_name)
  raise Noticent::InvalidScope, "payload #{@payload.klass} doesn't have a #{notifier_name} method" unless @payload.respond_to? notifier_name

  @payload.send(notifier_name)
end

#scopeObject



20
21
22
# File 'lib/noticent/dispatcher.rb', line 20

def scope
  alert.scope
end