tolliver 2.0.0 → 2.1.3
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/README.md +51 -3
- data/app/views/tolliver/notification_mailer/notify.html.erb +1 -1
- data/db/migrate/20210415120000_add_notification_attachments_url.rb +6 -0
- data/lib/tolliver.rb +26 -11
- data/lib/tolliver/jobs/batch_policy_job.rb +19 -0
- data/lib/tolliver/jobs/delivery_job.rb +18 -0
- data/lib/tolliver/mailers/notification_mailer.rb +3 -3
- data/lib/tolliver/models/notification.rb +3 -3
- data/lib/tolliver/models/notification_attachment.rb +12 -0
- data/lib/tolliver/services/{delivery.rb → delivery_service.rb} +2 -13
- data/lib/tolliver/services/methods/email.rb +14 -1
- data/lib/tolliver/services/methods/email/mailgun.rb +55 -0
- data/lib/tolliver/services/methods/email/smtp.rb +28 -0
- data/lib/tolliver/services/methods/sms.rb +5 -2
- data/lib/tolliver/services/methods/sms/plivo.rb +6 -7
- data/lib/tolliver/services/{notification.rb → notification_service.rb} +22 -8
- data/lib/tolliver/services/policies/batch.rb +7 -15
- metadata +9 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45746e4382ed1970ae1a7dc297f096d0616c824d4bf319949931372845aeb74d
|
4
|
+
data.tar.gz: 2445b15788d28a73cbde7d0e2f3e28cf090a9c886e9c56d4c4acb2fa9a7edc42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6693df6712589159a779c7eca498047d5a59f6db0992716622a821c0fc6d726739d32df94be7598a2b5705e3b45091e4f48744b06cdc2f4ff607b3359d3d8a7d
|
7
|
+
data.tar.gz: 0036c0fa396f8ace9fef5502b3a5984b47246fc6305dca95d9a45bcd10388e0057c85cef4d07adda19bc5fea738fa7b73b285e60fbc6f23d3ffeac086fe914e6
|
data/README.md
CHANGED
@@ -3,8 +3,8 @@ A long serving Junior Postman Tolliver Groat. With this gem you can create
|
|
3
3
|
notifications based on templates defined by the system administrator and
|
4
4
|
send it in a batch with different delivery methods:
|
5
5
|
|
6
|
-
- E-mail
|
7
|
-
- SMS
|
6
|
+
- E-mail with SMTP aor Mailgun provider
|
7
|
+
- SMS with Plivo provider
|
8
8
|
- Whatever custom delivery method
|
9
9
|
|
10
10
|
## Installation
|
@@ -20,13 +20,29 @@ Add database migrations to you application (you can modify DB structure accordin
|
|
20
20
|
$ rake tolliver:install:migrations
|
21
21
|
$ rake db:migrate
|
22
22
|
|
23
|
+
### Plivo support
|
24
|
+
|
25
|
+
Add gem to your Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'plivo'
|
29
|
+
```
|
30
|
+
|
31
|
+
### Mailgun support
|
32
|
+
|
33
|
+
Add gem to your Gemfile:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
gem 'mailgun-ruby'
|
37
|
+
```
|
38
|
+
|
23
39
|
## Configuration
|
24
40
|
|
25
41
|
You can configure module through `config/initializers/tolliver.rb` file:
|
26
42
|
|
27
43
|
```ruby
|
28
44
|
Tolliver.setup do |config|
|
29
|
-
config.
|
45
|
+
config.email_sender = 'no-reply@domain.com'
|
30
46
|
config.delivery_methods = [
|
31
47
|
:email
|
32
48
|
]
|
@@ -42,11 +58,43 @@ Available options:
|
|
42
58
|
- notification_template_model
|
43
59
|
- email_sender
|
44
60
|
- email_sender_name
|
61
|
+
- email_provider
|
62
|
+
- email_provider_params
|
45
63
|
- sms_sender
|
46
64
|
- sms_provider
|
47
65
|
- sms_provider_params
|
48
66
|
- delivery_methods
|
49
67
|
|
68
|
+
### Plivo support
|
69
|
+
|
70
|
+
Set Plivo as SMS provider and add Plivo auth ID and token into Tolliver configuration file:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Tolliver.setup do |config|
|
74
|
+
...
|
75
|
+
config.sms_provider = :plivo
|
76
|
+
config.sms_provider_params = {
|
77
|
+
auth_id: 'secret',
|
78
|
+
auth_token: 'secret'
|
79
|
+
}
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
### Mailgun support
|
84
|
+
|
85
|
+
Set Mailgun as e-mail provider and add Mailgun API key and domain into Tolliver configuration file:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
Tolliver.setup do |config|
|
89
|
+
...
|
90
|
+
config.email_provider = :mailgun
|
91
|
+
config.email_provider_params = {
|
92
|
+
api_key: 'key-secret',
|
93
|
+
domain: 'domain.tld'
|
94
|
+
}
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
50
98
|
## Usage
|
51
99
|
|
52
100
|
To enter new notification into the system, just call `notify` method:
|
@@ -1 +1 @@
|
|
1
|
-
<%= @notification.message.html_safe %>
|
1
|
+
<%= @notification.message.to_s.html_safe %>
|
data/lib/tolliver.rb
CHANGED
@@ -20,12 +20,19 @@ require "tolliver/models/notification_receiver"
|
|
20
20
|
require "tolliver/models/notification_template"
|
21
21
|
|
22
22
|
# Services
|
23
|
-
require "tolliver/services/
|
24
|
-
require "tolliver/services/
|
23
|
+
require "tolliver/services/notification_service"
|
24
|
+
require "tolliver/services/delivery_service"
|
25
25
|
require "tolliver/services/policies/batch"
|
26
26
|
require "tolliver/services/policies/instantly"
|
27
27
|
require "tolliver/services/methods/email"
|
28
|
+
require "tolliver/services/methods/email/smtp"
|
29
|
+
require "tolliver/services/methods/email/mailgun"
|
28
30
|
require "tolliver/services/methods/sms"
|
31
|
+
require "tolliver/services/methods/sms/plivo"
|
32
|
+
|
33
|
+
# Jobs
|
34
|
+
require "tolliver/jobs/delivery_job"
|
35
|
+
require "tolliver/jobs/batch_policy_job"
|
29
36
|
|
30
37
|
# Mailers
|
31
38
|
require "tolliver/mailers/notification_mailer"
|
@@ -49,19 +56,19 @@ module Tolliver
|
|
49
56
|
# *************************************************************************
|
50
57
|
|
51
58
|
def self.notify(options)
|
52
|
-
Tolliver::Services::
|
59
|
+
Tolliver::Services::NotificationService.instance.notify(options)
|
53
60
|
end
|
54
61
|
|
55
62
|
def self.deliver(notification)
|
56
|
-
Tolliver::Services::
|
63
|
+
Tolliver::Services::DeliveryService.instance.deliver(notification)
|
57
64
|
end
|
58
65
|
|
59
66
|
def self.enqueue_for_delivery(notification)
|
60
|
-
Tolliver::Services::
|
67
|
+
Tolliver::Services::DeliveryService.instance.enqueue_for_delivery(notification)
|
61
68
|
end
|
62
69
|
|
63
70
|
def self.reset_delivery(notification)
|
64
|
-
Tolliver::Services::
|
71
|
+
Tolliver::Services::DeliveryService.instance.reset_delivery(notification)
|
65
72
|
end
|
66
73
|
|
67
74
|
# *************************************************************************
|
@@ -149,16 +156,24 @@ module Tolliver
|
|
149
156
|
mattr_accessor :email_sender_name
|
150
157
|
#@@email_sender_name = "Test sender" to be set in module initializer if needed
|
151
158
|
|
159
|
+
# Used e-mail provider
|
160
|
+
mattr_accessor :email_provider
|
161
|
+
@@email_provider = :smtp
|
162
|
+
|
163
|
+
# E-mail provider params
|
164
|
+
mattr_accessor :email_provider_params
|
165
|
+
@@email_provider_params = {}
|
166
|
+
|
167
|
+
# Used SMS provider
|
168
|
+
mattr_accessor :sms_sender
|
169
|
+
#@@sms_sender = "+420 123 456 789" to be set in module initializer if needed
|
170
|
+
|
152
171
|
# Used SMS provider
|
153
172
|
mattr_accessor :sms_provider
|
154
|
-
@@sms_provider =
|
173
|
+
@@sms_provider = nil
|
155
174
|
|
156
175
|
# SMS provider params
|
157
176
|
mattr_accessor :sms_provider_params
|
158
177
|
@@sms_provider_params = {}
|
159
178
|
|
160
|
-
# Used SMS provider
|
161
|
-
mattr_accessor :sms_sender
|
162
|
-
#@@sms_sender = "+420 123 456 789" to be set in module initializer if needed
|
163
|
-
|
164
179
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Tolliver
|
2
|
+
module Jobs
|
3
|
+
class BatchPolicyJob < ActiveJob::Base
|
4
|
+
queue_as :default
|
5
|
+
|
6
|
+
def perform(notification_delivery_id, batch_size)
|
7
|
+
|
8
|
+
# Instantiate notification delivery object
|
9
|
+
notification_delivery = Tolliver.notification_delivery_model.find_by_id(notification_delivery_id)
|
10
|
+
return nil if notification_delivery.nil? || notification_delivery.policy != :batch
|
11
|
+
|
12
|
+
# Call policy logic
|
13
|
+
policy_service = Tolliver::Services::Policies::Batch.new
|
14
|
+
policy_service.deliver_batch_and_enqueue(notification_delivery, batch_size)
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Tolliver
|
2
|
+
module Jobs
|
3
|
+
class DeliveryJob < ActiveJob::Base
|
4
|
+
queue_as :default
|
5
|
+
|
6
|
+
def perform(notification_id)
|
7
|
+
|
8
|
+
# Instantiate notification object
|
9
|
+
notification = Tolliver.notification_model.find_by_id(notification_id)
|
10
|
+
return nil if notification.nil?
|
11
|
+
|
12
|
+
# Call standard API
|
13
|
+
Tolliver::Services::DeliveryService.instance.deliver(notification)
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -18,7 +18,7 @@ module Tolliver
|
|
18
18
|
|
19
19
|
# Sender
|
20
20
|
@sender_email = Tolliver.email_sender
|
21
|
-
raise Tolliver::Errors::StandardError.new("Please specify sender.") if @sender_email.nil?
|
21
|
+
raise Tolliver::Errors::StandardError.new("Please specify e-mail sender.") if @sender_email.nil?
|
22
22
|
unless Tolliver.email_sender_name.blank?
|
23
23
|
@sender_email = "#{Tolliver.email_sender_name} <#{@sender_email}>"
|
24
24
|
end
|
@@ -29,11 +29,11 @@ module Tolliver
|
|
29
29
|
|
30
30
|
# Attachments
|
31
31
|
notification.notification_attachments.each do |notification_attachment|
|
32
|
-
attachments[notification_attachment.name] = notification_attachment.
|
32
|
+
attachments[notification_attachment.name] = notification_attachment.read if notification_attachment.read
|
33
33
|
end
|
34
34
|
|
35
35
|
# Mail
|
36
|
-
mail(from: @sender_email, to: @notification_receiver.receiver_contact, subject: @notification.subject)
|
36
|
+
mail(from: @sender_email, to: @notification_receiver.receiver_contact.to_s, subject: @notification.subject)
|
37
37
|
end
|
38
38
|
|
39
39
|
end
|
@@ -52,15 +52,15 @@ module Tolliver
|
|
52
52
|
# ***********************************************************************
|
53
53
|
|
54
54
|
def deliver
|
55
|
-
Tolliver::Services::
|
55
|
+
Tolliver::Services::DeliveryService.instance.deliver(self)
|
56
56
|
end
|
57
57
|
|
58
58
|
def enqueue_for_delivery
|
59
|
-
Tolliver::Services::
|
59
|
+
Tolliver::Services::DeliveryService.instance.enqueue_for_delivery(self)
|
60
60
|
end
|
61
61
|
|
62
62
|
def reset_delivery
|
63
|
-
Tolliver::Services::
|
63
|
+
Tolliver::Services::DeliveryService.instance.reset_delivery(self)
|
64
64
|
end
|
65
65
|
|
66
66
|
end
|
@@ -30,6 +30,18 @@ module Tolliver
|
|
30
30
|
|
31
31
|
end
|
32
32
|
|
33
|
+
def read
|
34
|
+
if @data.nil?
|
35
|
+
unless self.attachment.blank?
|
36
|
+
@data = Base64.strict_decode64(self.attachment) rescue nil
|
37
|
+
end
|
38
|
+
unless self.url.blank?
|
39
|
+
@data = open(self.url) { |f| f.read }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
@data
|
43
|
+
end
|
44
|
+
|
33
45
|
end
|
34
46
|
end
|
35
47
|
end
|
@@ -13,23 +13,12 @@ require 'singleton'
|
|
13
13
|
|
14
14
|
module Tolliver
|
15
15
|
module Services
|
16
|
-
class
|
16
|
+
class DeliveryService
|
17
17
|
include Singleton
|
18
18
|
|
19
19
|
def enqueue_for_delivery(notification)
|
20
20
|
return nil if notification.nil?
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
# Entry point for QC
|
25
|
-
def self.deliver(notification_id)
|
26
|
-
|
27
|
-
# Instantiate notification object
|
28
|
-
notification = Tolliver.notification_model.find_by_id(notification_id)
|
29
|
-
return nil if notification.nil?
|
30
|
-
|
31
|
-
# Call standard API
|
32
|
-
self.instance.deliver(notification)
|
21
|
+
Tolliver::Jobs::DeliveryJob.perform_later(notification.id)
|
33
22
|
end
|
34
23
|
|
35
24
|
# Deliver notification to receivers by all configured methods
|
@@ -15,11 +15,14 @@ module Tolliver
|
|
15
15
|
class Email
|
16
16
|
|
17
17
|
def deliver(notification_receiver)
|
18
|
+
return false if provider.nil?
|
19
|
+
|
20
|
+
# Prepare notification
|
18
21
|
notification = notification_receiver.notification_delivery.notification
|
19
22
|
|
20
23
|
# Send email
|
21
24
|
begin
|
22
|
-
|
25
|
+
provider.deliver(notification, notification_receiver)
|
23
26
|
notification_receiver.status = 'sent'
|
24
27
|
#rescue Net::SMTPFatalError, Net::SMTPSyntaxError
|
25
28
|
rescue StandardError => e
|
@@ -36,6 +39,16 @@ module Tolliver
|
|
36
39
|
true
|
37
40
|
end
|
38
41
|
|
42
|
+
protected
|
43
|
+
|
44
|
+
def provider
|
45
|
+
if @provider.nil? && Tolliver.email_provider
|
46
|
+
provider_class_name = "Tolliver::Services::Methods::Email::#{Tolliver.email_provider.to_s.camelize}"
|
47
|
+
@provider = provider_class_name.constantize.new(Tolliver.email_provider_params)
|
48
|
+
end
|
49
|
+
@provider
|
50
|
+
end
|
51
|
+
|
39
52
|
end
|
40
53
|
end
|
41
54
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# *****************************************************************************
|
2
|
+
# * Copyright (c) 2019 Matěj Outlý
|
3
|
+
# *****************************************************************************
|
4
|
+
# *
|
5
|
+
# * Mailgun e-mail provider
|
6
|
+
# *
|
7
|
+
# * Author: Matěj Outlý
|
8
|
+
# * Date : 1. 12. 2017
|
9
|
+
# *
|
10
|
+
# *****************************************************************************
|
11
|
+
|
12
|
+
module Tolliver
|
13
|
+
module Services
|
14
|
+
module Methods
|
15
|
+
class Email
|
16
|
+
class Mailgun
|
17
|
+
|
18
|
+
def initialize(params = {})
|
19
|
+
require 'mailgun-ruby'
|
20
|
+
if params[:api_key].blank? || params[:domain].blank?
|
21
|
+
raise Tolliver::Errors::StandardError.new('Please provide API key and domain in e-mail provider params.')
|
22
|
+
end
|
23
|
+
@client = ::Mailgun::Client.new(params[:api_key])
|
24
|
+
@domain = params[:domain]
|
25
|
+
end
|
26
|
+
|
27
|
+
def deliver(notification, notification_receiver)
|
28
|
+
|
29
|
+
# Sender
|
30
|
+
raise Tolliver::Errors::StandardError.new("Please specify e-mail sender.") if Tolliver.email_sender.nil?
|
31
|
+
|
32
|
+
# Message builder
|
33
|
+
message = ::Mailgun::MessageBuilder.new
|
34
|
+
message.from(Tolliver.email_sender, {'full_name' => Tolliver.email_sender_name})
|
35
|
+
message.add_recipient(:to, notification_receiver.receiver_contact.to_s)
|
36
|
+
message.reply_to(notification_receiver.notification_delivery.sender_contact.to_s) unless notification_receiver.notification_delivery.sender_contact.blank?
|
37
|
+
message.subject(notification.subject)
|
38
|
+
message.body_text(ActionController::Base.helpers.strip_tags(notification.message.to_s))
|
39
|
+
message.body_html(notification.message)
|
40
|
+
notification.notification_attachments.each do |notification_attachment|
|
41
|
+
message.add_attachment(StringIO.new(notification_attachment.read), notification_attachment.name) if notification_attachment.read
|
42
|
+
end
|
43
|
+
response = @client.send_message(@domain, message)
|
44
|
+
if response.code != 200
|
45
|
+
raise Tolliver::Errors::StandardError.new(response.body)
|
46
|
+
end
|
47
|
+
|
48
|
+
true
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# *****************************************************************************
|
2
|
+
# * Copyright (c) 2019 Matěj Outlý
|
3
|
+
# *****************************************************************************
|
4
|
+
# *
|
5
|
+
# * SMTP e-mail provider
|
6
|
+
# *
|
7
|
+
# * Author: Matěj Outlý
|
8
|
+
# * Date : 1. 12. 2017
|
9
|
+
# *
|
10
|
+
# *****************************************************************************
|
11
|
+
|
12
|
+
module Tolliver
|
13
|
+
module Services
|
14
|
+
module Methods
|
15
|
+
class Email
|
16
|
+
class Smtp
|
17
|
+
|
18
|
+
def initialize(params = {}) end
|
19
|
+
|
20
|
+
def deliver(notification, notification_receiver)
|
21
|
+
Tolliver::NotificationMailer.notify(notification, notification_receiver).deliver_now
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -15,11 +15,14 @@ module Tolliver
|
|
15
15
|
class Sms
|
16
16
|
|
17
17
|
def deliver(notification_receiver)
|
18
|
+
return false if provider.nil?
|
19
|
+
|
20
|
+
# Prepare notification
|
18
21
|
notification = notification_receiver.notification_delivery.notification
|
19
22
|
|
20
23
|
# Send SMS
|
21
24
|
begin
|
22
|
-
provider.deliver(
|
25
|
+
provider.deliver(notification, notification_receiver)
|
23
26
|
notification_receiver.status = 'sent'
|
24
27
|
rescue StandardError => e
|
25
28
|
notification_receiver.status = 'error'
|
@@ -38,7 +41,7 @@ module Tolliver
|
|
38
41
|
protected
|
39
42
|
|
40
43
|
def provider
|
41
|
-
if @provider.nil?
|
44
|
+
if @provider.nil? && Tolliver.sms_provider
|
42
45
|
provider_class_name = "Tolliver::Services::Methods::Sms::#{Tolliver.sms_provider.to_s.camelize}"
|
43
46
|
@provider = provider_class_name.constantize.new(Tolliver.sms_provider_params)
|
44
47
|
end
|
@@ -9,24 +9,23 @@
|
|
9
9
|
# *
|
10
10
|
# *****************************************************************************
|
11
11
|
|
12
|
-
require 'plivo'
|
13
|
-
|
14
12
|
module Tolliver
|
15
13
|
module Services
|
16
14
|
module Methods
|
17
|
-
|
15
|
+
class Sms
|
18
16
|
class Plivo
|
19
17
|
|
20
18
|
def initialize(params = {})
|
19
|
+
require 'plivo'
|
21
20
|
if params[:auth_id].blank? || params[:auth_token].blank?
|
22
|
-
raise Tolliver::Errors::StandardError.new('Please provide Auth ID and Auth Token in provider params.')
|
21
|
+
raise Tolliver::Errors::StandardError.new('Please provide Auth ID and Auth Token in SMS provider params.')
|
23
22
|
end
|
24
23
|
@auth_id = params[:auth_id]
|
25
24
|
@auth_token = params[:auth_token]
|
26
25
|
@api = ::Plivo::RestAPI.new(@auth_id, @auth_token)
|
27
26
|
end
|
28
27
|
|
29
|
-
def deliver(
|
28
|
+
def deliver(notification, notification_receiver)
|
30
29
|
|
31
30
|
# Check message length.
|
32
31
|
if message.bytesize > 200
|
@@ -36,8 +35,8 @@ module Tolliver
|
|
36
35
|
# Request API
|
37
36
|
response = @api.send_message({
|
38
37
|
'src' => Tolliver.sms_sender, # TODO: This should be improved to take sender from number pool and remember number / message mapping
|
39
|
-
'dst' =>
|
40
|
-
'text' => message.to_s,
|
38
|
+
'dst' => notification_receiver.receiver_contact.to_s,
|
39
|
+
'text' => ActionController::Base.helpers.strip_tags(notification.message.to_s),
|
41
40
|
'method' => 'POST'
|
42
41
|
})
|
43
42
|
|
@@ -13,7 +13,7 @@ require 'singleton'
|
|
13
13
|
|
14
14
|
module Tolliver
|
15
15
|
module Services
|
16
|
-
class
|
16
|
+
class NotificationService
|
17
17
|
include Singleton
|
18
18
|
|
19
19
|
def notify(options)
|
@@ -35,8 +35,8 @@ module Tolliver
|
|
35
35
|
|
36
36
|
# Interpret params and store it in DB
|
37
37
|
params = map_params(options[:params] || {})
|
38
|
-
notification.message = interpret_named_params(notification_template.message, params)
|
39
|
-
notification.subject = interpret_named_params(notification_template.subject, params)
|
38
|
+
notification.message = eval_expressions(interpret_named_params(notification_template.message, params))
|
39
|
+
notification.subject = eval_expressions(interpret_named_params(notification_template.subject, params))
|
40
40
|
|
41
41
|
# Notification template
|
42
42
|
notification.notification_template = notification_template
|
@@ -55,8 +55,10 @@ module Tolliver
|
|
55
55
|
attachments = [attachments] unless attachments.is_a?(Array)
|
56
56
|
attachments.each do |attachment|
|
57
57
|
raise Tolliver::Errors::BadRequest.new('Missing attachment name.') if attachment[:name].blank?
|
58
|
-
|
59
|
-
|
58
|
+
if attachment[:attachment].blank? && attachment[:url].blank?
|
59
|
+
raise Tolliver::Errors::BadRequest.new('Missing attachment data or URL.')
|
60
|
+
end
|
61
|
+
notification.notification_attachments.create(name: attachment[:name], attachment: attachment[:attachment], url: attachment[:url])
|
60
62
|
end
|
61
63
|
end
|
62
64
|
|
@@ -98,8 +100,8 @@ module Tolliver
|
|
98
100
|
|
99
101
|
# Save to DB
|
100
102
|
filtered_receivers.each do |receiver|
|
101
|
-
raise Tolliver::Errors::BadRequest.new('Missing
|
102
|
-
raise Tolliver::Errors::BadRequest.new('Missing
|
103
|
+
raise Tolliver::Errors::BadRequest.new('Missing receiver ref.') if receiver[:ref].blank?
|
104
|
+
raise Tolliver::Errors::BadRequest.new('Missing receiver contact.') if receiver[:contact].blank?
|
103
105
|
notification_delivery.notification_receivers.create(receiver_ref: receiver[:ref], receiver_contact: receiver[:contact])
|
104
106
|
end
|
105
107
|
notification_delivery.sent_count = 0
|
@@ -147,7 +149,19 @@ module Tolliver
|
|
147
149
|
end
|
148
150
|
end
|
149
151
|
|
150
|
-
def
|
152
|
+
def eval_expressions(text)
|
153
|
+
text.gsub(/%{[^{}]+}/) do |match|
|
154
|
+
template_to_eval = match[2..-2].to_s.strip
|
155
|
+
begin
|
156
|
+
evaluated_match = eval(template_to_eval) # evaluate match
|
157
|
+
rescue
|
158
|
+
evaluated_match = ""
|
159
|
+
end
|
160
|
+
evaluated_match
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def interpret_positional_params_and_eval_expressions(text, params)
|
151
165
|
text.gsub(/%{[^{}]+}/) do |match|
|
152
166
|
template_to_eval = match[2..-2].gsub(/%([0-9]+)/, "params[\\1]") # substitute all %1, %2, %3, ... to a form which can be evaluated
|
153
167
|
begin
|
@@ -19,26 +19,15 @@ module Tolliver
|
|
19
19
|
# Input validation
|
20
20
|
return nil if notification_delivery.nil? || notification_delivery.policy != :batch
|
21
21
|
|
22
|
-
# Enqueue
|
23
|
-
|
22
|
+
# Enqueue
|
23
|
+
Tolliver::Jobs::BatchPolicyJob.perform_later(notification_delivery.id, batch_size)
|
24
24
|
end
|
25
25
|
|
26
|
-
|
27
|
-
def self.deliver(notification_delivery_id, batch_size = 10)
|
26
|
+
def deliver_batch_and_enqueue(notification_delivery, batch_size = 10)
|
28
27
|
|
29
|
-
#
|
30
|
-
notification_delivery = Tolliver.notification_delivery_model.find_by_id(notification_delivery_id)
|
28
|
+
# Input validation
|
31
29
|
return nil if notification_delivery.nil? || notification_delivery.policy != :batch
|
32
30
|
|
33
|
-
# Call internal logic
|
34
|
-
policy_service = self.new
|
35
|
-
policy_service.send(:deliver_batch_and_enqueue, notification_delivery, batch_size)
|
36
|
-
end
|
37
|
-
|
38
|
-
protected
|
39
|
-
|
40
|
-
def deliver_batch_and_enqueue(notification_delivery, batch_size = 10)
|
41
|
-
|
42
31
|
# Send single batch
|
43
32
|
remaining = deliver_batch(notification_delivery, batch_size)
|
44
33
|
|
@@ -59,6 +48,9 @@ module Tolliver
|
|
59
48
|
|
60
49
|
def deliver_batch(notification_delivery, batch_size = 10)
|
61
50
|
|
51
|
+
# Input validation
|
52
|
+
return nil if notification_delivery.nil? || notification_delivery.policy != :batch
|
53
|
+
|
62
54
|
# Nothing to do
|
63
55
|
return 0 if notification_delivery.sent_count == notification_delivery.receivers_count
|
64
56
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tolliver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matěj Outlý
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -50,22 +50,27 @@ files:
|
|
50
50
|
- db/migrate/20160509144238_create_notification_templates.rb
|
51
51
|
- db/migrate/20170630190600_create_notification_deliveries.rb
|
52
52
|
- db/migrate/20201027150000_create_notification_attachments.rb
|
53
|
+
- db/migrate/20210415120000_add_notification_attachments_url.rb
|
53
54
|
- lib/tolliver.rb
|
54
55
|
- lib/tolliver/engine.rb
|
55
56
|
- lib/tolliver/errors/bad_request.rb
|
56
57
|
- lib/tolliver/errors/not_found.rb
|
57
58
|
- lib/tolliver/errors/standard_error.rb
|
59
|
+
- lib/tolliver/jobs/batch_policy_job.rb
|
60
|
+
- lib/tolliver/jobs/delivery_job.rb
|
58
61
|
- lib/tolliver/mailers/notification_mailer.rb
|
59
62
|
- lib/tolliver/models/notification.rb
|
60
63
|
- lib/tolliver/models/notification_attachment.rb
|
61
64
|
- lib/tolliver/models/notification_delivery.rb
|
62
65
|
- lib/tolliver/models/notification_receiver.rb
|
63
66
|
- lib/tolliver/models/notification_template.rb
|
64
|
-
- lib/tolliver/services/
|
67
|
+
- lib/tolliver/services/delivery_service.rb
|
65
68
|
- lib/tolliver/services/methods/email.rb
|
69
|
+
- lib/tolliver/services/methods/email/mailgun.rb
|
70
|
+
- lib/tolliver/services/methods/email/smtp.rb
|
66
71
|
- lib/tolliver/services/methods/sms.rb
|
67
72
|
- lib/tolliver/services/methods/sms/plivo.rb
|
68
|
-
- lib/tolliver/services/
|
73
|
+
- lib/tolliver/services/notification_service.rb
|
69
74
|
- lib/tolliver/services/policies/batch.rb
|
70
75
|
- lib/tolliver/services/policies/instantly.rb
|
71
76
|
- lib/tolliver/utils/enum.rb
|