Module: UNotifier
- Defined in:
- lib/provider.rb,
lib/settings.rb,
lib/unotifier.rb,
lib/exceptions.rb,
lib/configuration.rb,
lib/unotifier/version.rb,
lib/provider/provider_base.rb,
lib/unotifier/user_notifier.rb,
lib/unotifier/system_notifier.rb,
lib/provider/user/action_cable.rb,
lib/provider/user/action_mailer.rb,
lib/provider/system/action_cable.rb
Defined Under Namespace
Modules: Provider, SystemNotifier, UserNotifier
Classes: AttributeMissingError, Configuration, EmptyLocaleKeyError, NotificationNotFoundError, NotificationsConfigNotFoundError, Settings, UNotifierError, UnknownLocaleKeyError, UnknownTargetError
Constant Summary
collapse
- VERSION =
"0.5.0".freeze
Class Attribute Summary collapse
Class Method Summary
collapse
-
.configure {|configuration| ... } ⇒ Object
-
.hide_sensitive_for?(target) ⇒ Boolean
-
.load_notification(key) ⇒ Object
-
.load_notification!(key) ⇒ Object
-
.locale_key_for(key, params = {}) ⇒ Object
-
.notification_settings_for(target) ⇒ Object
-
.notifications_config ⇒ Object
-
.notify(key, recepients, params = {}) ⇒ Object
-
.notify_external(key, target, params) ⇒ Object
-
.notify_onsite(key, target, params) ⇒ Object
-
.notify_target(key, target, params = {}) ⇒ Object
-
.reload! ⇒ Object
-
.urgency_for(key, params = {}) ⇒ Object
Class Attribute Details
.configuration ⇒ Object
17
18
19
|
# File 'lib/unotifier.rb', line 17
def self.configuration
@configuration ||= Configuration.new
end
|
Class Method Details
21
22
23
|
# File 'lib/unotifier.rb', line 21
def self.configure
yield(configuration)
end
|
.hide_sensitive_for?(target) ⇒ Boolean
126
127
128
|
# File 'lib/unotifier.rb', line 126
def self.hide_sensitive_for?(target)
!!target.notification_settings.dig("options", "hide_sensitive")
end
|
.load_notification(key) ⇒ Object
42
43
44
|
# File 'lib/unotifier.rb', line 42
def self.load_notification(key)
notifications_config.dig(*key.split("."))
end
|
.load_notification!(key) ⇒ Object
46
47
48
49
50
51
|
# File 'lib/unotifier.rb', line 46
def self.load_notification!(key)
notification = load_notification(key)
raise NotificationNotFoundError, key unless notification
notification
end
|
.locale_key_for(key, params = {}) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/unotifier.rb', line 53
def self.locale_key_for(key, params = {})
config = load_notification!(key)["user"]
if config["locale_options"].is_a?(Array)
raise EmptyLocaleKeyError, key unless params[:locale_key]
raise UnknownLocaleKeyError.new(
key, params[:locale_key], config["locale_options"]
) unless config["locale_options"].include?(params[:locale_key])
"notifications.#{key}.#{params[:locale_key]}"
else
"notifications.#{key}"
end
end
|
.notification_settings_for(target) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/unotifier.rb', line 109
def self.notification_settings_for(target)
user_settings = target.notification_settings
Settings
.grouped_by_urgency_keys_from(notifications_config)
.each_with_object({}) do |(level, grouped_keys), obj|
obj[level] =
grouped_keys.each_with_object({}) do |(urgency, keys), settings|
settings[urgency] = keys.each_with_object({}) do |(key, _), out|
out[key] =
user_settings.is_a?(Hash) && user_settings.dig(level, key) ||
Settings::DEFAULT_URGENCY
end
end
end
.merge("options" => user_settings["options"])
end
|
.notifications_config ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/unotifier.rb', line 25
def self.notifications_config
@notifications_config ||= if configuration.notifications_path.is_a?(Array)
configuration.notifications_path.each_with_object({}) do |path, config|
config.deep_merge!(YAML.load_file(path))
end
else
YAML.load_file(configuration.notifications_path)
end
rescue Errno::ENOENT
raise NotificationsConfigNotFoundError, configuration.notifications_path
end
|
.notify(key, recepients, params = {}) ⇒ Object
69
70
71
72
73
74
75
|
# File 'lib/unotifier.rb', line 69
def self.notify(key, recepients, params = {})
targets = recepients.is_a?(Enumerable) ? recepients : [recepients]
targets.each do |target|
notify_target(key, target, params)
end
end
|
.notify_external(key, target, params) ⇒ Object
105
106
107
|
# File 'lib/unotifier.rb', line 105
def self.notify_external(key, target, params)
notify(key, target, params.merge(external_only: true))
end
|
.notify_onsite(key, target, params) ⇒ Object
101
102
103
|
# File 'lib/unotifier.rb', line 101
def self.notify_onsite(key, target, params)
notify(key, target, params.merge(onsite_only: true))
end
|
.notify_target(key, target, params = {}) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/unotifier.rb', line 77
def self.notify_target(key, target, params = {})
locale_key = locale_key_for(key, params)
urgency = urgency_for(key, params)
notification = configuration.resource_class.new(
key: key,
target: target,
link: params[:link],
autohide_delay: params[:autohide_delay],
urgency: urgency,
title: configuration.localization_provider.t(
"#{locale_key}.title", params.merge(locale: target.locale)
),
body: configuration.localization_provider.t(
"#{locale_key}.body", params.merge(locale: target.locale, default: "")
)
)
notification.save!
UserNotifier.call(notification, params)
SystemNotifier.call(notification, params[:system])
end
|
.reload! ⇒ Object
37
38
39
40
|
# File 'lib/unotifier.rb', line 37
def self.reload!
@configuration = nil
@notifications_config = nil
end
|
.urgency_for(key, params = {}) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/unotifier.rb', line 130
def self.urgency_for(key, params = {})
config = load_notification!(key)["user"]
if config["target"].is_a?(Hash)
urgency = config.dig("target", params[:target], "urgency")
raise UnknownTargetError.new(key, params[:target]) unless urgency
urgency
else
config["urgency"]
end
end
|