Module: UNotifier::SystemNotifier

Defined in:
lib/unotifier/system_notifier.rb

Class Method Summary collapse

Class Method Details

.notify_system(notification, params = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/unotifier/system_notifier.rb', line 3

def self.notify_system(notification, params = {})
  key = notification.key
  config = UNotifier.load_notification!(key)
  return if config["system"].nil?

  # Raise only when we're sure that config has "system" block
  raise AttributeMissingError.new(key, "system") unless params

  data = config["system"]["keys"].each_with_object({}) do |(head, attributes), obj|
    head = head.to_sym
    attributes = attributes.map(&:to_sym)
    raise AttributeMissingError.new(key, head) unless params.key?(head)

    attributes.each { |a| raise AttributeMissingError.new(key, a) unless params[head].keys.include?(a) }

    obj[head] = params[head].slice(*attributes)
  end

  data.merge!(key: notification.key)

  UNotifier.configuration.system_providers.each do |provider|
    provider.notify notification, data
  end
end