tolliver 2.1.3 → 2.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45746e4382ed1970ae1a7dc297f096d0616c824d4bf319949931372845aeb74d
4
- data.tar.gz: 2445b15788d28a73cbde7d0e2f3e28cf090a9c886e9c56d4c4acb2fa9a7edc42
3
+ metadata.gz: 1c0fa6672c057ce36fec360bddd1f1a7360dcdecb6198678838a6150978a8b12
4
+ data.tar.gz: 13ddc4dba78f936159bc825f4cee588d66941a9ff296d5fe0abd70b6ae02cbf0
5
5
  SHA512:
6
- metadata.gz: 6693df6712589159a779c7eca498047d5a59f6db0992716622a821c0fc6d726739d32df94be7598a2b5705e3b45091e4f48744b06cdc2f4ff607b3359d3d8a7d
7
- data.tar.gz: 0036c0fa396f8ace9fef5502b3a5984b47246fc6305dca95d9a45bcd10388e0057c85cef4d07adda19bc5fea738fa7b73b285e60fbc6f23d3ffeac086fe914e6
6
+ metadata.gz: 4e2ddef5141f52daf5bdfb4d648c2420dad063f2ba1fd06d2753eca7f8e56e66184bfa72a60124ab7203e0821119796a398508df86a555d9fa2d7f77ac289736
7
+ data.tar.gz: 23a5b86b0b8daac5a8f334e3d2bf53fb42b380fda433550c4742216d7631cd408920436467d85224d07c872b8fb3cb80f7db62d2d1403338e99456d6f4d73372
data/README.md CHANGED
@@ -7,7 +7,7 @@ send it in a batch with different delivery methods:
7
7
  - SMS with Plivo provider
8
8
  - Whatever custom delivery method
9
9
 
10
- ## Installation
10
+ ## 1. Installation
11
11
 
12
12
  Add gem to your Gemfile:
13
13
 
@@ -20,7 +20,7 @@ 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
23
+ ### 1.1. Plivo support
24
24
 
25
25
  Add gem to your Gemfile:
26
26
 
@@ -28,7 +28,7 @@ Add gem to your Gemfile:
28
28
  gem 'plivo'
29
29
  ```
30
30
 
31
- ### Mailgun support
31
+ ### 1.2. Mailgun support
32
32
 
33
33
  Add gem to your Gemfile:
34
34
 
@@ -36,7 +36,7 @@ Add gem to your Gemfile:
36
36
  gem 'mailgun-ruby'
37
37
  ```
38
38
 
39
- ## Configuration
39
+ ## 2. Configuration
40
40
 
41
41
  You can configure module through `config/initializers/tolliver.rb` file:
42
42
 
@@ -65,7 +65,7 @@ Available options:
65
65
  - sms_provider_params
66
66
  - delivery_methods
67
67
 
68
- ### Plivo support
68
+ ### 2.1. Plivo support
69
69
 
70
70
  Set Plivo as SMS provider and add Plivo auth ID and token into Tolliver configuration file:
71
71
 
@@ -80,7 +80,7 @@ Tolliver.setup do |config|
80
80
  end
81
81
  ```
82
82
 
83
- ### Mailgun support
83
+ ### 2.2. Mailgun support
84
84
 
85
85
  Set Mailgun as e-mail provider and add Mailgun API key and domain into Tolliver configuration file:
86
86
 
@@ -95,7 +95,7 @@ Tolliver.setup do |config|
95
95
  end
96
96
  ```
97
97
 
98
- ## Usage
98
+ ## 3. Usage
99
99
 
100
100
  To enter new notification into the system, just call `notify` method:
101
101
 
@@ -107,8 +107,8 @@ Tolliver.notify(
107
107
  {key: :key_2, value: :value_2}
108
108
  ],
109
109
  receivers: [
110
- {ref: :receiver_1, contact: "receiver_1@domain.tld"},
111
- {ref: :receiver_2, contact: "receiver_2@domain.tld"},
110
+ {ref: :receiver_1, email: "receiver_1@domain.tld"},
111
+ {ref: :receiver_2, email: "receiver_2@domain.tld"},
112
112
  ]
113
113
  )
