tolliver 1.0.0 → 2.1.2

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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +71 -13
  3. data/app/models/tolliver/notification.rb +0 -1
  4. data/app/models/tolliver/notification_attachment.rb +16 -0
  5. data/app/models/tolliver/notification_delivery.rb +1 -3
  6. data/app/models/tolliver/notification_receiver.rb +0 -2
  7. data/app/models/tolliver/notification_template.rb +0 -1
  8. data/app/views/tolliver/notification_mailer/notify.html.erb +1 -0
  9. data/app/views/tolliver/notification_mailer/notify.text.erb +1 -0
  10. data/config/locales/en.yml +1 -1
  11. data/db/migrate/20160121093817_create_notifications.rb +1 -12
  12. data/db/migrate/20160121094838_create_notification_receivers.rb +6 -4
  13. data/db/migrate/20160509144238_create_notification_templates.rb +4 -4
  14. data/db/migrate/20170630190600_create_notification_deliveries.rb +10 -3
  15. data/db/migrate/20201027150000_create_notification_attachments.rb +17 -0
  16. data/db/migrate/20210415120000_add_notification_attachments_url.rb +6 -0
  17. data/lib/tolliver.rb +66 -54
  18. data/lib/tolliver/errors/bad_request.rb +17 -0
  19. data/lib/tolliver/errors/not_found.rb +17 -0
  20. data/lib/tolliver/errors/standard_error.rb +17 -0
  21. data/lib/tolliver/jobs/batch_policy_job.rb +19 -0
  22. data/lib/tolliver/jobs/delivery_job.rb +18 -0
  23. data/lib/tolliver/mailers/notification_mailer.rb +10 -13
  24. data/lib/tolliver/models/notification.rb +11 -64
  25. data/lib/tolliver/models/notification_attachment.rb +47 -0
  26. data/lib/tolliver/models/notification_delivery.rb +32 -40
  27. data/lib/tolliver/models/notification_receiver.rb +3 -25
  28. data/lib/tolliver/models/notification_template.rb +6 -10
  29. data/lib/tolliver/services/delivery_service.rb +63 -0
  30. data/lib/tolliver/services/methods/email.rb +55 -0
  31. data/lib/tolliver/services/methods/email/mailgun.rb +55 -0
  32. data/lib/tolliver/services/{delivery.rb → methods/email/smtp.rb} +11 -13
  33. data/lib/tolliver/services/methods/sms.rb +54 -0
  34. data/lib/tolliver/services/methods/sms/plivo.rb +50 -0
  35. data/lib/tolliver/services/notification_service.rb +179 -0
  36. data/lib/tolliver/services/policies/batch.rb +82 -0
  37. data/lib/tolliver/services/policies/instantly.rb +50 -0
  38. metadata +27 -16
  39. data/app/views/notification_mailer/notify.html.erb +0 -1
  40. data/app/views/notification_mailer/notify.text.erb +0 -1
  41. data/lib/tolliver/models/notification_delivery/batch.rb +0 -77
  42. data/lib/tolliver/models/notification_delivery/instantly.rb +0 -55
  43. data/lib/tolliver/models/notification_receiver/email.rb +0 -46
  44. data/lib/tolliver/models/notification_receiver/sms.rb +0 -45
  45. data/lib/tolliver/services/notification.rb +0 -234
  46. data/lib/tolliver/services/sms/plivo.rb +0 -49
