dallal 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/dallal.rb +2 -1
- data/lib/dallal/configuration.rb +11 -2
- data/lib/dallal/events/observer.rb +1 -0
- data/lib/dallal/notification.rb +5 -5
- data/lib/dallal/notifications/sms_notification.rb +22 -3
- data/lib/dallal/notifiers/sms_notifier.rb +17 -1
- data/lib/dallal/version.rb +2 -2
- data/lib/generators/dallal/install/templates/dallal.rb +11 -7
- data/spec/dallal/configuration_spec.rb +4 -1
- data/spec/dallal/events/event_subscriber_spec.rb +1 -0
- data/spec/dallal/notification_spec.rb +135 -8
- data/spec/dallal/notifiers/notifier_spec.rb +1 -0
- data/spec/dallal/notifiers/sms_notifier_spec.rb +26 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/20160805224858_add_phone_number_to_users.rb +5 -0
- data/spec/dummy/db/schema.rb +5 -4
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +15 -0
- data/spec/dummy/log/test.log +29539 -0
- data/spec/factories/users.rb +1 -0
- data/spec/supports/notifiers.rb +0 -0
- metadata +27 -6
- data/README.rdoc +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48bb2a87abb110aaab509c6ebaf377f63260f7aa
|
4
|
+
data.tar.gz: 9ceaa80e8deabc46baa7c4ed0a688bbb5c0036c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 426f3fc734c51e3779d9a992de79fe1a6791b990445ebece635307d844ce57d744932930d690eed734ccf17b7b0babaa470347759871d3dd2952998bb3d9d92d
|
7
|
+
data.tar.gz: 9fcbf3ec632c534d11ae99416f8db2c8e8a321319b57dab4a7acc9da2cee0f8981fda62319fef5445da6204bae5077edb1354257612565a757177d0fc3c066b2
|
data/lib/dallal.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require "dallal/engine" if defined?(Rails)
|
2
|
+
require 'dallal/configuration'
|
2
3
|
require 'dallal/notification'
|
3
4
|
require 'dallal/notifiers/notifier'
|
4
5
|
require 'dallal/events/events'
|
5
|
-
require 'dallal/configuration'
|
6
6
|
require 'dallal/events/event_publisher'
|
7
|
+
require 'twilio-ruby'
|
7
8
|
|
8
9
|
module Dallal
|
9
10
|
extend Configuration
|
data/lib/dallal/configuration.rb
CHANGED
@@ -2,8 +2,9 @@ require 'ostruct'
|
|
2
2
|
|
3
3
|
module Dallal
|
4
4
|
module Configuration
|
5
|
-
CURRENT_ATTRS = [:user_class_name, :dallal_class_name,
|
6
|
-
:
|
5
|
+
CURRENT_ATTRS = [:user_class_name, :dallal_class_name, :enabled,
|
6
|
+
:email_layout, :from_email, :from_name,
|
7
|
+
:twilio_account_id, :twilio_auth_token, :sms_from].freeze
|
7
8
|
DEPRECATED_ATTRS = [].freeze
|
8
9
|
CONFIG_ATTRS = (CURRENT_ATTRS + DEPRECATED_ATTRS).freeze
|
9
10
|
|
@@ -42,12 +43,20 @@ module Dallal
|
|
42
43
|
|
43
44
|
private
|
44
45
|
def set_default_values
|
46
|
+
# App config
|
45
47
|
self.user_class_name = 'User'
|
46
48
|
self.dallal_class_name = 'Dallal'
|
47
49
|
self.enabled = true
|
50
|
+
|
51
|
+
# Email config
|
48
52
|
self.email_layout = 'mailer'
|
49
53
|
self.from_email = 'foo@bar.xyz'
|
50
54
|
self.from_name = 'just a name'
|
55
|
+
|
56
|
+
# SMS config
|
57
|
+
self.twilio_account_id = 'YOUR TWILIO ACCOUNT ID'
|
58
|
+
self.twilio_auth_token = 'TWILIO_AUTH_TOKEN'
|
59
|
+
self.sms_from = 'Sender phone number'
|
51
60
|
end
|
52
61
|
end
|
53
62
|
end
|
@@ -26,6 +26,7 @@ module Dallal
|
|
26
26
|
def self.create_notification(id:, event:)
|
27
27
|
# TODO This loops on all callbacks. Rethink this
|
28
28
|
# method and implement it differently. Add listeners?
|
29
|
+
# Change callbacks data structure
|
29
30
|
callbacks.each do |callback|
|
30
31
|
next unless callback[:on].include?(event)
|
31
32
|
obj = model_class.constantize.find(id)
|
data/lib/dallal/notification.rb
CHANGED
@@ -42,16 +42,16 @@ module Dallal
|
|
42
42
|
|
43
43
|
def dispatch!
|
44
44
|
validate!
|
45
|
-
@notifiers.each
|
45
|
+
@notifiers.each do |notifier|
|
46
|
+
# notifier.validate!
|
47
|
+
notifier.notify!
|
48
|
+
end
|
46
49
|
@notifiers.each { |n| n.persist! } if persist?
|
47
50
|
end
|
48
51
|
|
49
52
|
private
|
50
53
|
# TODO Implement this
|
51
|
-
#
|
52
|
-
# on email notification when template is nil throw error
|
53
|
-
# on sms notification when payload is nil throw an error
|
54
|
-
# on any other notifiers add a validation logic here
|
54
|
+
# Add validation logic here
|
55
55
|
def validate!
|
56
56
|
if false
|
57
57
|
raise "You have not defined \'notify\' in \'with\' block for #{class_name} notifier"
|
@@ -2,10 +2,29 @@ require 'dallal/notifications/base_notification'
|
|
2
2
|
module Dallal
|
3
3
|
module Notifications
|
4
4
|
class SmsNotification < BaseNotification
|
5
|
-
attr_reader :
|
5
|
+
attr_reader :to
|
6
|
+
attr_reader :from
|
7
|
+
attr_reader :body
|
8
|
+
|
9
|
+
def initialize(notification, target)
|
10
|
+
super(notification, target)
|
11
|
+
@from = Dallal.configuration.sms_from
|
12
|
+
end
|
6
13
|
|
7
|
-
def
|
8
|
-
|
14
|
+
def template template
|
15
|
+
raise NotImplementedError
|
16
|
+
end
|
17
|
+
|
18
|
+
def message message
|
19
|
+
@body = message
|
20
|
+
end
|
21
|
+
|
22
|
+
def recipient recipient_number
|
23
|
+
@to = recipient_number
|
24
|
+
end
|
25
|
+
|
26
|
+
def to
|
27
|
+
@to || target.phone_number
|
9
28
|
end
|
10
29
|
|
11
30
|
def notifier
|
@@ -3,12 +3,28 @@ module Dallal
|
|
3
3
|
module Notifiers
|
4
4
|
class SmsNotifier < Notifier
|
5
5
|
|
6
|
-
|
6
|
+
class << self
|
7
|
+
def client
|
8
|
+
@client ||= Twilio::REST::Client.new(
|
9
|
+
Dallal.configuration.twilio_account_id,
|
10
|
+
Dallal.configuration.twilio_auth_token)
|
11
|
+
end
|
7
12
|
end
|
8
13
|
|
14
|
+
def notify!
|
15
|
+
client.messages.create(from: notification.from,
|
16
|
+
to: notification.to,
|
17
|
+
body: notification.body)
|
18
|
+
end
|
9
19
|
|
10
20
|
def persist!
|
11
21
|
end
|
22
|
+
|
23
|
+
def client
|
24
|
+
self.class.client
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
12
28
|
end
|
13
29
|
end
|
14
30
|
end
|
data/lib/dallal/version.rb
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
Dallal.configure do |config|
|
2
2
|
# Set the user class name of your app
|
3
|
-
config.user_class_name = 'User'
|
3
|
+
# config.user_class_name = 'User'
|
4
4
|
|
5
5
|
# Set up user notification class name
|
6
|
-
config.dallal_class_name = '
|
6
|
+
# config.dallal_class_name = 'DallalNo'
|
7
7
|
|
8
|
-
# Enable / Disable notifications
|
8
|
+
# Enable / Disable notifications globally
|
9
9
|
config.enabled = true
|
10
10
|
|
11
|
-
#
|
12
|
-
|
11
|
+
# Email config
|
12
|
+
config.email_layout = 'mailer'
|
13
|
+
config.from_email = 'info@yourdomain.com'
|
14
|
+
config.from_name = 'System Display Name'
|
13
15
|
|
14
|
-
#
|
15
|
-
|
16
|
+
# Twulio SMS configuration
|
17
|
+
config.twilio_account_id = 'YOUR TWILIO ACCOUNT ID'
|
18
|
+
config.twilio_auth_token = 'YOUR TWILIO_AUTH_TOKEN'
|
19
|
+
config.sms_from = 'Your Twilio phone phone number'
|
16
20
|
end
|
@@ -45,7 +45,10 @@ describe Dallal::Configuration do
|
|
45
45
|
enabled: true,
|
46
46
|
email_layout: 'mailer',
|
47
47
|
from_email: 'foo@bar.xyz',
|
48
|
-
from_name: 'just a name'
|
48
|
+
from_name: 'just a name',
|
49
|
+
twilio_account_id: 'YOUR TWILIO ACCOUNT ID',
|
50
|
+
twilio_auth_token: 'TWILIO_AUTH_TOKEN',
|
51
|
+
sms_from: 'Sender phone number',
|
49
52
|
})
|
50
53
|
end
|
51
54
|
end
|
@@ -27,7 +27,7 @@ describe Dallal::Notification do
|
|
27
27
|
Dallal::Notification.new(event: :create, model_class: 'Post', opts: { a: 1 }, _object: @post)
|
28
28
|
end
|
29
29
|
|
30
|
-
it 'should get
|
30
|
+
it 'should get a single target and call the block' do
|
31
31
|
executed = false
|
32
32
|
subject.notify(@user) do
|
33
33
|
executed = true
|
@@ -36,6 +36,17 @@ describe Dallal::Notification do
|
|
36
36
|
expect(subject.targets).to eq [@user]
|
37
37
|
end
|
38
38
|
|
39
|
+
it 'should get multiple target and call the block' do
|
40
|
+
executed = false
|
41
|
+
other = double("OtherUser")
|
42
|
+
subject.notify(@user, other) do
|
43
|
+
executed = true
|
44
|
+
end
|
45
|
+
|
46
|
+
expect(executed).to eq true
|
47
|
+
expect(subject.targets).to eq [@user, other]
|
48
|
+
end
|
49
|
+
|
39
50
|
context 'notify with nested with block' do
|
40
51
|
it 'calls with block' do
|
41
52
|
executed = false
|
@@ -53,14 +64,18 @@ describe Dallal::Notification do
|
|
53
64
|
end
|
54
65
|
|
55
66
|
it 'evaluates correctly an sms block' do
|
67
|
+
subject.define_singleton_method(:post) {}
|
68
|
+
allow(subject).to receive(:post).and_return(@post)
|
56
69
|
subject.notify(@user) do
|
57
70
|
with :sms do
|
58
|
-
|
71
|
+
message "Message #{post.user.email}"
|
72
|
+
recipient post.user.phone_number
|
59
73
|
end
|
60
74
|
end
|
61
75
|
notifier = subject.notifiers.first
|
62
76
|
expect(notifier).to be_a(Dallal::Notifiers::SmsNotifier)
|
63
|
-
expect(notifier.notification.
|
77
|
+
expect(notifier.notification.body).to eq("Message #{@post.user.email}")
|
78
|
+
expect(notifier.notification.to).to eq @post.user.phone_number
|
64
79
|
expect(subject.targets).to eq([@user])
|
65
80
|
end
|
66
81
|
|
@@ -101,14 +116,20 @@ describe Dallal::Notification do
|
|
101
116
|
end
|
102
117
|
end
|
103
118
|
|
104
|
-
context "multiple
|
119
|
+
context "multiple types" do
|
105
120
|
it 'fills properties correctly' do
|
121
|
+
subject.define_singleton_method(:post) do
|
122
|
+
@post
|
123
|
+
end
|
124
|
+
allow(subject).to receive(:post).and_return(@post)
|
125
|
+
|
106
126
|
subject.notify(@user) do
|
107
127
|
with :email do
|
108
128
|
template :email_template
|
109
129
|
end
|
110
130
|
with :sms do
|
111
|
-
|
131
|
+
message "Message #{post.user.email}"
|
132
|
+
recipient post.user.phone_number
|
112
133
|
end
|
113
134
|
end
|
114
135
|
email_notifier = subject.notifiers.first
|
@@ -118,9 +139,41 @@ describe Dallal::Notification do
|
|
118
139
|
expect(email_notifier).to be_a(Dallal::Notifiers::EmailNotifier)
|
119
140
|
expect(email_notifier.notification.template_name).to eq :email_template
|
120
141
|
expect(sms_notifier).to be_a(Dallal::Notifiers::SmsNotifier)
|
121
|
-
expect(sms_notifier.notification.
|
142
|
+
expect(sms_notifier.notification.body).to eq "Message #{@post.user.email}"
|
143
|
+
expect(sms_notifier.notification.to).to eq @post.user.phone_number
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context "multiple #notify blocks" do
|
148
|
+
before do
|
149
|
+
subject.define_singleton_method(:post) do
|
150
|
+
@post
|
151
|
+
end
|
152
|
+
allow(subject).to receive(:post).and_return(@post)
|
122
153
|
end
|
123
|
-
|
154
|
+
it 'evaluates multiple efinitions of notify' do
|
155
|
+
subject.notify(@user) do
|
156
|
+
with :email do
|
157
|
+
template :a_template
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
second_user = double("SecondUser")
|
162
|
+
subject.notify(second_user) do
|
163
|
+
with :email do
|
164
|
+
template :b_template
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
expect(subject.notifiers.size).to eq 2
|
169
|
+
first = subject.notifiers[0]
|
170
|
+
expect(first.notification.target).to eq @user
|
171
|
+
expect(first.notification.template_name).to eq :a_template
|
172
|
+
second = subject.notifiers[1]
|
173
|
+
expect(second.notification.target).to eq second_user
|
174
|
+
expect(second.notification.template_name).to eq :b_template
|
175
|
+
end
|
176
|
+
end
|
124
177
|
end
|
125
178
|
|
126
179
|
describe 'dispatch!' do
|
@@ -139,12 +192,86 @@ describe Dallal::Notification do
|
|
139
192
|
subject.dispatch!
|
140
193
|
end
|
141
194
|
end
|
142
|
-
context 'single email notification' do
|
195
|
+
context 'single email notification with persistance' do
|
143
196
|
pending
|
144
197
|
end
|
198
|
+
|
199
|
+
context 'single sms notification' do
|
200
|
+
it 'send an sms notification' do
|
201
|
+
subject.notify(@user) do
|
202
|
+
with :sms do
|
203
|
+
message "a message"
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
expect(subject.notifiers.size).to eq 1
|
208
|
+
expect(subject.notifiers.first).to receive(:notify!)
|
209
|
+
expect(subject.notifiers.first).to_not receive(:persist!)
|
210
|
+
|
211
|
+
subject.dispatch!
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
context 'single sms notification with persistance' do
|
216
|
+
pending
|
217
|
+
end
|
218
|
+
|
145
219
|
context 'multiple notifications' do
|
146
220
|
pending
|
147
221
|
end
|
222
|
+
|
223
|
+
context "multiple users to be notified" do
|
224
|
+
it 'sends an notification to each one of them' do
|
225
|
+
other = double("OtherUser")
|
226
|
+
subject.notify(@user, other) do
|
227
|
+
with :sms do
|
228
|
+
message 'a message'
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
expect(subject.notifiers.size).to eq 2
|
233
|
+
expect(subject.notifiers.first).to receive(:notify!)
|
234
|
+
expect(subject.notifiers.last).to receive(:notify!)
|
235
|
+
subject.dispatch!
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
describe "#with" do
|
241
|
+
pending
|
242
|
+
end
|
243
|
+
|
244
|
+
describe "#get_notifier" do
|
245
|
+
before do
|
246
|
+
subject.define_singleton_method(:post) {}
|
247
|
+
allow(subject).to receive(:post).and_return(@post)
|
248
|
+
end
|
249
|
+
context "sms notification" do
|
250
|
+
context "when recipient is defined" do
|
251
|
+
it 'creates a notification and return an sms notifier' do
|
252
|
+
blk = proc { message "a message"; recipient post.user }
|
253
|
+
notifier = subject.send(:get_notifier, :sms, @user, &blk)
|
254
|
+
expect(notifier).to be_a Dallal::Notifiers::SmsNotifier
|
255
|
+
notification = notifier.notification
|
256
|
+
expect(notification.target).to eq @user
|
257
|
+
expect(notification.body).to eq "a message"
|
258
|
+
expect(notification.to).to eq @post.user
|
259
|
+
expect(notification.from).to eq Dallal.configuration.sms_from
|
260
|
+
end
|
261
|
+
end
|
262
|
+
context "when recipient is not defined" do
|
263
|
+
it 'gets the number from target user' do
|
264
|
+
blk = proc { message "a message" }
|
265
|
+
notifier = subject.send(:get_notifier, :sms, @user, &blk)
|
266
|
+
expect(notifier).to be_a Dallal::Notifiers::SmsNotifier
|
267
|
+
notification = notifier.notification
|
268
|
+
expect(notification.target).to eq @user
|
269
|
+
expect(notification.body).to eq "a message"
|
270
|
+
expect(notification.to).to eq @post.user.phone_number
|
271
|
+
expect(notification.from).to eq Dallal.configuration.sms_from
|
272
|
+
end
|
273
|
+
end
|
274
|
+
end
|
148
275
|
end
|
149
276
|
|
150
277
|
describe 'persist?' do
|
@@ -4,6 +4,7 @@ describe Dallal::Notifiers::Notifier do
|
|
4
4
|
let(:notification) { double("Notification") }
|
5
5
|
subject { Dallal::Notifiers::Notifier.new(notification) }
|
6
6
|
it { expect(subject).to be_a Dallal::AbstractInterface }
|
7
|
+
it { expect(subject.notification).to eq(notification) }
|
7
8
|
|
8
9
|
describe "#persist!" do
|
9
10
|
it 'should raise an error' do
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
describe Dallal::Notifiers::SmsNotifier do
|
4
|
+
let(:notification) do
|
5
|
+
double("Notification", body: 'a message', from: '111', to: '222')
|
6
|
+
end
|
7
|
+
subject { Dallal::Notifiers::SmsNotifier.new(notification) }
|
8
|
+
|
9
|
+
it { expect(subject).to be_a(Dallal::Notifiers::Notifier) }
|
10
|
+
|
11
|
+
describe '.client' do
|
12
|
+
it 'should return a twillio clinet' do
|
13
|
+
client = subject.client
|
14
|
+
expect(client).to be_a Twilio::REST::Client
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#notify' do
|
19
|
+
it 'should create a message with twillio reset client' do
|
20
|
+
expect(subject.client).to receive_message_chain(:messages, :create).with(from: notification.from, to: notification.to, body: notification.body)
|
21
|
+
subject.notify!
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#persist'
|
26
|
+
end
|