noticed 1.6.0 → 2.0.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 +4 -4
- data/README.md +122 -147
- data/app/jobs/noticed/application_job.rb +9 -0
- data/app/jobs/noticed/event_job.rb +19 -0
- data/app/models/concerns/noticed/deliverable.rb +115 -0
- data/app/models/concerns/noticed/notification_methods.rb +17 -0
- data/app/models/concerns/noticed/readable.rb +62 -0
- data/app/models/noticed/application_record.rb +6 -0
- data/app/models/noticed/deliverable/deliver_by.rb +43 -0
- data/app/models/noticed/event.rb +15 -0
- data/app/models/noticed/notification.rb +16 -0
- data/db/migrate/20231215190233_create_noticed_tables.rb +25 -0
- data/lib/generators/noticed/delivery_method_generator.rb +1 -1
- data/lib/generators/noticed/install_generator.rb +19 -0
- data/lib/generators/noticed/{notification_generator.rb → notifier_generator.rb} +2 -2
- data/lib/generators/noticed/templates/README +5 -4
- data/lib/generators/noticed/templates/notifier.rb.tt +24 -0
- data/lib/noticed/api_client.rb +44 -0
- data/lib/noticed/bulk_delivery_method.rb +46 -0
- data/lib/noticed/bulk_delivery_methods/discord.rb +11 -0
- data/lib/noticed/bulk_delivery_methods/slack.rb +17 -0
- data/lib/noticed/bulk_delivery_methods/webhook.rb +18 -0
- data/lib/noticed/coder.rb +2 -0
- data/lib/noticed/delivery_method.rb +50 -0
- data/lib/noticed/delivery_methods/action_cable.rb +7 -39
- data/lib/noticed/delivery_methods/discord.rb +11 -0
- data/lib/noticed/delivery_methods/email.rb +9 -45
- data/lib/noticed/delivery_methods/fcm.rb +23 -64
- data/lib/noticed/delivery_methods/ios.rb +25 -112
- data/lib/noticed/delivery_methods/microsoft_teams.rb +5 -22
- data/lib/noticed/delivery_methods/slack.rb +6 -16
- data/lib/noticed/delivery_methods/test.rb +2 -12
- data/lib/noticed/delivery_methods/twilio_messaging.rb +37 -0
- data/lib/noticed/delivery_methods/vonage_sms.rb +20 -0
- data/lib/noticed/delivery_methods/webhook.rb +17 -0
- data/lib/noticed/engine.rb +1 -9
- data/lib/noticed/required_options.rb +21 -0
- data/lib/noticed/translation.rb +7 -3
- data/lib/noticed/version.rb +1 -1
- data/lib/noticed.rb +30 -15
- metadata +29 -106
- data/lib/generators/noticed/model_generator.rb +0 -63
- data/lib/generators/noticed/templates/notification.rb.tt +0 -27
- data/lib/noticed/base.rb +0 -160
- data/lib/noticed/delivery_methods/base.rb +0 -93
- data/lib/noticed/delivery_methods/database.rb +0 -34
- data/lib/noticed/delivery_methods/twilio.rb +0 -51
- data/lib/noticed/delivery_methods/vonage.rb +0 -40
- data/lib/noticed/has_notifications.rb +0 -49
- data/lib/noticed/model.rb +0 -81
- data/lib/noticed/notification_channel.rb +0 -15
- data/lib/noticed/text_coder.rb +0 -16
- data/lib/rails_6_polyfills/actioncable/test_adapter.rb +0 -70
- data/lib/rails_6_polyfills/actioncable/test_helper.rb +0 -143
- data/lib/rails_6_polyfills/activejob/serializers.rb +0 -240
- data/lib/rails_6_polyfills/base.rb +0 -18
- data/lib/tasks/noticed_tasks.rake +0 -4
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: 2.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:
|
|
11
|
+
date: 2024-01-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -16,98 +16,14 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
19
|
+
version: 6.1.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:
|
|
27
|
-
- !ruby/object:Gem::Dependency
|
|
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: webmock
|
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
|
72
|
-
requirements:
|
|
73
|
-
- - ">="
|
|
74
|
-
- !ruby/object:Gem::Version
|
|
75
|
-
version: '0'
|
|
76
|
-
type: :development
|
|
77
|
-
prerelease: false
|
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
-
requirements:
|
|
80
|
-
- - ">="
|
|
81
|
-
- !ruby/object:Gem::Version
|
|
82
|
-
version: '0'
|
|
83
|
-
- !ruby/object:Gem::Dependency
|
|
84
|
-
name: mysql2
|
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
|
86
|
-
requirements:
|
|
87
|
-
- - ">="
|
|
88
|
-
- !ruby/object:Gem::Version
|
|
89
|
-
version: '0'
|
|
90
|
-
type: :development
|
|
91
|
-
prerelease: false
|
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
-
requirements:
|
|
94
|
-
- - ">="
|
|
95
|
-
- !ruby/object:Gem::Version
|
|
96
|
-
version: '0'
|
|
97
|
-
- !ruby/object:Gem::Dependency
|
|
98
|
-
name: sqlite3
|
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
|
100
|
-
requirements:
|
|
101
|
-
- - ">="
|
|
102
|
-
- !ruby/object:Gem::Version
|
|
103
|
-
version: '0'
|
|
104
|
-
type: :development
|
|
105
|
-
prerelease: false
|
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
-
requirements:
|
|
108
|
-
- - ">="
|
|
109
|
-
- !ruby/object:Gem::Version
|
|
110
|
-
version: '0'
|
|
26
|
+
version: 6.1.0
|
|
111
27
|
description: Database, browser, realtime ActionCable, Email, SMS, Slack notifications,
|
|
112
28
|
and more for Rails apps
|
|
113
29
|
email:
|
|
@@ -119,38 +35,45 @@ files:
|
|
|
119
35
|
- MIT-LICENSE
|
|
120
36
|
- README.md
|
|
121
37
|
- Rakefile
|
|
38
|
+
- app/jobs/noticed/application_job.rb
|
|
39
|
+
- app/jobs/noticed/event_job.rb
|
|
40
|
+
- app/models/concerns/noticed/deliverable.rb
|
|
41
|
+
- app/models/concerns/noticed/notification_methods.rb
|
|
42
|
+
- app/models/concerns/noticed/readable.rb
|
|
43
|
+
- app/models/noticed/application_record.rb
|
|
44
|
+
- app/models/noticed/deliverable/deliver_by.rb
|
|
45
|
+
- app/models/noticed/event.rb
|
|
46
|
+
- app/models/noticed/notification.rb
|
|
47
|
+
- db/migrate/20231215190233_create_noticed_tables.rb
|
|
122
48
|
- lib/generators/noticed/delivery_method_generator.rb
|
|
123
|
-
- lib/generators/noticed/
|
|
124
|
-
- lib/generators/noticed/
|
|
49
|
+
- lib/generators/noticed/install_generator.rb
|
|
50
|
+
- lib/generators/noticed/notifier_generator.rb
|
|
125
51
|
- lib/generators/noticed/templates/README
|
|
126
52
|
- lib/generators/noticed/templates/delivery_method.rb.tt
|
|
127
|
-
- lib/generators/noticed/templates/
|
|
53
|
+
- lib/generators/noticed/templates/notifier.rb.tt
|
|
128
54
|
- lib/noticed.rb
|
|
129
|
-
- lib/noticed/
|
|
55
|
+
- lib/noticed/api_client.rb
|
|
56
|
+
- lib/noticed/bulk_delivery_method.rb
|
|
57
|
+
- lib/noticed/bulk_delivery_methods/discord.rb
|
|
58
|
+
- lib/noticed/bulk_delivery_methods/slack.rb
|
|
59
|
+
- lib/noticed/bulk_delivery_methods/webhook.rb
|
|
130
60
|
- lib/noticed/coder.rb
|
|
61
|
+
- lib/noticed/delivery_method.rb
|
|
131
62
|
- lib/noticed/delivery_methods/action_cable.rb
|
|
132
|
-
- lib/noticed/delivery_methods/
|
|
133
|
-
- lib/noticed/delivery_methods/database.rb
|
|
63
|
+
- lib/noticed/delivery_methods/discord.rb
|
|
134
64
|
- lib/noticed/delivery_methods/email.rb
|
|
135
65
|
- lib/noticed/delivery_methods/fcm.rb
|
|
136
66
|
- lib/noticed/delivery_methods/ios.rb
|
|
137
67
|
- lib/noticed/delivery_methods/microsoft_teams.rb
|
|
138
68
|
- lib/noticed/delivery_methods/slack.rb
|
|
139
69
|
- lib/noticed/delivery_methods/test.rb
|
|
140
|
-
- lib/noticed/delivery_methods/
|
|
141
|
-
- lib/noticed/delivery_methods/
|
|
70
|
+
- lib/noticed/delivery_methods/twilio_messaging.rb
|
|
71
|
+
- lib/noticed/delivery_methods/vonage_sms.rb
|
|
72
|
+
- lib/noticed/delivery_methods/webhook.rb
|
|
142
73
|
- lib/noticed/engine.rb
|
|
143
|
-
- lib/noticed/
|
|
144
|
-
- lib/noticed/model.rb
|
|
145
|
-
- lib/noticed/notification_channel.rb
|
|
146
|
-
- lib/noticed/text_coder.rb
|
|
74
|
+
- lib/noticed/required_options.rb
|
|
147
75
|
- lib/noticed/translation.rb
|
|
148
76
|
- lib/noticed/version.rb
|
|
149
|
-
- lib/rails_6_polyfills/actioncable/test_adapter.rb
|
|
150
|
-
- lib/rails_6_polyfills/actioncable/test_helper.rb
|
|
151
|
-
- lib/rails_6_polyfills/activejob/serializers.rb
|
|
152
|
-
- lib/rails_6_polyfills/base.rb
|
|
153
|
-
- lib/tasks/noticed_tasks.rake
|
|
154
77
|
homepage: https://github.com/excid3/noticed
|
|
155
78
|
licenses:
|
|
156
79
|
- MIT
|
|
@@ -170,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
170
93
|
- !ruby/object:Gem::Version
|
|
171
94
|
version: '0'
|
|
172
95
|
requirements: []
|
|
173
|
-
rubygems_version: 3.3
|
|
96
|
+
rubygems_version: 3.5.3
|
|
174
97
|
signing_key:
|
|
175
98
|
specification_version: 4
|
|
176
99
|
summary: Notifications for Ruby on Rails applications
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "rails/generators/named_base"
|
|
4
|
-
|
|
5
|
-
module Noticed
|
|
6
|
-
module Generators
|
|
7
|
-
class ModelGenerator < Rails::Generators::NamedBase
|
|
8
|
-
include Rails::Generators::ResourceHelpers
|
|
9
|
-
|
|
10
|
-
source_root File.expand_path("../templates", __FILE__)
|
|
11
|
-
|
|
12
|
-
desc "Generates a Notification model for storing notifications."
|
|
13
|
-
|
|
14
|
-
argument :name, type: :string, default: "Notification", banner: "Notification"
|
|
15
|
-
argument :attributes, type: :array, default: [], banner: "field:type field:type"
|
|
16
|
-
|
|
17
|
-
def generate_notification
|
|
18
|
-
generate :model, name, "recipient:references{polymorphic}", "type", params_column, "read_at:datetime:index", *attributes
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def add_noticed_model
|
|
22
|
-
inject_into_class model_path, class_name, " include Noticed::Model\n"
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def add_not_nullable
|
|
26
|
-
migration_path = Dir.glob(Rails.root.join("db/migrate/*")).max_by { |f| File.mtime(f) }
|
|
27
|
-
|
|
28
|
-
# Force is required because null: false already exists in the file and Thor isn't smart enough to tell the difference
|
|
29
|
-
insert_into_file migration_path, after: "t.string :type", force: true do
|
|
30
|
-
", null: false"
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def done
|
|
35
|
-
readme "README" if behavior == :invoke
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
private
|
|
39
|
-
|
|
40
|
-
def model_path
|
|
41
|
-
@model_path ||= File.join("app", "models", "#{file_path}.rb")
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def params_column
|
|
45
|
-
case current_adapter
|
|
46
|
-
when "postgresql", "postgis"
|
|
47
|
-
"params:jsonb"
|
|
48
|
-
else
|
|
49
|
-
# MySQL and SQLite both support json
|
|
50
|
-
"params:json"
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def current_adapter
|
|
55
|
-
if ActiveRecord::Base.respond_to?(:connection_db_config)
|
|
56
|
-
ActiveRecord::Base.connection_db_config.adapter
|
|
57
|
-
else
|
|
58
|
-
ActiveRecord::Base.connection_config[:adapter]
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
end
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# To deliver this notification:
|
|
2
|
-
#
|
|
3
|
-
# <%= class_name %>.with(post: @post).deliver_later(current_user)
|
|
4
|
-
# <%= class_name %>.with(post: @post).deliver(current_user)
|
|
5
|
-
|
|
6
|
-
class <%= class_name %> < Noticed::Base
|
|
7
|
-
# Add your delivery methods
|
|
8
|
-
#
|
|
9
|
-
# deliver_by :database
|
|
10
|
-
# deliver_by :email, mailer: "UserMailer"
|
|
11
|
-
# deliver_by :slack
|
|
12
|
-
# deliver_by :custom, class: "MyDeliveryMethod"
|
|
13
|
-
|
|
14
|
-
# Add required params
|
|
15
|
-
#
|
|
16
|
-
# param :post
|
|
17
|
-
|
|
18
|
-
# Define helper methods to make rendering easier.
|
|
19
|
-
#
|
|
20
|
-
# def message
|
|
21
|
-
# t(".message")
|
|
22
|
-
# end
|
|
23
|
-
#
|
|
24
|
-
# def url
|
|
25
|
-
# post_path(params[:post])
|
|
26
|
-
# end
|
|
27
|
-
end
|
data/lib/noticed/base.rb
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
module Noticed
|
|
2
|
-
class Base
|
|
3
|
-
include Translation
|
|
4
|
-
include Rails.application.routes.url_helpers
|
|
5
|
-
|
|
6
|
-
extend ActiveModel::Callbacks
|
|
7
|
-
define_model_callbacks :deliver
|
|
8
|
-
|
|
9
|
-
class_attribute :delivery_methods, instance_writer: false, default: []
|
|
10
|
-
class_attribute :param_names, instance_writer: false, default: []
|
|
11
|
-
|
|
12
|
-
# Gives notifications access to the record and recipient during delivery
|
|
13
|
-
attr_accessor :record, :recipient
|
|
14
|
-
|
|
15
|
-
delegate :read?, :unread?, to: :record
|
|
16
|
-
|
|
17
|
-
class << self
|
|
18
|
-
def deliver_by(name, options = {})
|
|
19
|
-
delivery_methods.push(name: name, options: options)
|
|
20
|
-
define_model_callbacks(name)
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
# Copy delivery methods from parent
|
|
24
|
-
def inherited(base) # :nodoc:
|
|
25
|
-
base.delivery_methods = delivery_methods.dup
|
|
26
|
-
base.param_names = param_names.dup
|
|
27
|
-
super
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def with(params)
|
|
31
|
-
new(params)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# Shortcut for delivering without params
|
|
35
|
-
def deliver(recipients)
|
|
36
|
-
new.deliver(recipients)
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
# Shortcut for delivering later without params
|
|
40
|
-
def deliver_later(recipients)
|
|
41
|
-
new.deliver_later(recipients)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def params(*names)
|
|
45
|
-
param_names.concat Array.wrap(names)
|
|
46
|
-
end
|
|
47
|
-
alias_method :param, :params
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
def initialize(params = {})
|
|
51
|
-
@params = params
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def deliver(recipients)
|
|
55
|
-
validate!
|
|
56
|
-
|
|
57
|
-
run_callbacks :deliver do
|
|
58
|
-
Array.wrap(recipients).uniq.each do |recipient|
|
|
59
|
-
run_delivery(recipient, enqueue: false)
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def deliver_later(recipients)
|
|
65
|
-
validate!
|
|
66
|
-
|
|
67
|
-
run_callbacks :deliver do
|
|
68
|
-
Array.wrap(recipients).uniq.each do |recipient|
|
|
69
|
-
run_delivery(recipient, enqueue: true)
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def params
|
|
75
|
-
@params || {}
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def clear_recipient
|
|
79
|
-
self.recipient = nil
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
private
|
|
83
|
-
|
|
84
|
-
# Runs all delivery methods for a notification
|
|
85
|
-
def run_delivery(recipient, enqueue: true)
|
|
86
|
-
delivery_methods = self.class.delivery_methods.dup
|
|
87
|
-
|
|
88
|
-
self.recipient = recipient
|
|
89
|
-
|
|
90
|
-
# Run database delivery inline first if it exists so other methods have access to the record
|
|
91
|
-
if (index = delivery_methods.find_index { |m| m[:name] == :database })
|
|
92
|
-
delivery_method = delivery_methods.delete_at(index)
|
|
93
|
-
self.record = run_delivery_method(delivery_method, recipient: recipient, enqueue: false, record: nil)
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
delivery_methods.each do |delivery_method|
|
|
97
|
-
run_delivery_method(delivery_method, recipient: recipient, enqueue: enqueue, record: record)
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
# Actually runs an individual delivery
|
|
102
|
-
def run_delivery_method(delivery_method, recipient:, enqueue:, record:)
|
|
103
|
-
args = {
|
|
104
|
-
notification_class: self.class.name,
|
|
105
|
-
options: delivery_method[:options],
|
|
106
|
-
params: params,
|
|
107
|
-
recipient: recipient,
|
|
108
|
-
record: record
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
run_callbacks delivery_method[:name] do
|
|
112
|
-
method = delivery_method_for(delivery_method[:name], delivery_method[:options])
|
|
113
|
-
|
|
114
|
-
# If the queue is `nil`, ActiveJob will use a default queue name.
|
|
115
|
-
queue = delivery_method.dig(:options, :queue)
|
|
116
|
-
|
|
117
|
-
# Always perfrom later if a delay is present
|
|
118
|
-
if (delay = delivery_method.dig(:options, :delay))
|
|
119
|
-
# Dynamic delays with metho calls or
|
|
120
|
-
delay = send(delay) if delay.is_a? Symbol
|
|
121
|
-
|
|
122
|
-
method.set(wait: delay, queue: queue).perform_later(args)
|
|
123
|
-
elsif enqueue
|
|
124
|
-
method.set(queue: queue).perform_later(args)
|
|
125
|
-
else
|
|
126
|
-
method.perform_now(args)
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
def delivery_method_for(name, options)
|
|
132
|
-
if options[:class]
|
|
133
|
-
options[:class].constantize
|
|
134
|
-
else
|
|
135
|
-
"Noticed::DeliveryMethods::#{name.to_s.camelize}".constantize
|
|
136
|
-
end
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
def validate!
|
|
140
|
-
validate_params_present!
|
|
141
|
-
validate_options_of_delivery_methods!
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
# Validates that all params are present
|
|
145
|
-
def validate_params_present!
|
|
146
|
-
self.class.param_names.each do |param_name|
|
|
147
|
-
if params[param_name].nil?
|
|
148
|
-
raise ValidationError, "#{param_name} is missing."
|
|
149
|
-
end
|
|
150
|
-
end
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
def validate_options_of_delivery_methods!
|
|
154
|
-
delivery_methods.each do |delivery_method|
|
|
155
|
-
method = delivery_method_for(delivery_method[:name], delivery_method[:options])
|
|
156
|
-
method.validate!(delivery_method[:options])
|
|
157
|
-
end
|
|
158
|
-
end
|
|
159
|
-
end
|
|
160
|
-
end
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
module Noticed
|
|
2
|
-
module DeliveryMethods
|
|
3
|
-
class Base < Noticed.parent_class.constantize
|
|
4
|
-
extend ActiveModel::Callbacks
|
|
5
|
-
define_model_callbacks :deliver
|
|
6
|
-
|
|
7
|
-
class_attribute :option_names, instance_writer: false, default: []
|
|
8
|
-
|
|
9
|
-
attr_reader :notification, :options, :params, :recipient, :record
|
|
10
|
-
|
|
11
|
-
class << self
|
|
12
|
-
# Copy option names from parent
|
|
13
|
-
def inherited(base) # :nodoc:
|
|
14
|
-
base.option_names = option_names.dup
|
|
15
|
-
super
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def options(*names)
|
|
19
|
-
option_names.concat Array.wrap(names)
|
|
20
|
-
end
|
|
21
|
-
alias_method :option, :options
|
|
22
|
-
|
|
23
|
-
def validate!(delivery_method_options)
|
|
24
|
-
option_names.each do |option_name|
|
|
25
|
-
unless delivery_method_options.key? option_name
|
|
26
|
-
raise ValidationError, "option `#{option_name}` must be set for #{name}"
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def assign_args(args)
|
|
33
|
-
@notification = args.fetch(:notification_class).constantize.new(args[:params])
|
|
34
|
-
@options = args[:options] || {}
|
|
35
|
-
@params = args[:params]
|
|
36
|
-
@recipient = args[:recipient]
|
|
37
|
-
@record = args[:record]
|
|
38
|
-
|
|
39
|
-
# Make notification aware of database record and recipient during delivery
|
|
40
|
-
@notification.record = args[:record]
|
|
41
|
-
@notification.recipient = args[:recipient]
|
|
42
|
-
self
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
def perform(args)
|
|
46
|
-
assign_args(args)
|
|
47
|
-
|
|
48
|
-
return if (condition = @options[:if]) && !@notification.send(condition)
|
|
49
|
-
return if (condition = @options[:unless]) && @notification.send(condition)
|
|
50
|
-
|
|
51
|
-
run_callbacks :deliver do
|
|
52
|
-
deliver
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def deliver
|
|
57
|
-
raise NotImplementedError, "Delivery methods must implement a deliver method"
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
private
|
|
61
|
-
|
|
62
|
-
# Helper method for making POST requests from delivery methods
|
|
63
|
-
#
|
|
64
|
-
# Usage:
|
|
65
|
-
# post("http://example.com", basic_auth: {user:, pass:}, headers: {}, json: {}, form: {})
|
|
66
|
-
#
|
|
67
|
-
def post(url, args = {})
|
|
68
|
-
options ||= {}
|
|
69
|
-
basic_auth = args.delete(:basic_auth)
|
|
70
|
-
headers = args.delete(:headers)
|
|
71
|
-
|
|
72
|
-
request = HTTP
|
|
73
|
-
request = request.basic_auth(user: basic_auth[:user], pass: basic_auth[:pass]) if basic_auth
|
|
74
|
-
request = request.headers(headers) if headers
|
|
75
|
-
|
|
76
|
-
response = request.post(url, args)
|
|
77
|
-
|
|
78
|
-
if options[:debug]
|
|
79
|
-
Rails.logger.debug("POST #{url}")
|
|
80
|
-
Rails.logger.debug("Response: #{response.code}: #{response}")
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
if !options[:ignore_failure] && !response.status.success?
|
|
84
|
-
puts response.status
|
|
85
|
-
puts response.body
|
|
86
|
-
raise ResponseUnsuccessful.new(response)
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
response
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
end
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
module Noticed
|
|
2
|
-
module DeliveryMethods
|
|
3
|
-
class Database < Base
|
|
4
|
-
# Must return the database record
|
|
5
|
-
def deliver
|
|
6
|
-
recipient.send(association_name).create!(attributes)
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def self.validate!(options)
|
|
10
|
-
super
|
|
11
|
-
|
|
12
|
-
# Must be executed right away so the other deliveries can access the db record
|
|
13
|
-
raise ArgumentError, "database delivery cannot be delayed" if options.key?(:delay)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
private
|
|
17
|
-
|
|
18
|
-
def association_name
|
|
19
|
-
options[:association] || :notifications
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def attributes
|
|
23
|
-
if (method = options[:format])
|
|
24
|
-
notification.send(method)
|
|
25
|
-
else
|
|
26
|
-
{
|
|
27
|
-
type: notification.class.name,
|
|
28
|
-
params: notification.params
|
|
29
|
-
}
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
module Noticed
|
|
2
|
-
module DeliveryMethods
|
|
3
|
-
class Twilio < Base
|
|
4
|
-
def deliver
|
|
5
|
-
post(url, basic_auth: {user: account_sid, pass: auth_token}, form: 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
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
module Noticed
|
|
2
|
-
module DeliveryMethods
|
|
3
|
-
class Vonage < Base
|
|
4
|
-
def deliver
|
|
5
|
-
response = post("https://rest.nexmo.com/sms/json", json: format)
|
|
6
|
-
status = response.parse.dig("messages", 0, "status")
|
|
7
|
-
if !options[:ignore_failure] && status != "0"
|
|
8
|
-
raise ResponseUnsuccessful.new(response)
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
response
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
private
|
|
15
|
-
|
|
16
|
-
def format
|
|
17
|
-
if (method = options[:format])
|
|
18
|
-
notification.send(method)
|
|
19
|
-
else
|
|
20
|
-
{
|
|
21
|
-
api_key: credentials[:api_key],
|
|
22
|
-
api_secret: credentials[:api_secret],
|
|
23
|
-
from: notification.params[:from],
|
|
24
|
-
text: notification.params[:body],
|
|
25
|
-
to: notification.params[:to],
|
|
26
|
-
type: "unicode"
|
|
27
|
-
}
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def credentials
|
|
32
|
-
if (method = options[:credentials])
|
|
33
|
-
notification.send(method)
|
|
34
|
-
else
|
|
35
|
-
Rails.application.credentials.vonage
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
end
|