-
Notifications
You must be signed in to change notification settings - Fork 15
/
base.rb
223 lines (174 loc) · 5.9 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# frozen_string_literal: true
module AbstractNotifier
# NotificationDelivery payload wrapper which contains
# information about the current notifier class
# and knows how to trigger the delivery
class NotificationDelivery
attr_reader :action_name, :notifier_class
def initialize(notifier_class, action_name, params: {}, args: [], kwargs: {})
@notifier_class = notifier_class
@action_name = action_name
@params = params
@args = args
@kwargs = kwargs
end
def processed
return @processed if instance_variable_defined?(:@processed)
@processed = notifier.process_action(action_name, *args, **kwargs) || Notification.new(nil)
end
alias_method :notification, :processed
def notify_later(**)
if notifier_class.async_adapter.respond_to?(:enqueue_delivery)
notifier_class.async_adapter.enqueue_delivery(self, **)
else
notifier_class.async_adapter.enqueue(notifier_class.name, action_name, params:, args:, kwargs:)
end
end
def notify_now
return unless notification.payload
notifier.deliver!(notification)
end
def delivery_params = {params:, args:, kwargs:}
private
attr_reader :params, :args, :kwargs
def notifier
@notifier ||= notifier_class.new(action_name, **params)
end
end
# Notification object contains the compiled payload to be delivered
class Notification
attr_reader :payload
def initialize(payload)
@payload = payload
end
end
# Base class for notifiers
class Base
class ParamsProxy
attr_reader :notifier_class, :params
def initialize(notifier_class, params)
@notifier_class = notifier_class
@params = params
end
# rubocop:disable Style/MethodMissingSuper
def method_missing(method_name, *args, **kwargs)
NotificationDelivery.new(notifier_class, method_name, params:, args:, kwargs:)
end
# rubocop:enable Style/MethodMissingSuper
def respond_to_missing?(*)
notifier_class.respond_to_missing?(*)
end
end
class << self
attr_writer :driver
def driver
return @driver if instance_variable_defined?(:@driver)
@driver =
if superclass.respond_to?(:driver)
superclass.driver
else
raise "Driver not found for #{name}. " \
"Please, specify driver via `self.driver = MyDriver`"
end
end
def async_adapter=(args)
adapter, options = Array(args)
@async_adapter = AsyncAdapters.lookup(adapter, options)
end
def async_adapter
return @async_adapter if instance_variable_defined?(:@async_adapter)
@async_adapter =
if superclass.respond_to?(:async_adapter)
superclass.async_adapter
else
AbstractNotifier.async_adapter
end
end
def default(method_name = nil, **hargs, &block)
return @defaults_generator = block if block
return @defaults_generator = proc { send(method_name) } unless method_name.nil?
@default_params =
if superclass.respond_to?(:default_params)
superclass.default_params.merge(hargs).freeze
else
hargs.freeze
end
end
def defaults_generator
return @defaults_generator if instance_variable_defined?(:@defaults_generator)
@defaults_generator =
if superclass.respond_to?(:defaults_generator)
superclass.defaults_generator
end
end
def default_params
return @default_params if instance_variable_defined?(:@default_params)
@default_params =
if superclass.respond_to?(:default_params)
superclass.default_params.dup
else
{}
end
end
def method_missing(method_name, *args, **kwargs)
if action_methods.include?(method_name.to_s)
NotificationDelivery.new(self, method_name, args:, kwargs:)
else
super
end
end
def with(params)
ParamsProxy.new(self, params)
end
def respond_to_missing?(method_name, _include_private = false)
action_methods.include?(method_name.to_s) || super
end
# See https://github.com/rails/rails/blob/b13a5cb83ea00d6a3d71320fd276ca21049c2544/actionpack/lib/abstract_controller/base.rb#L74
def action_methods
@action_methods ||= begin
# All public instance methods of this class, including ancestors
methods = (public_instance_methods(true) -
# Except for public instance methods of Base and its ancestors
Base.public_instance_methods(true) +
# Be sure to include shadowed public instance methods of this class
public_instance_methods(false))
methods.map!(&:to_s)
methods.to_set
end
end
end
attr_reader :params, :notification_name
def initialize(notification_name, **params)
@notification_name = notification_name
@params = params.freeze
end
def process_action(...)
public_send(...)
end
def deliver!(notification)
self.class.driver.call(notification.payload)
end
def notification(**payload)
merge_defaults!(payload)
payload[:body] = implicit_payload_body unless payload.key?(:body)
raise ArgumentError, "Notification body must be present" if
payload[:body].nil? || payload[:body].empty?
@notification = Notification.new(payload)
end
private
def implicit_payload_body
# no-op — override to provide custom logic
end
def merge_defaults!(payload)
defaults =
if self.class.defaults_generator
instance_exec(&self.class.defaults_generator)
else
self.class.default_params
end
defaults.each do |k, v|
payload[k] = v unless payload.key?(k)
end
end
end
end