@@ -0,0 +1,82 @@
1
+ # *****************************************************************************
2
+ # * Copyright (c) 2019 Matěj Outlý
3
+ # *****************************************************************************
4
+ # *
5
+ # * Notification receiver
6
+ # *
7
+ # * Author: Matěj Outlý
8
+ # * Date : 21. 1. 2016
9
+ # *
10
+ # *****************************************************************************
11
+
12
+ module Tolliver
13
+ module Services
14
+ module Policies
15
+ class Batch
16
+
17
+ def deliver(notification_delivery, batch_size = 10)
18
+
19
+ # Input validation
20
+ return nil if notification_delivery.nil? || notification_delivery.policy != :batch
21
+
22
+ # Enqueue
23
+ Tolliver::Jobs::BatchPolicyJob.perform_later(notification_delivery.id, batch_size)
24
+ end
25
+
26
+ def deliver_batch_and_enqueue(notification_delivery, batch_size = 10)
27
+
28
+ # Input validation
29
+ return nil if notification_delivery.nil? || notification_delivery.policy != :batch
30
+
31
+ # Send single batch
32
+ remaining = deliver_batch(notification_delivery, batch_size)
33
+
34
+ # If still some receivers remaining, enqueue next batch
35
+ if remaining > 0
36
+
37
+ # Sleep for a while to prevent SMTP/SMS service overflow
38
+ sleep 5 # seconds
39
+
40
+ # Queue next batch
41
+ deliver(notification_delivery, batch_size)
42
+ false
43
+ else
44
+ true
45
+ end
46
+
47
+ end
48
+
49
+ def deliver_batch(notification_delivery, batch_size = 10)
50
+
51
+ # Input validation
52
+ return nil if notification_delivery.nil? || notification_delivery.policy != :batch
53
+
54
+ # Nothing to do
55
+ return 0 if notification_delivery.sent_count == notification_delivery.receivers_count
56
+
57
+ # Get batch of receivers prepared for send
58
+ notification_receivers = notification_delivery.notification_receivers.where(sent_at: nil).limit(batch_size)
59
+
60
+ # Send entire batch
61
+ sent_counter = 0
62
+ notification_receivers.each do |notification_receiver|
63
+ sent_counter += 1 if notification_delivery.method_service.deliver(notification_receiver)
64
+ end
65
+
66
+ # Is postponed
67
+ notification_delivery.is_postponed = false
68
+
69
+ # Update statistics
70
+ notification_delivery.sent_count += sent_counter
71
+ notification_delivery.sent_at = Time.current if notification_delivery.sent_count == notification_delivery.receivers_count
72
+
73
+ # Save
74
+ notification_delivery.save
75
+
76
+ (notification_delivery.receivers_count - notification_delivery.sent_count)
77
+ end
78
+
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,50 @@
1
+ # *****************************************************************************
2
+ # * Copyright (c) 2019 Matěj Outlý
3
+ # *****************************************************************************
4
+ # *
5
+ # * Notification receiver
6
+ # *
7
+ # * Author: Matěj Outlý
8
+ # * Date : 21. 1. 2016
9
+ # *
10
+ # *****************************************************************************
11
+
12
+ module Tolliver
13
+ module Services
14
+ module Policies
15
+ class Instantly
16
+
17
+ def deliver(notification_delivery)
18
+
19
+ # Input validation
20
+ return nil if notification_delivery.nil? || notification_delivery.policy != :instantly
21
+
22
+ # Nothing to do
23
+ return 0 if notification_delivery.sent_count == notification_delivery.receivers_count
24
+
25
+ # Get batch of receivers prepared for send
26
+ notification_receivers = notification_delivery.notification_receivers #.where(sent_at: nil)
27
+
28
+ # Send entire batch
29
+ sent_counter = 0
30
+ notification_receivers.each do |notification_receiver|
31
+ sent_counter += 1 if notification_delivery.method_service.deliver(notification_receiver)
32
+ end
33
+
34
+ # Is postponed
35
+ notification_delivery.is_postponed = false
36
+
37
+ # Update statistics
38
+ notification_delivery.sent_count += sent_counter
39
+ notification_delivery.sent_at = Time.current if notification_delivery.sent_count == notification_delivery.receivers_count
40
+
41
+ # Save
42
+ notification_delivery.save
43
+
44
+ (notification_delivery.receivers_count - notification_delivery.sent_count)
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ 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: 1.0.0
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matěj Outlý
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-18 00:00:00.000000000 Z
11
+ date: 2021-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '4.2'
19
+ version: '6.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '4.2'
26
+ version: '6.0'
27
27
  description: Tolliver is an engine for event notification and distribution among users
28
28
  via e-mail, SMS or other 3rd party systems.
29
29
  email:
@@ -36,11 +36,12 @@ files:
36
36
  - README.md
37
37
  - app/mailers/tolliver/notification_mailer.rb
38
38
  - app/models/tolliver/notification.rb
39
+ - app/models/tolliver/notification_attachment.rb
39
40
  - app/models/tolliver/notification_delivery.rb
40
41
  - app/models/tolliver/notification_receiver.rb
41
42
  - app/models/tolliver/notification_template.rb
42
- - app/views/notification_mailer/notify.html.erb
43
- - app/views/notification_mailer/notify.text.erb
43
+ - app/views/tolliver/notification_mailer/notify.html.erb
44
+ - app/views/tolliver/notification_mailer/notify.text.erb
44
45
  - config/locales/cs.yml
45
46
  - config/locales/en.yml
46
47
  - config/routes.rb
@@ -48,26 +49,36 @@ files:
48
49
  - db/migrate/20160121094838_create_notification_receivers.rb
49
50
  - db/migrate/20160509144238_create_notification_templates.rb
50
51
  - db/migrate/20170630190600_create_notification_deliveries.rb
52
+ - db/migrate/20201027150000_create_notification_attachments.rb
53
+ - db/migrate/20210415120000_add_notification_attachments_url.rb
51
54
  - lib/tolliver.rb
