noticed 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +235 -4
- data/Rakefile +12 -12
- data/app/channels/noticed/notification_channel.rb +15 -0
- data/lib/noticed.rb +30 -2
- data/lib/noticed/base.rb +104 -0
- data/lib/noticed/coder.rb +11 -0
- data/lib/noticed/delivery_methods/action_cable.rb +27 -0
- data/lib/noticed/delivery_methods/base.rb +23 -0
- data/lib/noticed/delivery_methods/database.rb +23 -0
- data/lib/noticed/delivery_methods/email.rb +19 -0
- data/lib/noticed/delivery_methods/slack.rb +27 -0
- data/lib/noticed/delivery_methods/test.rb +15 -0
- data/lib/noticed/delivery_methods/twilio.rb +51 -0
- data/lib/noticed/delivery_methods/vonage.rb +34 -0
- data/lib/noticed/engine.rb +4 -0
- data/lib/noticed/version.rb +1 -1
- metadata +57 -4
- data/lib/noticed/railtie.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a68b0a9d002890caff4346a36eac937e9efe9c759fa656586d7c23984f974593
|
4
|
+
data.tar.gz: 22f5537095dcb8fb2f0a09f558c7b277d9840611bc29fc263544cc6344edc7f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0a1fc6372bd1eb3f128eaecc8b05bbd37ee6a540f30ef05fafcd8b593dc01e00c53d4ecb135c93184c6d264940707b4513f0b33eeca4d57c52ee87b80870ca8
|
7
|
+
data.tar.gz: ee98827a78024e374126ad2f0f5fb9210320808fb244c4171ab543d27c2c875d6a05ca6045b4b3b2dab2fff17c9ec9ae514d3a7f98105d5b74642d0c6150bcec
|
data/README.md
CHANGED
@@ -1,8 +1,16 @@
|
|
1
|
-
# Noticed
|
2
|
-
Short description and motivation.
|
1
|
+
# Noticed - Notifications for your Ruby on Rails app.
|
3
2
|
|
4
|
-
|
5
|
-
|
3
|
+
[![Build Status](https://github.com/excid3/noticed/workflows/Tests/badge.svg)](https://github.com/excid3/noticed/actions)
|
4
|
+
|
5
|
+
Currently, we support these notification delivery methods out of the box:
|
6
|
+
|
7
|
+
* Database
|
8
|
+
* Email
|
9
|
+
* Websocket (realtime)
|
10
|
+
* Twilio (SMS)
|
11
|
+
* Vonage / Nexmo (SMS)
|
12
|
+
|
13
|
+
And you can easily add new notification types for any other delivery methods.
|
6
14
|
|
7
15
|
## Installation
|
8
16
|
Add this line to your application's Gemfile:
|
@@ -21,6 +29,229 @@ Or install it yourself as:
|
|
21
29
|
$ gem install noticed
|
22
30
|
```
|
23
31
|
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
You can define a Notification as a class that inherits from Noticed::Base. To add delivery methods, simply `include` the module for the delivery methods you would like to use.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class CommentNotification < Noticed::Base
|
38
|
+
deliver_by :database
|
39
|
+
deliver_by :action_cable
|
40
|
+
deliver_by :email, if: :email_notifications?
|
41
|
+
|
42
|
+
def email_notifications?
|
43
|
+
!!recipient.preferences[:email]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
To send a notification to a user:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
notification = CommentNotification.with(comment: @comment.to_gid)
|
52
|
+
|
53
|
+
# Deliver notification in background job
|
54
|
+
notification.deliver_later(@comment.post.author)
|
55
|
+
|
56
|
+
# Deliver notification immediately
|
57
|
+
notification.deliver(@comment.post.author)
|
58
|
+
```
|
59
|
+
|
60
|
+
This will instantiate a new notification with the `comment` global ID stored in the metadata.
|
61
|
+
|
62
|
+
Each delivery method is able to transfrom this metadata that's best for the format. For example, the database may simply store the comment so it can be linked when rendering in the navbar. The websocket mechanism may transform this into a browser notification or insert it into the navbar.
|
63
|
+
|
64
|
+
**Shared Options**
|
65
|
+
|
66
|
+
* `if: :method_name` - Calls `method_name`and cancels delivery method if `false` is returned
|
67
|
+
* `unless: :method_name` - Calls `method_name`and cancels delivery method if `true` is returned
|
68
|
+
|
69
|
+
## Delivery Methods
|
70
|
+
|
71
|
+
The delivery methods are designed to be overriden so that you can customi1ze the notification for each medium.
|
72
|
+
|
73
|
+
For example, emails will require a subject, body, and email address while an SMS requires a phone number and simple message. You can define the formats for each of these in your Notification and the delivery method will handle the processing of it.
|
74
|
+
|
75
|
+
### Database
|
76
|
+
|
77
|
+
Writes notification to the database.
|
78
|
+
|
79
|
+
`deliver_by :database`
|
80
|
+
|
81
|
+
**Note:** Database notifications are special in that they will run before the other delivery methods. We do this so you can reference the database record ID in other delivery methods.
|
82
|
+
|
83
|
+
### Email
|
84
|
+
|
85
|
+
Sends an email notification. Emails will always be sent with `deliver_later`
|
86
|
+
|
87
|
+
`deliver_by :email, mailer: "UserMailer"`
|
88
|
+
|
89
|
+
**Options**
|
90
|
+
|
91
|
+
* `mailer` - **Required**
|
92
|
+
|
93
|
+
The mailer that should send the email
|
94
|
+
|
95
|
+
* `method: :invoice_paid` - *Optional*
|
96
|
+
|
97
|
+
Used to customize the method on the mailer that is called
|
98
|
+
|
99
|
+
### ActionCable
|
100
|
+
|
101
|
+
Sends a notification to the browser via websockets (ActionCable channel by default).
|
102
|
+
|
103
|
+
`deliver_by :action_cable`
|
104
|
+
|
105
|
+
**Options**
|
106
|
+
|
107
|
+
* `format: :format_for_action_cable` - *Optional*
|
108
|
+
|
109
|
+
Use a custom method to define the Hash sent through ActionCable
|
110
|
+
|
111
|
+
* `channel` - *Optional*
|
112
|
+
|
113
|
+
Override the ActionCable channel used to send notifications.
|
114
|
+
|
115
|
+
Defaults to `Noticed::NotificationChannel`
|
116
|
+
|
117
|
+
### Slack
|
118
|
+
|
119
|
+
Sends a Slack notification via webhook.
|
120
|
+
|
121
|
+
`deliver_by :slack`
|
122
|
+
|
123
|
+
**Options**
|
124
|
+
|
125
|
+
* `format: :format_for_slack` - *Optional*
|
126
|
+
|
127
|
+
Use a custom method to define the payload sent to Slack. Method should return a Hash.
|
128
|
+
|
129
|
+
* `url: :url_for_slack` - *Optional*
|
130
|
+
|
131
|
+
Use a custom method to retrieve the Slack Webhook URL. Method should return a String.
|
132
|
+
|
133
|
+
Defaults to `Rails.application.credentials.slack[:notification_url]`
|
134
|
+
|
135
|
+
### Twilio SMS
|
136
|
+
|
137
|
+
Sends an SMS notification via Twilio.
|
138
|
+
|
139
|
+
`deliver_by :twilio`
|
140
|
+
|
141
|
+
**Options**
|
142
|
+
|
143
|
+
* `credentials: :get_twilio_credentials` - *Optional*
|
144
|
+
|
145
|
+
Use a custom method to retrieve the credentials for Twilio. Method should return a Hash with `:account_sid`, `:auth_token` and `:phone_ number` keys.
|
146
|
+
|
147
|
+
Defaults to `Rails.application.credentials.twilio[:account_sid]` and `Rails.application.credentials.twilio[:auth_token]`
|
148
|
+
|
149
|
+
* `url: :get_twilio_url` - *Optional*
|
150
|
+
|
151
|
+
Use a custom method to retrieve the Twilio URL. Method should return the Twilio API url as a string.
|
152
|
+
|
153
|
+
Defaults to `"https://api.twilio.com/2010-04-01/Accounts/#{twilio_credentials(recipient)[:account_sid]}/Messages.json"`
|
154
|
+
|
155
|
+
* `format: :format_for_twilio` - *Optional*
|
156
|
+
|
157
|
+
Use a custom method to define the payload sent to Twilio. Method should return a Hash.
|
158
|
+
|
159
|
+
Defaults to:
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
{
|
163
|
+
Body: notification.params[:message],
|
164
|
+
From: twilio_credentials[:number],
|
165
|
+
To: recipient.phone_number
|
166
|
+
}
|
167
|
+
```
|
168
|
+
|
169
|
+
### Vonage SMS
|
170
|
+
|
171
|
+
Sends an SMS notification vai Vonage / Nexmo.
|
172
|
+
|
173
|
+
`deliver_by :vonage`
|
174
|
+
|
175
|
+
**Options:**
|
176
|
+
|
177
|
+
* `credentials: :get_credentials` - *Optional*
|
178
|
+
|
179
|
+
Use a custom method for retrieving credentials. Method should return a Hash with `:api_key` and `:api_secret` keys.
|
180
|
+
|
181
|
+
Defaults to `Rails.application.credentials.vonage[:api_key]` and `Rails.application.credentials.vonage[:api_secret]`
|
182
|
+
|
183
|
+
* `deliver_by :vonage, format: :format_for_vonage` - *Optional*
|
184
|
+
|
185
|
+
Use a custom method to generate the params sent to Vonage. Method should return a Hash. Defaults to:
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
{
|
189
|
+
api_key: vonage_credentials[:api_key],
|
190
|
+
api_secret: vonage_credentials[:api_secret],
|
191
|
+
from: notification.params[:from],
|
192
|
+
text: notification.params[:body],
|
193
|
+
to: notification.params[:to],
|
194
|
+
type: "unicode"
|
195
|
+
}
|
196
|
+
```
|
197
|
+
|
198
|
+
### User Preferences
|
199
|
+
|
200
|
+
Each delivery method implements a `deliver_with_#{name}` method that receives the recipient as the first argument. You can override this method to check the user's preferences and skip processing if they have disabled that type of notification.
|
201
|
+
|
202
|
+
For example:
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
class CommentNotification < Noticed::Base
|
206
|
+
deliver_by :email, if: :email_notifications?
|
207
|
+
|
208
|
+
def email_notifications?
|
209
|
+
recipient.email_notifications?
|
210
|
+
end
|
211
|
+
end
|
212
|
+
```
|
213
|
+
|
214
|
+
### Custom Delivery Methods
|
215
|
+
|
216
|
+
You can define a custom delivery method easily by adding a `deliver_by` line with a unique name and class option. The class will be instantiated and should inherit from `Noticed::DeliveryMethods::Base`.
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
class MyNotification < Noticed::Base
|
220
|
+
deliver_by :discord, class: "DiscordNotification"
|
221
|
+
end
|
222
|
+
```
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
class DiscordNotification < Noticed::DeliveryMethods::Base
|
226
|
+
def deliver
|
227
|
+
# Logic for sending a Discord notification
|
228
|
+
end
|
229
|
+
end
|
230
|
+
```
|
231
|
+
|
232
|
+
Delivery methods have access to the following methods and attributes:
|
233
|
+
|
234
|
+
* `notification` - The instance of the Notification. You can call methods on the notification to let the user easily override formatting and other functionality of the delivery method.
|
235
|
+
* `options` - Any configuration options on the `deliver_by` line.
|
236
|
+
* `recipient` - The object who should receive the notification. This is typically a User, Account, or other ActiveRecord model.
|
237
|
+
* `params` - The params passed into the notification. This is details about the event that happened. For example, a user commenting on a post would have params of `{ user: User.first }`
|
238
|
+
|
239
|
+
#### Limitations
|
240
|
+
|
241
|
+
Rails 6.1+ can serialize Class and Module objects as arguments to ActiveJob. The following syntax should work for Rails 6.1+:
|
242
|
+
|
243
|
+
```ruby
|
244
|
+
deliver_by DiscordNotification
|
245
|
+
```
|
246
|
+
|
247
|
+
For Rails 6.0 and earlier, you must pass strings of the class names in the `deliver_by` options.
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
deliver_by :discord, class: "DiscordNotification"
|
251
|
+
```
|
252
|
+
|
253
|
+
We recommend the Rails 6.0 compatible options to prevent confusion.
|
254
|
+
|
24
255
|
## Contributing
|
25
256
|
Contribution directions go here.
|
26
257
|
|
data/Rakefile
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
begin
|
2
|
-
require
|
2
|
+
require "bundler/setup"
|
3
3
|
rescue LoadError
|
4
|
-
puts
|
4
|
+
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
|
5
5
|
end
|
6
6
|
|
7
|
-
require
|
7
|
+
require "rdoc/task"
|
8
8
|
|
9
9
|
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
-
rdoc.rdoc_dir =
|
11
|
-
rdoc.title
|
12
|
-
rdoc.options <<
|
13
|
-
rdoc.rdoc_files.include(
|
14
|
-
rdoc.rdoc_files.include(
|
10
|
+
rdoc.rdoc_dir = "rdoc"
|
11
|
+
rdoc.title = "Noticed"
|
12
|
+
rdoc.options << "--line-numbers"
|
13
|
+
rdoc.rdoc_files.include("README.md")
|
14
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
15
15
|
end
|
16
16
|
|
17
|
-
require
|
17
|
+
require "bundler/gem_tasks"
|
18
18
|
|
19
|
-
require
|
19
|
+
require "rake/testtask"
|
20
20
|
|
21
21
|
Rake::TestTask.new(:test) do |t|
|
22
|
-
t.libs <<
|
23
|
-
t.pattern =
|
22
|
+
t.libs << "test"
|
23
|
+
t.pattern = "test/**/*_test.rb"
|
24
24
|
t.verbose = false
|
25
25
|
end
|
26
26
|
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Noticed
|
2
|
+
class NotificationChannel < ApplicationCable::Channel
|
3
|
+
def subscribed
|
4
|
+
stream_for current_user
|
5
|
+
end
|
6
|
+
|
7
|
+
def unsubscribed
|
8
|
+
stop_all_streams
|
9
|
+
end
|
10
|
+
|
11
|
+
def mark_as_read(data)
|
12
|
+
current_user.notifications.where(id: data["ids"]).mark_as_read!
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/noticed.rb
CHANGED
@@ -1,5 +1,33 @@
|
|
1
|
-
require "
|
1
|
+
require "http"
|
2
|
+
require "noticed/engine"
|
2
3
|
|
3
4
|
module Noticed
|
4
|
-
|
5
|
+
autoload :Base, "noticed/base"
|
6
|
+
autoload :Coder, "noticed/coder"
|
7
|
+
|
8
|
+
module DeliveryMethods
|
9
|
+
autoload :Base, "noticed/delivery_methods/base"
|
10
|
+
autoload :ActionCable, "noticed/delivery_methods/action_cable"
|
11
|
+
autoload :Database, "noticed/delivery_methods/database"
|
12
|
+
autoload :Email, "noticed/delivery_methods/email"
|
13
|
+
autoload :Slack, "noticed/delivery_methods/slack"
|
14
|
+
autoload :Test, "noticed/delivery_methods/test"
|
15
|
+
autoload :Twilio, "noticed/delivery_methods/twilio"
|
16
|
+
autoload :Vonage, "noticed/delivery_methods/vonage"
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.notify(recipients:, notification:)
|
20
|
+
recipients.each do |recipient|
|
21
|
+
notification.notify(recipient)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Clear the recipient after sending to the group
|
25
|
+
notification.recipient = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
mattr_accessor :parent_class
|
29
|
+
@@parent_class = "ApplicationJob"
|
30
|
+
|
31
|
+
class ValidationError < StandardError
|
32
|
+
end
|
5
33
|
end
|
data/lib/noticed/base.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
module Noticed
|
2
|
+
class Base
|
3
|
+
class_attribute :delivery_methods, instance_writer: false, default: []
|
4
|
+
class_attribute :param_names, instance_writer: false, default: []
|
5
|
+
|
6
|
+
attr_accessor :record
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def deliver_by(name, options = {})
|
10
|
+
delivery_methods.push(
|
11
|
+
name: name,
|
12
|
+
options: options
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Copy delivery methods from parent
|
17
|
+
def inherited(base) #:nodoc:
|
18
|
+
base.delivery_methods = delivery_methods.dup
|
19
|
+
base.param_names = param_names.dup
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
23
|
+
def with(params)
|
24
|
+
new(params)
|
25
|
+
end
|
26
|
+
|
27
|
+
def param(name)
|
28
|
+
param_names.push(name)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def initialize(params = {})
|
33
|
+
@params = params
|
34
|
+
end
|
35
|
+
|
36
|
+
def deliver(recipient)
|
37
|
+
validate!
|
38
|
+
run_delivery(recipient, enqueue: false)
|
39
|
+
end
|
40
|
+
|
41
|
+
def deliver_later(recipient)
|
42
|
+
validate!
|
43
|
+
run_delivery(recipient, enqueue: true)
|
44
|
+
end
|
45
|
+
|
46
|
+
def params
|
47
|
+
@params || {}
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
# Runs all delivery methods for a notification
|
53
|
+
def run_delivery(recipient, enqueue: true)
|
54
|
+
delivery_methods = self.class.delivery_methods.dup
|
55
|
+
|
56
|
+
# Run database delivery inline first if it exists so other methods have access to the record
|
57
|
+
if (index = delivery_methods.find_index { |m| m[:name] == :database })
|
58
|
+
delivery_method = delivery_methods.delete_at(index)
|
59
|
+
@record = run_delivery_method(delivery_method, recipient: recipient, enqueue: false)
|
60
|
+
end
|
61
|
+
|
62
|
+
delivery_methods.each do |delivery_method|
|
63
|
+
run_delivery_method(delivery_method, recipient: recipient, enqueue: enqueue)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Actually runs an individual delivery
|
68
|
+
def run_delivery_method(delivery_method, recipient:, enqueue:)
|
69
|
+
return if (delivery_method_name = delivery_method.dig(:options, :if)) && !send(delivery_method_name)
|
70
|
+
return if (delivery_method_name = delivery_method.dig(:options, :unless)) && send(delivery_method_name)
|
71
|
+
|
72
|
+
args = {
|
73
|
+
notification_class: self.class.name,
|
74
|
+
options: delivery_method[:options],
|
75
|
+
params: params,
|
76
|
+
recipient: recipient,
|
77
|
+
record: record
|
78
|
+
}
|
79
|
+
|
80
|
+
klass = get_class(delivery_method[:name], delivery_method[:options])
|
81
|
+
enqueue ? klass.perform_later(args) : klass.perform_now(args)
|
82
|
+
end
|
83
|
+
|
84
|
+
# Retrieves the correct class for a delivery method
|
85
|
+
def get_class(name, options)
|
86
|
+
if name.is_a? Class
|
87
|
+
name
|
88
|
+
elsif options[:class]
|
89
|
+
options[:class].constantize
|
90
|
+
else
|
91
|
+
"Noticed::DeliveryMethods::#{name.to_s.classify}".constantize
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Validates that all params are present
|
96
|
+
def validate!
|
97
|
+
self.class.param_names.each do |param_name|
|
98
|
+
if params[param_name].nil?
|
99
|
+
raise ValidationError, "#{param_name} is missing."
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Noticed
|
2
|
+
module DeliveryMethods
|
3
|
+
class ActionCable < Base
|
4
|
+
def deliver
|
5
|
+
channel.broadcast_to recipient, format
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def format
|
11
|
+
if (method = options[:format])
|
12
|
+
notification.send(method)
|
13
|
+
else
|
14
|
+
notification.params
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def channel
|
19
|
+
if (method = options[:channel])
|
20
|
+
notification.send(method)
|
21
|
+
else
|
22
|
+
Noticed::NotificationChannel
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Noticed
|
2
|
+
module DeliveryMethods
|
3
|
+
class Base < Noticed.parent_class.constantize
|
4
|
+
attr_reader :notification, :options, :recipient
|
5
|
+
delegate :params, to: :notification
|
6
|
+
|
7
|
+
def perform(notification_class:, options:, params:, recipient:, record:)
|
8
|
+
@notification = notification_class.constantize.new(params)
|
9
|
+
@options = options
|
10
|
+
@recipient = recipient
|
11
|
+
|
12
|
+
# Keep track of the database record for rendering
|
13
|
+
@notification.record = record
|
14
|
+
|
15
|
+
deliver
|
16
|
+
end
|
17
|
+
|
18
|
+
def deliver
|
19
|
+
raise NotImplementedError, "Delivery methods must implement a deliver method"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Noticed
|
2
|
+
module DeliveryMethods
|
3
|
+
class Database < Base
|
4
|
+
# Must return the database record
|
5
|
+
def deliver
|
6
|
+
recipient.notifications.create(attributes)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def attributes
|
12
|
+
if (method = options[:format])
|
13
|
+
notification.send(method)
|
14
|
+
else
|
15
|
+
{
|
16
|
+
type: notification.class.name,
|
17
|
+
params: params
|
18
|
+
}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Noticed
|
2
|
+
module DeliveryMethods
|
3
|
+
class Email < Base
|
4
|
+
def deliver
|
5
|
+
mailer.with(notification.params).send(method.to_sym).deliver_later
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def mailer
|
11
|
+
options.fetch(:mailer).constantize
|
12
|
+
end
|
13
|
+
|
14
|
+
def method
|
15
|
+
options[:method] || notification.class.name.underscore
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Noticed
|
2
|
+
module DeliveryMethods
|
3
|
+
class Slack < Base
|
4
|
+
def deliver
|
5
|
+
HTTP.post(url, json: format)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def format
|
11
|
+
if (method = options[:format])
|
12
|
+
notification.send(method)
|
13
|
+
else
|
14
|
+
params
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def url
|
19
|
+
if (method = options[:url])
|
20
|
+
notification.send(method)
|
21
|
+
else
|
22
|
+
Rails.application.credentials.slack[:notification_url]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Noticed
|
2
|
+
module DeliveryMethods
|
3
|
+
class Twilio < Base
|
4
|
+
def deliver
|
5
|
+
HTTP.basic_auth(user: account_sid, pass: auth_token).post(url, json: format)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def format
|
11
|
+
if (method = options[:format])
|
12
|
+
notification.send(method)
|
13
|
+
else
|
14
|
+
{
|
15
|
+
From: phone_number,
|
16
|
+
To: recipient.phone_number,
|
17
|
+
Body: notification.params[:message]
|
18
|
+
}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def url
|
23
|
+
if (method = options[:url])
|
24
|
+
notification.send(method)
|
25
|
+
else
|
26
|
+
"https://api.twilio.com/2010-04-01/Accounts/#{account_sid}/Messages.json"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def account_sid
|
31
|
+
credentials.fetch(:account_sid)
|
32
|
+
end
|
33
|
+
|
34
|
+
def auth_token
|
35
|
+
credentials.fetch(:auth_token)
|
36
|
+
end
|
37
|
+
|
38
|
+
def phone_number
|
39
|
+
credentials.fetch(:phone_number)
|
40
|
+
end
|
41
|
+
|
42
|
+
def credentials
|
43
|
+
if (method = options[:credentials])
|
44
|
+
notification.send(method)
|
45
|
+
else
|
46
|
+
Rails.application.credentials.twilio
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Noticed
|
2
|
+
module DeliveryMethods
|
3
|
+
class Vonage < Base
|
4
|
+
def deliver
|
5
|
+
HTTP.post("https://rest.nexmo.com/sms/json", json: format)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def format
|
11
|
+
if (method = options[:format])
|
12
|
+
notification.send(method)
|
13
|
+
else
|
14
|
+
{
|
15
|
+
api_key: credentials[:api_key],
|
16
|
+
api_secret: credentials[:api_secret],
|
17
|
+
from: notification.params[:from],
|
18
|
+
text: notification.params[:body],
|
19
|
+
to: notification.params[:to],
|
20
|
+
type: "unicode"
|
21
|
+
}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def credentials
|
26
|
+
if (method = options[:credentials])
|
27
|
+
notification.send(method)
|
28
|
+
else
|
29
|
+
Rails.application.credentials.vonage
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/noticed/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: noticed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Oliver
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -25,7 +25,49 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 6.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: http
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pg
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: standard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mocha
|
29
71
|
requirement: !ruby/object:Gem::Requirement
|
30
72
|
requirements:
|
31
73
|
- - ">="
|
@@ -49,8 +91,19 @@ files:
|
|
49
91
|
- MIT-LICENSE
|
50
92
|
- README.md
|
51
93
|
- Rakefile
|
94
|
+
- app/channels/noticed/notification_channel.rb
|
52
95
|
- lib/noticed.rb
|
53
|
-
- lib/noticed/
|
96
|
+
- lib/noticed/base.rb
|
97
|
+
- lib/noticed/coder.rb
|
98
|
+
- lib/noticed/delivery_methods/action_cable.rb
|
99
|
+
- lib/noticed/delivery_methods/base.rb
|
100
|
+
- lib/noticed/delivery_methods/database.rb
|
101
|
+
- lib/noticed/delivery_methods/email.rb
|
102
|
+
- lib/noticed/delivery_methods/slack.rb
|
103
|
+
- lib/noticed/delivery_methods/test.rb
|
104
|
+
- lib/noticed/delivery_methods/twilio.rb
|
105
|
+
- lib/noticed/delivery_methods/vonage.rb
|
106
|
+
- lib/noticed/engine.rb
|
54
107
|
- lib/noticed/version.rb
|
55
108
|
- lib/tasks/noticed_tasks.rake
|
56
109
|
homepage: https://github.com/excid3/noticed
|
data/lib/noticed/railtie.rb
DELETED