active_delivery 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +127 -2
- data/lib/active_delivery/base.rb +6 -0
- data/lib/active_delivery/lines/base.rb +2 -0
- data/lib/active_delivery/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba024e906a6a7bdf0dafd6a4e83ecefc7121b475ec9f9e7dfdadaacf4a2e04c0
|
4
|
+
data.tar.gz: 1045c894dc23a4d90282fac45a158af962839d9cd026ff8d5331371141c5c120
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f54fd937683e8e70871ece3900d2f75026e0674e316cc8969ad5066e5757b4982697f3e2c5e59d9969497c54874b6c9ea107ee08eae502d1ef87f7013234943
|
7
|
+
data.tar.gz: 4af2720f68788f2cc35d4acbdfc2ed6870a809d075b08a16663ddbd459ebc734939eab1208a4acfc6fd46dc1ac2a19603d1de5ccfbfbeb1d5ff1a11ae749ff45
|
data/README.md
CHANGED
@@ -43,6 +43,19 @@ end
|
|
43
43
|
|
44
44
|
- better testability (see [Testing](#testing)).
|
45
45
|
|
46
|
+
## Installation
|
47
|
+
|
48
|
+
Add this line to your application's Gemfile:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
gem 'active_delivery'
|
52
|
+
```
|
53
|
+
|
54
|
+
And then execute:
|
55
|
+
|
56
|
+
```sh
|
57
|
+
$ bundle
|
58
|
+
```
|
46
59
|
|
47
60
|
## Usage
|
48
61
|
|
@@ -55,7 +68,7 @@ class PostsDelivery < ActiveDelivery::Base
|
|
55
68
|
end
|
56
69
|
```
|
57
70
|
|
58
|
-
It acts like a proxy in front of the different delivery channels (i.e. mailers, notifiers). That means that calling a method on delivery class invokes the same method on the corresponding class, e.g.:
|
71
|
+
It acts like a proxy in front of the different delivery channels (i.e. mailers, notifiers). That means that calling a method on delivery class invokes the same method on the corresponding _sender_ class, e.g.:
|
59
72
|
|
60
73
|
```ruby
|
61
74
|
PostsDelivery.notify(:published, user, post)
|
@@ -63,7 +76,7 @@ PostsDelivery.notify(:published, user, post)
|
|
63
76
|
# under the hood it calls
|
64
77
|
PostsMailer.published(user, post).deliver_later
|
65
78
|
|
66
|
-
# and if you have a notifier (or
|
79
|
+
# and if you have a notifier (or any other line, see below)
|
67
80
|
PostsNotifier.published(user, post).notify_later
|
68
81
|
```
|
69
82
|
|
@@ -138,6 +151,118 @@ specify "when event is not found" do
|
|
138
151
|
end
|
139
152
|
```
|
140
153
|
|
154
|
+
*NOTE:** test mode activated automatically if `RAILS_ENV` or `RACK_ENV` env variable is equal to "test". Otherwise add `require "active_delivery/testing"` to your `spec_helper.rb` / `rails_helper.rb` manually.
|
155
|
+
|
156
|
+
## Custom "lines"
|
157
|
+
|
158
|
+
_Line_ class describes the way you want to _transfer_ your deliveries.
|
159
|
+
|
160
|
+
Out-of-the box we provide only Action Mailer _line_.
|
161
|
+
|
162
|
+
Line connects _delivery_ to the _sender_ class responsible for sending notifications.
|
163
|
+
|
164
|
+
If you want to use parameterized deliveries, your _sender_ class must respond to `.with(params)` method.
|
165
|
+
|
166
|
+
Assume that we want to send messages via _pigeons_ and we have the following sender class:
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
class EventPigeon
|
170
|
+
class << self
|
171
|
+
# Add `.with` method as an alias
|
172
|
+
alias with new
|
173
|
+
|
174
|
+
# delegate delivery action to instance
|
175
|
+
def message_arrived(*args)
|
176
|
+
new.message_arrived(*arsg)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def initialize(params = {})
|
181
|
+
# do smth with params
|
182
|
+
end
|
183
|
+
|
184
|
+
def message_arrived(msg)
|
185
|
+
# send pigeon with the message
|
186
|
+
end
|
187
|
+
end
|
188
|
+
```
|
189
|
+
|
190
|
+
Now we want to add a _pigeon_ line to our `EventDelivery`, that is we want to send pigeons when
|
191
|
+
we call `EventDelivery.notify(:message_arrived, "ping-ping!")`.
|
192
|
+
|
193
|
+
Line class has the following API:
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
|
197
|
+
class PigeonLine < ActiveDelivery::Lines::Base
|
198
|
+
# This method is used to infer sender class
|
199
|
+
# `name` is the name of the delivery class
|
200
|
+
def resolve_class(name)
|
201
|
+
name.gsub(/Delivery$/, "Pigeon").safe_constantize
|
202
|
+
end
|
203
|
+
|
204
|
+
# This method should return true if sender recognizes the delivery action
|
205
|
+
def notify?(delivery_action)
|
206
|
+
# `handler_class` is available within the line instance
|
207
|
+
sender_class.respond_to?(delivery_action)
|
208
|
+
end
|
209
|
+
|
210
|
+
# Called when we want to send message synchronously
|
211
|
+
# `sender` here either `sender_class` or `sender_class.with(params)`
|
212
|
+
# if params passed.
|
213
|
+
def notify_now(sender, delivery_action, *args)
|
214
|
+
# For example, our EventPigeon class returns some `Pigeon` object
|
215
|
+
pigeon = sender.public_send(delivery_action, *args)
|
216
|
+
# PigeonLaunchService do all the sending job
|
217
|
+
PigeonService.launch pigeon
|
218
|
+
end
|
219
|
+
|
220
|
+
# Called when we want to send message asynchronously.
|
221
|
+
# For example, you can use background job here.
|
222
|
+
def notify_later(sender, delivery_action, *args)
|
223
|
+
pigeon = sender.public_send(delivery_action, *args)
|
224
|
+
# PigeonLaunchService do all the sending job
|
225
|
+
PigeonLaunchJob.perform_later pigeon
|
226
|
+
end
|
227
|
+
end
|
228
|
+
```
|
229
|
+
|
230
|
+
**NOTE**: we fallback to superclass's sender class if `resolve_class` returns nil.
|
231
|
+
You can disable automatic inference of sender classes by marking delivery as _abstract_:
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
# we don't not want to use ApplicationMailer by default, don't we?
|
235
|
+
class ApplicationDelivery < ActiveDelivery::Base
|
236
|
+
self.abstract_class = true
|
237
|
+
end
|
238
|
+
```
|
239
|
+
|
240
|
+
Final step is to register the line within your delivery class:
|
241
|
+
|
242
|
+
```ruby
|
243
|
+
class EventDelivery < ActiveDelivery::Base
|
244
|
+
# under the hood a new instance of PigeonLine is created
|
245
|
+
# and used to send pigeons!
|
246
|
+
register_line :pigeon, PigeonLine
|
247
|
+
|
248
|
+
# you can pass additional options to customize your line
|
249
|
+
# (and use multiple pigeons lines with different configuration)
|
250
|
+
#
|
251
|
+
# register_line :pigeon, PigeonLine, namespace: "AngryPigeons"
|
252
|
+
#
|
253
|
+
# now you can explicitly specify pigeon class
|
254
|
+
# pigeon MyCustomPigeon
|
255
|
+
#
|
256
|
+
# or define pigeon specific callbacks
|
257
|
+
#
|
258
|
+
# before_notify :ensure_pigeon_is_not_dead, on: :pigeon
|
259
|
+
end
|
260
|
+
```
|
261
|
+
|
262
|
+
## Related projects
|
263
|
+
|
264
|
+
- [`abstract_notifier`](https://github.com/palkan/abstract_notifier) – Action Mailer-like interface for text-based notifications.
|
265
|
+
|
141
266
|
## Contributing
|
142
267
|
|
143
268
|
Bug reports and pull requests are welcome on GitHub at https://github.com/palkan/active_delivery.
|
data/lib/active_delivery/base.rb
CHANGED
@@ -32,6 +32,8 @@ module ActiveDelivery
|
|
32
32
|
# See https://api.rubyonrails.org/classes/ActionMailer/Parameterized.html
|
33
33
|
class Base
|
34
34
|
class << self
|
35
|
+
attr_accessor :abstract_class
|
36
|
+
|
35
37
|
alias with new
|
36
38
|
|
37
39
|
# Enqueues delivery (i.e. uses #deliver_later for mailers)
|
@@ -70,6 +72,10 @@ module ActiveDelivery
|
|
70
72
|
end
|
71
73
|
CODE
|
72
74
|
end
|
75
|
+
|
76
|
+
def abstract_class?
|
77
|
+
abstract_class == true
|
78
|
+
end
|
73
79
|
end
|
74
80
|
|
75
81
|
attr_reader :params
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_delivery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Dementyev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|