52
55
  - lib/tolliver/engine.rb
56
+ - lib/tolliver/errors/bad_request.rb
57
+ - lib/tolliver/errors/not_found.rb
58
+ - lib/tolliver/errors/standard_error.rb
59
+ - lib/tolliver/jobs/batch_policy_job.rb
60
+ - lib/tolliver/jobs/delivery_job.rb
53
61
  - lib/tolliver/mailers/notification_mailer.rb
54
62
  - lib/tolliver/models/notification.rb
63
+ - lib/tolliver/models/notification_attachment.rb
55
64
  - lib/tolliver/models/notification_delivery.rb
56
- - lib/tolliver/models/notification_delivery/batch.rb
57
- - lib/tolliver/models/notification_delivery/instantly.rb
58
65
  - lib/tolliver/models/notification_receiver.rb
59
- - lib/tolliver/models/notification_receiver/email.rb
60
- - lib/tolliver/models/notification_receiver/sms.rb
61
66
  - lib/tolliver/models/notification_template.rb
62
- - lib/tolliver/services/delivery.rb
63
- - lib/tolliver/services/notification.rb
64
- - lib/tolliver/services/sms/plivo.rb
67
+ - lib/tolliver/services/delivery_service.rb
68
+ - lib/tolliver/services/methods/email.rb
69
+ - lib/tolliver/services/methods/email/mailgun.rb
70
+ - lib/tolliver/services/methods/email/smtp.rb
71
+ - lib/tolliver/services/methods/sms.rb
72
+ - lib/tolliver/services/methods/sms/plivo.rb
73
+ - lib/tolliver/services/notification_service.rb
74
+ - lib/tolliver/services/policies/batch.rb
75
+ - lib/tolliver/services/policies/instantly.rb
65
76
  - lib/tolliver/utils/enum.rb
66
77
  homepage: https://github.com/matej-outly/tolliver
67
78
  licenses:
68
79
  - MIT
69
80
  metadata: {}
70
- post_install_message:
81
+ post_install_message:
71
82
  rdoc_options: []
72
83
  require_paths:
73
84
  - lib
@@ -83,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
94
  version: '0'
84
95
  requirements: []
85
96
  rubygems_version: 3.0.6
86
- signing_key:
97
+ signing_key:
87
98
  specification_version: 4
88
99
  summary: Event notification and distribution
89
100
  test_files: []
