tolliver 2.2.0 → 2.3.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: 1c0fa6672c057ce36fec360bddd1f1a7360dcdecb6198678838a6150978a8b12
4
- data.tar.gz: 13ddc4dba78f936159bc825f4cee588d66941a9ff296d5fe0abd70b6ae02cbf0
3
+ metadata.gz: 071baa1e13612b56607c74a22057e7a35dff214916a5cb6f8cf2a7640a54facf
4
+ data.tar.gz: d1b9e7e25cc83abc2ca03e2382dfd965fa8eb2bebd872696aa0e2f578247a7ef
5
5
  SHA512:
6
- metadata.gz: 4e2ddef5141f52daf5bdfb4d648c2420dad063f2ba1fd06d2753eca7f8e56e66184bfa72a60124ab7203e0821119796a398508df86a555d9fa2d7f77ac289736
7
- data.tar.gz: 23a5b86b0b8daac5a8f334e3d2bf53fb42b380fda433550c4742216d7631cd408920436467d85224d07c872b8fb3cb80f7db62d2d1403338e99456d6f4d73372
6
+ metadata.gz: 165fb812806151315d58b2482ac909d06dfbff0796d6d35eb19640b736988547f89dae17c71aadbfc01603cff4cdbdc82c2f7d9ee94a6d3c9f7a2439858a4f5b
7
+ data.tar.gz: 7d2ae9b7a4117cffa8f8baa260e338a890d3f9fc6b1a16a046a1b8d803369390179e12263cd46138a653758acf9cd873a31ee3d88100a0c02f3a4518f6cfada7
data/README.md CHANGED
@@ -5,6 +5,7 @@ send it in a batch with different delivery methods:
5
5
 
6
6
  - E-mail with SMTP aor Mailgun provider
7
7
  - SMS with Plivo provider
8
+ - Slack
8
9
  - Whatever custom delivery method
9
10
 
10
11
  ## 1. Installation
@@ -27,7 +28,6 @@ Add gem to your Gemfile:
27
28
  ```ruby
28
29
  gem 'plivo'
29
30
  ```
30
-
31
31
  ### 1.2. Mailgun support
32
32
 
33
33
  Add gem to your Gemfile:
@@ -35,6 +35,13 @@ Add gem to your Gemfile:
35
35
  ```ruby
36
36
  gem 'mailgun-ruby'
37
37
  ```