114
114
  ```
@@ -0,0 +1,12 @@
1
+ class AddShortMessageAndPhone < ActiveRecord::Migration[6.0]
2
+ def change
3
+ add_column :notification_templates, :short_message, :string
4
+ add_column :notifications, :short_message, :string
5
+
6
+ rename_column :notification_deliveries, :sender_contact, :sender_email
7
+ add_column :notification_deliveries, :sender_phone, :string
8
+
9
+ rename_column :notification_receivers, :receiver_contact, :receiver_email
10
+ add_column :notification_receivers, :receiver_phone, :string
11
+ end
12
+ end
@@ -33,7 +33,7 @@ module Tolliver
33
33
  end
34
34
 
35
35
  # Mail
36
- mail(from: @sender_email, to: @notification_receiver.receiver_contact.to_s, subject: @notification.subject)
36
+ mail(from: @sender_email, to: @notification_receiver.receiver_email.to_s, subject: @notification.subject)
37
37
  end
38
38
 
39
39
  end
@@ -26,7 +26,7 @@ module Tolliver
26
26
  # Validators
27
27
  # *********************************************************************
28
28
 
29
- validates_presence_of :notification_delivery_id, :receiver_ref, :receiver_contact
29
+ validates_presence_of :notification_delivery_id, :receiver_ref
30
30
 
31
31
  # *********************************************************************
32
32
  # State
@@ -14,6 +14,23 @@ module Tolliver
14
14
  module Methods
15
15
  class Email
16
16
 
17
+ def is_notification_valid?(notification)
18
+ return false if notification.subject.blank?
19
+ return false if notification.message.blank?
20
+ true
21
+ end
22
+
23
+ def is_notification_delivery_valid?(notification_delivery)
24
+ return false if !notification_delivery.sender_email.blank? && !(notification_delivery.sender_email =~ URI::MailTo::EMAIL_REGEXP)
25
+ true
26
+ end
27
+
28
+ def is_notification_receiver_valid?(notification_receiver)
29
+ return false if notification_receiver.receiver_email.blank?
30
+ return false unless notification_receiver.receiver_email =~ URI::MailTo::EMAIL_REGEXP
31
+ true
32
+ end
33
+
17
34
  def deliver(notification_receiver)
18
35
  return false if provider.nil?
19
36
 
@@ -29,17 +29,19 @@ module Tolliver
29
29
  # Sender
30
30
  raise Tolliver::Errors::StandardError.new("Please specify e-mail sender.") if Tolliver.email_sender.nil?
31
31
 
32
- # Message builder
32
+ # Build message
33
33
  message = ::Mailgun::MessageBuilder.new
34
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?
35
+ message.add_recipient(:to, notification_receiver.receiver_email.to_s)
36
+ message.reply_to(notification_receiver.notification_delivery.sender_email.to_s) unless notification_receiver.notification_delivery.sender_email.blank?
37
37
  message.subject(notification.subject)
38
38
  message.body_text(ActionController::Base.helpers.strip_tags(notification.message.to_s))
39
39
  message.body_html(notification.message)
40
40
  notification.notification_attachments.each do |notification_attachment|
41
41
  message.add_attachment(StringIO.new(notification_attachment.read), notification_attachment.name) if notification_attachment.read
42
42
  end
43
+
44
+ # Request API
43
45
  response = @client.send_message(@domain, message)
44
46
  if response.code != 200
45
47
  raise Tolliver::Errors::StandardError.new(response.body)
@@ -14,6 +14,20 @@ module Tolliver
14
14
  module Methods
15
15
  class Sms
16
16
 
17
+ def is_notification_valid?(notification)
18
+ return false if notification.short_message.blank?
19
+ true
20
+ end
21
+
22
+ def is_notification_delivery_valid?(_)
23
+ true
24
+ end
25
+
26
+ def is_notification_receiver_valid?(notification_receiver)
27
+ return false if notification_receiver.receiver_phone.blank?
28
+ true
29
+ end
30
+
17
31
  def deliver(notification_receiver)
18
32
  return false if provider.nil?
19
33
 
@@ -22,23 +22,18 @@ module Tolliver
22
22
  end
23
23
  @auth_id = params[:auth_id]
24
24
  @auth_token = params[:auth_token]
25
- @api = ::Plivo::RestAPI.new(@auth_id, @auth_token)
25
+ @client = ::Plivo::RestClient.new(@auth_id, @auth_token)
26
26
  end
27
27
 
28
28
  def deliver(notification, notification_receiver)
29
29
 
30
- # Check message length.
31
- if message.bytesize > 200
32
- raise 'Message too long.'
33
- end
30
+ # Sender
31
+ raise Tolliver::Errors::StandardError.new("Please specify SMS sender.") if Tolliver.sms_sender.nil?
34
32
 
35
33
  # Request API
36
- response = @api.send_message({
37
- 'src' => Tolliver.sms_sender, # TODO: This should be improved to take sender from number pool and remember number / message mapping
38
- 'dst' => notification_receiver.receiver_contact.to_s,
39
- 'text' => ActionController::Base.helpers.strip_tags(notification.message.to_s),
40
- 'method' => 'POST'
41
- })
34
+ @client.message.create(Tolliver.sms_sender, # TODO: This should be improved to take sender from number pool and remember number / message mapping
35
+ [notification_receiver.receiver_phone.to_s],
36
+ ActionController::Base.helpers.strip_tags(notification.short_message.to_s))
42
37
 
43
38
  true
44
39
  end
@@ -29,17 +29,23 @@ module Tolliver
29
29
  raise Tolliver::Errors::BadRequest.new('Missing template.') if template.blank?
30
30
  notification_template = Tolliver.notification_template_model.where(ref: template).first
31
31
  notification_template = Tolliver.notification_template_model.where(id: template).first if notification_template.nil?
32
+ if notification_template.nil? && template.to_s == 'common'
33
+ notification_template = Tolliver.notification_template_model.new(subject: '%{subject}',
34
+ message: '%{message}', short_message: '%{message}',
35
+ is_disabled: false, is_dry: false)
36
+ end
32
37
  raise Tolliver::Errors::NotFound.new("Template #{template.to_s} not found.") if notification_template.nil?
33
38
 
34
- if !notification_template.is_disabled && !notification_template.message.blank?
39
+ if !notification_template.is_disabled
35
40
 
36
41
  # Interpret params and store it in DB
37
42
  params = map_params(options[: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))
43
+ notification.subject = eval_expressions(interpret_named_params(notification_template.subject.to_s, params))
44
+ notification.message = eval_expressions(interpret_named_params(notification_template.message.to_s, params))
45
+ notification.short_message = eval_expressions(interpret_named_params(notification_template.short_message.to_s, params))
40
46
 
41
47
  # Notification template
42
- notification.notification_template = notification_template
48
+ notification.notification_template = notification_template unless notification_template.new_record?
43
49
 
44
50
  # Signature
45
51
  unless options[:signature].blank?
@@ -77,7 +83,10 @@ module Tolliver
77
83
  methods.each do |method|
78
84
 
79
85
  # Delivery object
80
- notification_delivery = notification.notification_deliveries.create(method: method)
86
+ notification_delivery = notification.notification_deliveries.build(method: method)
87
+
88
+ # Validate notification suitability for the selected method
89
+ next unless notification_delivery.method_service.is_notification_valid?(notification)
81
90
 
82
91
  # Is postponed
83
92
  if options[:is_postponed]
@@ -86,26 +95,38 @@ module Tolliver
86
95
 
87
96
  # Sender
88
97
  unless options[:sender].blank?
89
- raise Tolliver::Errors::BadRequest.new('Missing sender ref.') if options[:sender][:ref].blank?
90
- raise Tolliver::Errors::BadRequest.new('Missing sender contact.') if options[:sender][:contact].blank?
91
98
  notification_delivery.sender_ref = options[:sender][:ref]
92
- notification_delivery.sender_contact = options[:sender][:contact]
99
+ notification_delivery.sender_email = options[:sender][:email]
100
+ notification_delivery.sender_phone = options[:sender][:phone]
101
+ raise Tolliver::Errors::BadRequest.new('Missing sender ref.') if notification_delivery.sender_ref.blank?
102
+ unless notification_delivery.method_service.is_notification_delivery_valid?(notification_delivery) # throw exception if sender not valid
103
+ raise Tolliver::Errors::BadRequest.new('Sender contact not valid.')
104
+ end
93
105
  end
94
106
 
107
+ # Must be persisted in DB before receiver created
108
+ notification_delivery.save
109
+
95
110
  # Receivers
96
111
  receivers = options[:receivers] || []
97
112
  raise Tolliver::Errors::BadRequest.new('Missing receivers.') if receivers.blank? || receivers.empty?
98
113
  receivers = [receivers] unless receivers.is_a?(Array)
99
- filtered_receivers = receivers.dup # TODO contact validation based on method and filter
100
114
 
101
- # Save to DB
102
- filtered_receivers.each do |receiver|
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?
105
- notification_delivery.notification_receivers.create(receiver_ref: receiver[:ref], receiver_contact: receiver[:contact])
115
+ # Save to DB if valid for the selected method
116
+ receivers_count = 0
117
+ receivers.each do |receiver|
118
+ notification_receiver = notification_delivery.notification_receivers.build
119
+ notification_receiver.receiver_ref = receiver[:ref]
120
+ notification_receiver.receiver_email = receiver[:email]
121
+ notification_receiver.receiver_phone = receiver[:phone]
122
+ raise Tolliver::Errors::BadRequest.new('Missing receiver ref.') if notification_receiver.receiver_ref.blank?
123
+ if notification_delivery.method_service.is_notification_receiver_valid?(notification_receiver) # ignore receiver if not valid
124
+ notification_receiver.save
125
+ receivers_count += 1
126
+ end
106
127
  end
107
128
  notification_delivery.sent_count = 0
108
- notification_delivery.receivers_count = filtered_receivers.size
129
+ notification_delivery.receivers_count = receivers_count
109
130
  notification_delivery.save
110
131
 
111
132
  end
@@ -113,7 +134,7 @@ module Tolliver
113
134
  end
114
135
 
115
136
  else
116
- notification = nil # Do not create notification with empty message
137
+ notification = nil # Do not create disabled notification
117
138
  end
118
139
 
119
140
  end
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.1.3
4
+ version: 2.2.0
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: 2021-04-14 00:00:00.000000000 Z
11
+ date: 2021-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -51,6 +51,7 @@ files:
51
51
  - db/migrate/20170630190600_create_notification_deliveries.rb
52
52
  - db/migrate/20201027150000_create_notification_attachments.rb
53
53
  - db/migrate/20210415120000_add_notification_attachments_url.rb
54
+ - db/migrate/20210528120000_add_short_message_and_phone.rb
54
55
  - lib/tolliver.rb
55
56
  - lib/tolliver/engine.rb
56
57
  - lib/tolliver/errors/bad_request.rb