@@ -1 +0,0 @@
1
- <%= @notification.message.html_safe %>
@@ -1 +0,0 @@
1
- <%= @notification.message.to_s.strip_tags %>
@@ -1,77 +0,0 @@
1
- # *****************************************************************************
2
- # * Copyright (c) 2019 Matěj Outlý
3
- # *****************************************************************************
4
- # *
5
- # * Notification receiver
6
- # *
7
- # * Author: Matěj Outlý
8
- # * Date : 21. 1. 2016
9
- # *
10
- # *****************************************************************************
11
-
12
- module Tolliver
13
- module Models
14
- module NotificationDelivery
15
- module Batch
16
- extend ActiveSupport::Concern
17
-
18
- module ClassMethods
19
-
20
- protected
21
-
22
- def deliver_batch(notification_delivery_id, batch_size = 10)
23
-
24
- # Find notification delivery object
25
- notification_delivery = Tolliver.notification_delivery_model.find_by_id(notification_delivery_id)
26
- return nil if notification_delivery.nil? || notification_delivery.policy != :batch
27
-
28
- # Nothing to do
29
- return 0 if notification_delivery.sent_count == notification_delivery.receivers_count
30
-
31
- # Get batch of receivers prepared for send
32
- notification_receivers = notification_delivery.notification_receivers.where(sent_at: nil).limit(batch_size)
33
-
34
- # Send entire batch
35
- sent_counter = 0
36
- notification_receivers.each do |notification_receiver|
37
- sent_counter += 1 if notification_receiver.deliver
38
- end
39
-
40
- # Update statistics
41
- notification_delivery.sent_count += sent_counter
42
- notification_delivery.sent_at = Time.current if notification_delivery.sent_count == notification_delivery.receivers_count
43
-
44
- # Save
45
- notification_delivery.save
46
-
47
- return (notification_delivery.receivers_count - notification_delivery.sent_count)
48
-
49
- end
50
-
51
- def deliver_batch_and_enqueue(notification_delivery_id, batch_size = 10)
52
-
53
- # Send single batch
54
- remaining = deliver_batch(notification_delivery_id, batch_size)
55
- return nil if remaining.nil?
56
-
57
- # If still some receivers remaining, enqueue next batch
58
- if remaining > 0
59
-
60
- # Sleep for a while to prevent SMTP/SMS service overflow
61
- sleep 5 # seconds
62
-
63
- # Queue next batch
64
- QC.enqueue("#{self.to_s}.deliver_batch_and_enqueue", notification_delivery_id, batch_size)
65
- return false
66
- else
67
- return true
68
- end
69
-
70
- end
71
-
72
- end
73
-
74
- end
75
- end
76
- end
77
- end
@@ -1,55 +0,0 @@
1
- # *****************************************************************************
2
- # * Copyright (c) 2019 Matěj Outlý
3
- # *****************************************************************************
4
- # *
5
- # * Notification receiver
6
- # *
7
- # * Author: Matěj Outlý
8
- # * Date : 21. 1. 2016
9
- # *
10
- # *****************************************************************************
11
-
12
- module Tolliver
13
- module Models
14
- module NotificationDelivery
15
- module Instantly
16
- extend ActiveSupport::Concern
17
-
18
- module ClassMethods
19
-
20
- protected
21
-
22
- def deliver_instantly(notification_delivery_id)
23
-
24
- # Find notification delivery object
25
- notification_delivery = Tolliver.notification_delivery_model.find_by_id(notification_delivery_id)
26
- return nil if notification_delivery.nil? || notification_delivery.policy != :instantly
27
-
28
- # Nothing to do
29
- return 0 if notification_delivery.sent_count == notification_delivery.receivers_count
30
-
31
- # Get batch of receivers prepared for send
32
- notification_receivers = notification_delivery.notification_receivers #.where(sent_at: nil)
33
-
34
- # Send entire batch
35
- sent_counter = 0
36
- notification_receivers.each do |notification_receiver|
37
- sent_counter += 1 if notification_receiver.deliver
38
- end
39
-
40
- # Update statistics
41
- notification_delivery.sent_count += sent_counter
42
- notification_delivery.sent_at = Time.current if notification_delivery.sent_count == notification_delivery.receivers_count
43
-
44
- # Save
45
- notification_delivery.save
46
-
47
- return (notification_delivery.receivers_count - notification_delivery.sent_count)
48
- end
49
-
50
- end
51
-
52
- end
53
- end
54
- end
55
- end
@@ -1,46 +0,0 @@
1
- # *****************************************************************************
2
- # * Copyright (c) 2019 Matěj Outlý
3
- # *****************************************************************************
4
- # *
5
- # * Notification receiver
6
- # *
7
- # * Author: Matěj Outlý
8
- # * Date : 21. 1. 2016
9
- # *
10
- # *****************************************************************************
11
-
12
- module Tolliver
13
- module Models
14
- module NotificationReceiver
15
- module Email
16
- extend ActiveSupport::Concern
17
-
18
- protected
19
-
20
- # Send notification by e-mail
21
- def deliver_by_email
22
- notification = self.notification_delivery.notification
23
-
24
- # Send email
25
- begin
26
- Tolliver::NotificationMailer.notify(notification, self.receiver).deliver_now
27
- self.state = "sent"
28
- #rescue Net::SMTPFatalError, Net::SMTPSyntaxError
29
- rescue StandardError => e
30
- self.state = "error"
31
- self.error_message = e.message
32
- end
33
-
34
- # Mark as sent
35
- self.sent_at = Time.current
36
-
37
- # Save
38
- self.save
39
-
40
- return true
41
- end
42
-
43
- end
44
- end
45
- end
46
- end
@@ -1,45 +0,0 @@
1
- # *****************************************************************************
2
- # * Copyright (c) 2019 Matěj Outlý
3
- # *****************************************************************************
4
- # *
5
- # * Notification receiver
6
- # *
7
- # * Author: Matěj Outlý
8
- # * Date : 21. 1. 2016
9
- # *
10
- # *****************************************************************************
11
-
12
- module Tolliver
13
- module Models
14
- module NotificationReceiver
15
- module Sms
16
- extend ActiveSupport::Concern
17
-
18
- protected
19
-
20
- # Send notification by SMS
21
- def deliver_by_sms
22
- notification = self.notification_delivery.notification
23
-
24
- # Send SMS
25
- begin
26
- Tolliver.sms_provider_obj.deliver(self.receiver.phone, notification.message.strip_tags)
27
- self.state = "sent"
28
- rescue StandardError => e
29
- self.state = "error"
30
- self.error_message = e.message
31
- end
32
-
33
- # Mark as sent
34
- self.sent_at = Time.current
35
-
36
- # Save
37
- self.save
38
-
39
- return true
40
- end
41
-
42
- end
43
- end
44
- end
45
- end