38
+ ### 1.3. Slack support
39
+
40
+ Add gem to your Gemfile:
41
+
42
+ ```ruby
43
+ gem 'slack-ruby-client'
44
+ ````
38
45
 
39
46
  ## 2. Configuration
40
47
 
@@ -63,6 +70,7 @@ Available options:
63
70
  - sms_sender
64
71
  - sms_provider
65
72
  - sms_provider_params
73
+ - slack_params
66
74
  - delivery_methods
67
75
 
68
76
  ### 2.1. Plivo support
@@ -0,0 +1,5 @@
1
+ class AddSlackId < ActiveRecord::Migration[6.0]
2
+ def change
3
+ add_column :notification_receivers, :receiver_slack_id, :string
4
+ end
5
+ end
@@ -23,13 +23,7 @@ module Tolliver
23
23
  has_many :notification_deliveries, class_name: Tolliver.notification_delivery_model.to_s, dependent: :destroy
24
24
  has_many :notification_receivers, class_name: Tolliver.notification_receiver_model.to_s, through: :notification_deliveries
25
25
  has_many :notification_attachments, class_name: Tolliver.notification_attachment_model.to_s, dependent: :destroy
26
- belongs_to :notification_template, class_name: Tolliver.notification_template_model.to_s
27
-
28
- # *********************************************************************
29
- # Validators
30
- # *********************************************************************
31
-
32
- validates_presence_of :subject
26
+ belongs_to :notification_template, class_name: Tolliver.notification_template_model.to_s, optional: true
33
27
 
34
28
  end
35
29
 
@@ -0,0 +1,90 @@
1
+ # *****************************************************************************
2
+ # * Copyright (c) 2019 Matěj Outlý
3
+ # *****************************************************************************
4
+ # *
5
+ # * Notification method - Slack
6
+ # *
7
+ # * Author: Matěj Outlý
8
+ # * Date : 21. 1. 2016
9
+ # *
10
+ # *****************************************************************************
11
+
12
+ module Tolliver
13
+ module Services
14
+ module Methods
15
+ class Slack
16
+
17
+ def is_notification_valid?(notification)
18
+ return false if notification.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_slack_id.blank? && notification_receiver.receiver_email.blank?
28
+ true
29
+ end
30
+
31
+ def deliver(notification_receiver)
32
+
33
+ # Prepare notification
34
+ notification = notification_receiver.notification_delivery.notification
35
+
36
+ # Send message
37
+ begin
38
+ ensure_slack_id(notification_receiver)
39
+ client.chat_postMessage(channel: notification_receiver.receiver_slack_id, text: notification.message)
40
+ notification_receiver.status = 'sent'
41
+ rescue StandardError => e
42
+ notification_receiver.status = 'error'
43
+ notification_receiver.error_message = e.message
44
+ end
45
+
46
+ # Mark as sent
47
+ notification_receiver.sent_at = Time.current
48
+
49
+ # Save
50
+ notification_receiver.save
51
+
52
+ true
53
+ end
54
+
55
+ private
56
+
57
+ def ensure_slack_id(notification_receiver)
58
+ if notification_receiver.receiver_slack_id.blank?
59
+ unless notification_receiver.receiver_email.blank?
60
+ response = client.users_lookupByEmail(email: notification_receiver.receiver_email)
61
+ if response.ok
62
+ notification_receiver.receiver_slack_id = response.user.id
63
+ notification_receiver.save(validate: false)
64
+ end
65
+ end
66
+ end
67
+ if notification_receiver.receiver_slack_id.blank?
68
+ raise Tolliver::Errors::StandardError.new('Receiver Slack ID not recognized.')
69
+ end
70
+ end
71
+
72
+ def client
73
+ if @client.nil?
74
+ require 'slack'
75
+ api_token = Tolliver.slack_params[:api_token]
76
+ if api_token.blank?
77
+ raise Tolliver::Errors::StandardError.new('Please provide API Token in Slack params.')
78
+ end
79
+ ::Slack.configure do |config|
80
+ config.token = api_token
81
+ end
82
+ @client = ::Slack::Web::Client.new
83
+ end
84
+ @client
85
+ end
86
+
87
+ end
88
+ end
89
+ end
90
+ end
@@ -53,7 +53,7 @@ module Tolliver
53
53
  end
54
54
 
55
55
  # Save to DB
56
- notification.save
56
+ notification.save!
57
57
 
58
58
  # Attachments
59
59
  unless options[:attachments].blank?
@@ -105,7 +105,7 @@ module Tolliver
105
105
  end
106
106
 
107
107
  # Must be persisted in DB before receiver created
108
- notification_delivery.save
108
+ notification_delivery.save!
109
109
 
110
110
  # Receivers
111
111
  receivers = options[:receivers] || []
@@ -121,13 +121,13 @@ module Tolliver
121
121
  notification_receiver.receiver_phone = receiver[:phone]
122
122
  raise Tolliver::Errors::BadRequest.new('Missing receiver ref.') if notification_receiver.receiver_ref.blank?
123
123
  if notification_delivery.method_service.is_notification_receiver_valid?(notification_receiver) # ignore receiver if not valid
124
- notification_receiver.save
124
+ notification_receiver.save!
125
125
  receivers_count += 1
126
126
  end
127
127
  end
128
128
  notification_delivery.sent_count = 0
129
129
  notification_delivery.receivers_count = receivers_count
130
- notification_delivery.save
130
+ notification_delivery.save!
131
131
 
132
132
  end
133
133
 
data/lib/tolliver.rb CHANGED
@@ -29,6 +29,7 @@ require "tolliver/services/methods/email/smtp"
29
29
  require "tolliver/services/methods/email/mailgun"
30
30
  require "tolliver/services/methods/sms"
31
31
  require "tolliver/services/methods/sms/plivo"
32
+ require "tolliver/services/methods/slack"
32
33
 
33
34
  # Jobs
34
35
  require "tolliver/jobs/delivery_job"
@@ -134,6 +135,7 @@ module Tolliver
134
135
  # Available methods:
135
136
  # - email
136
137
  # - sms
138
+ # - slack
137
139
  # - whatever
138
140
  mattr_accessor :delivery_methods
139
141
  @@delivery_methods = [
@@ -170,10 +172,14 @@ module Tolliver
170
172
 
171
173
  # Used SMS provider
172
174
  mattr_accessor :sms_provider
173
- @@sms_provider = nil
175
+ @@sms_provider = :plivo
174
176
 
175
177
  # SMS provider params
176
178
  mattr_accessor :sms_provider_params
177
179
  @@sms_provider_params = {}
178
180
 
181
+ # Slack provider params
182
+ mattr_accessor :slack_params
183
+ @@slack_params = {}
184
+
179
185
  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.2.0
4
+ version: 2.3.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-05-29 00:00:00.000000000 Z
11
+ date: 2022-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,6 +52,7 @@ files:
52
52
  - db/migrate/20201027150000_create_notification_attachments.rb
53
53
  - db/migrate/20210415120000_add_notification_attachments_url.rb
54
54
  - db/migrate/20210528120000_add_short_message_and_phone.rb
55
+ - db/migrate/20220805120000_add_slack_id.rb
55
56
  - lib/tolliver.rb
56
57
  - lib/tolliver/engine.rb
57
58
  - lib/tolliver/errors/bad_request.rb
@@ -69,6 +70,7 @@ files:
69
70
  - lib/tolliver/services/methods/email.rb
70
71
  - lib/tolliver/services/methods/email/mailgun.rb
71
72
  - lib/tolliver/services/methods/email/smtp.rb
73
+ - lib/tolliver/services/methods/slack.rb
72
74
  - lib/tolliver/services/methods/sms.rb
73
75
  - lib/tolliver/services/methods/sms/plivo.rb
74
76
  - lib/tolliver/services/notification_service.rb