noticed 1.6.3 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +269 -237
  3. data/app/jobs/noticed/application_job.rb +9 -0
  4. data/app/jobs/noticed/event_job.rb +19 -0
  5. data/app/models/concerns/noticed/deliverable.rb +115 -0
  6. data/app/models/concerns/noticed/notification_methods.rb +17 -0
  7. data/app/models/concerns/noticed/readable.rb +78 -0
  8. data/app/models/noticed/application_record.rb +6 -0
  9. data/app/models/noticed/deliverable/deliver_by.rb +43 -0
  10. data/app/models/noticed/event.rb +15 -0
  11. data/app/models/noticed/notification.rb +14 -0
  12. data/db/migrate/20231215190233_create_noticed_tables.rb +25 -0
  13. data/lib/generators/noticed/delivery_method_generator.rb +1 -1
  14. data/lib/generators/noticed/install_generator.rb +19 -0
  15. data/lib/generators/noticed/{notification_generator.rb → notifier_generator.rb} +2 -2
  16. data/lib/generators/noticed/templates/README +5 -4
  17. data/lib/generators/noticed/templates/delivery_method.rb.tt +4 -8
  18. data/lib/generators/noticed/templates/notifier.rb.tt +24 -0
  19. data/lib/noticed/api_client.rb +44 -0
  20. data/lib/noticed/bulk_delivery_method.rb +46 -0
  21. data/lib/noticed/bulk_delivery_methods/discord.rb +11 -0
  22. data/lib/noticed/bulk_delivery_methods/slack.rb +17 -0
  23. data/lib/noticed/bulk_delivery_methods/webhook.rb +18 -0
  24. data/lib/noticed/coder.rb +2 -0
  25. data/lib/noticed/delivery_method.rb +51 -0
  26. data/lib/noticed/delivery_methods/action_cable.rb +7 -39
  27. data/lib/noticed/delivery_methods/discord.rb +11 -0
  28. data/lib/noticed/delivery_methods/email.rb +13 -45
  29. data/lib/noticed/delivery_methods/fcm.rb +23 -64
  30. data/lib/noticed/delivery_methods/ios.rb +25 -112
  31. data/lib/noticed/delivery_methods/microsoft_teams.rb +5 -22
  32. data/lib/noticed/delivery_methods/slack.rb +6 -16
  33. data/lib/noticed/delivery_methods/test.rb +2 -12
  34. data/lib/noticed/delivery_methods/twilio_messaging.rb +37 -0
  35. data/lib/noticed/delivery_methods/vonage_sms.rb +20 -0
  36. data/lib/noticed/delivery_methods/webhook.rb +17 -0
  37. data/lib/noticed/engine.rb +1 -9
  38. data/lib/noticed/required_options.rb +21 -0
  39. data/lib/noticed/translation.rb +7 -3
  40. data/lib/noticed/version.rb +1 -1
  41. data/lib/noticed.rb +30 -15
  42. metadata +29 -40
  43. data/lib/generators/noticed/model/base_generator.rb +0 -47
  44. data/lib/generators/noticed/model/mysql_generator.rb +0 -18
  45. data/lib/generators/noticed/model/postgresql_generator.rb +0 -18
  46. data/lib/generators/noticed/model/sqlite3_generator.rb +0 -18
  47. data/lib/generators/noticed/model_generator.rb +0 -63
  48. data/lib/generators/noticed/templates/notification.rb.tt +0 -27
  49. data/lib/noticed/base.rb +0 -160
  50. data/lib/noticed/delivery_methods/base.rb +0 -95
  51. data/lib/noticed/delivery_methods/database.rb +0 -34
  52. data/lib/noticed/delivery_methods/twilio.rb +0 -51
  53. data/lib/noticed/delivery_methods/vonage.rb +0 -40
  54. data/lib/noticed/has_notifications.rb +0 -49
  55. data/lib/noticed/model.rb +0 -85
  56. data/lib/noticed/notification_channel.rb +0 -15
  57. data/lib/noticed/text_coder.rb +0 -16
  58. data/lib/rails_6_polyfills/actioncable/test_adapter.rb +0 -70
  59. data/lib/rails_6_polyfills/actioncable/test_helper.rb +0 -143
  60. data/lib/rails_6_polyfills/activejob/serializers.rb +0 -240
  61. data/lib/rails_6_polyfills/base.rb +0 -18
  62. data/lib/tasks/noticed_tasks.rake +0 -4
@@ -1,3 +1,3 @@
1
1
  module Noticed
2
- VERSION = "1.6.3"
2
+ VERSION = "2.0.1"
3
3
  end
data/lib/noticed.rb CHANGED
@@ -1,41 +1,56 @@
1
- require "active_job/arguments"
2
- require "http"
1
+ require "noticed/version"
3
2
  require "noticed/engine"
4
3
 
5
4
  module Noticed
6
- autoload :Base, "noticed/base"
5
+ include ActiveSupport::Deprecation::DeprecatedConstantAccessor
6
+
7
+ def self.deprecator # :nodoc:
8
+ @deprecator ||= ActiveSupport::Deprecation.new
9
+ end
10
+
11
+ deprecate_constant :Base, "Noticed::Event", deprecator: deprecator
12
+
13
+ autoload :ApiClient, "noticed/api_client"
14
+ autoload :BulkDeliveryMethod, "noticed/bulk_delivery_method"
7
15
  autoload :Coder, "noticed/coder"
8
- autoload :HasNotifications, "noticed/has_notifications"
9
- autoload :Model, "noticed/model"
10
- autoload :TextCoder, "noticed/text_coder"
16
+ autoload :DeliveryMethod, "noticed/delivery_method"
17
+ autoload :RequiredOptions, "noticed/required_options"
11
18
  autoload :Translation, "noticed/translation"
12
- autoload :NotificationChannel, "noticed/notification_channel"
19
+
20
+ module BulkDeliveryMethods
21
+ autoload :Discord, "noticed/bulk_delivery_methods/discord"
22
+ autoload :Slack, "noticed/bulk_delivery_methods/slack"
23
+ autoload :Webhook, "noticed/bulk_delivery_methods/webhook"
24
+ end
13
25
 
14
26
  module DeliveryMethods
27
+ include ActiveSupport::Deprecation::DeprecatedConstantAccessor
28
+ deprecate_constant :Base, "Noticed::DeliveryMethod", deprecator: Noticed.deprecator
29
+
15
30
  autoload :ActionCable, "noticed/delivery_methods/action_cable"
16
- autoload :Base, "noticed/delivery_methods/base"
17
- autoload :Database, "noticed/delivery_methods/database"
18
31
  autoload :Email, "noticed/delivery_methods/email"
19
32
  autoload :Fcm, "noticed/delivery_methods/fcm"
20
33
  autoload :Ios, "noticed/delivery_methods/ios"
21
34
  autoload :MicrosoftTeams, "noticed/delivery_methods/microsoft_teams"
22
35
  autoload :Slack, "noticed/delivery_methods/slack"
23
36
  autoload :Test, "noticed/delivery_methods/test"
24
- autoload :Twilio, "noticed/delivery_methods/twilio"
25
- autoload :Vonage, "noticed/delivery_methods/vonage"
37
+ autoload :TwilioMessaging, "noticed/delivery_methods/twilio_messaging"
38
+ autoload :VonageSms, "noticed/delivery_methods/vonage_sms"
39
+ autoload :Webhook, "noticed/delivery_methods/webhook"
26
40
  end
27
41
 
28
- mattr_accessor :parent_class
29
- @@parent_class = "ApplicationJob"
30
-
31
42
  class ValidationError < StandardError
32
43
  end
33
44
 
34
45
  class ResponseUnsuccessful < StandardError
35
46
  attr_reader :response
36
47
 
37
- def initialize(response)
48
+ def initialize(response, url, args)
38
49
  @response = response
50
+ @url = url
51
+ @args = args
52
+
53
+ super("POST request to #{url} returned #{response.code} response:\n#{response.body.inspect}")
39
54
  end
40
55
  end
41
56
  end
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: 1.6.3
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-11 00:00:00.000000000 Z
11
+ date: 2024-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,28 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 5.2.0
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: 5.2.0
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
26
+ version: 6.1.0
41
27
  description: Database, browser, realtime ActionCable, Email, SMS, Slack notifications,
42
28
  and more for Rails apps
43
29
  email:
@@ -49,42 +35,45 @@ files:
49
35
  - MIT-LICENSE
50
36
  - README.md
51
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
52
48
  - lib/generators/noticed/delivery_method_generator.rb
53
- - lib/generators/noticed/model/base_generator.rb
54
- - lib/generators/noticed/model/mysql_generator.rb
55
- - lib/generators/noticed/model/postgresql_generator.rb
56
- - lib/generators/noticed/model/sqlite3_generator.rb
57
- - lib/generators/noticed/model_generator.rb
58
- - lib/generators/noticed/notification_generator.rb
49
+ - lib/generators/noticed/install_generator.rb
50
+ - lib/generators/noticed/notifier_generator.rb
59
51
  - lib/generators/noticed/templates/README
60
52
  - lib/generators/noticed/templates/delivery_method.rb.tt
61
- - lib/generators/noticed/templates/notification.rb.tt
53
+ - lib/generators/noticed/templates/notifier.rb.tt
62
54
  - lib/noticed.rb
63
- - lib/noticed/base.rb
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
64
60
  - lib/noticed/coder.rb
61
+ - lib/noticed/delivery_method.rb
65
62
  - lib/noticed/delivery_methods/action_cable.rb
66
- - lib/noticed/delivery_methods/base.rb
67
- - lib/noticed/delivery_methods/database.rb
63
+ - lib/noticed/delivery_methods/discord.rb
68
64
  - lib/noticed/delivery_methods/email.rb
69
65
  - lib/noticed/delivery_methods/fcm.rb
70
66
  - lib/noticed/delivery_methods/ios.rb
71
67
  - lib/noticed/delivery_methods/microsoft_teams.rb
72
68
  - lib/noticed/delivery_methods/slack.rb
73
69
  - lib/noticed/delivery_methods/test.rb
74
- - lib/noticed/delivery_methods/twilio.rb
75
- - lib/noticed/delivery_methods/vonage.rb
70
+ - lib/noticed/delivery_methods/twilio_messaging.rb
71
+ - lib/noticed/delivery_methods/vonage_sms.rb
72
+ - lib/noticed/delivery_methods/webhook.rb
76
73
  - lib/noticed/engine.rb
77
- - lib/noticed/has_notifications.rb
78
- - lib/noticed/model.rb
79
- - lib/noticed/notification_channel.rb
80
- - lib/noticed/text_coder.rb
74
+ - lib/noticed/required_options.rb
81
75
  - lib/noticed/translation.rb
82
76
  - lib/noticed/version.rb
83
- - lib/rails_6_polyfills/actioncable/test_adapter.rb
84
- - lib/rails_6_polyfills/actioncable/test_helper.rb
85
- - lib/rails_6_polyfills/activejob/serializers.rb
86
- - lib/rails_6_polyfills/base.rb
87
- - lib/tasks/noticed_tasks.rake
88
77
  homepage: https://github.com/excid3/noticed
89
78
  licenses:
90
79
  - MIT
@@ -104,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
93
  - !ruby/object:Gem::Version
105
94
  version: '0'
106
95
  requirements: []
107
- rubygems_version: 3.4.12
96
+ rubygems_version: 3.5.3
108
97
  signing_key:
109
98
  specification_version: 4
110
99
  summary: Notifications for Ruby on Rails applications
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rails/generators/named_base"
4
-
5
- module Noticed
6
- module Generators
7
- module Model
8
- class BaseGenerator < Rails::Generators::NamedBase
9
- include Rails::Generators::ResourceHelpers
10
-
11
- source_root File.expand_path("../templates", __FILE__)
12
-
13
- desc "Generates a Notification model for storing notifications."
14
-
15
- argument :name, type: :string, default: "Notification", banner: "Notification"
16
- argument :attributes, type: :array, default: [], banner: "field:type field:type"
17
-
18
- def generate_notification
19
- generate :model, name, "recipient:references{polymorphic}", "type", "params:#{json_column_type}", "read_at:datetime:index", *attributes
20
- end
21
-
22
- def add_noticed_model
23
- inject_into_class model_path, class_name, " include Noticed::Model\n"
24
- end
25
-
26
- def add_not_nullable
27
- migration_path = Dir.glob(Rails.root.join("db/migrate/*")).max_by { |f| File.mtime(f) }
28
-
29
- # Force is required because null: false already exists in the file and Thor isn't smart enough to tell the difference
30
- insert_into_file migration_path, after: "t.string :type", force: true do
31
- ", null: false"
32
- end
33
- end
34
-
35
- def done
36
- readme "README" if behavior == :invoke
37
- end
38
-
39
- private
40
-
41
- def model_path
42
- @model_path ||= File.join("app", "models", "#{file_path}.rb")
43
- end
44
- end
45
- end
46
- end
47
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rails/generators/named_base"
4
- require_relative "base_generator"
5
-
6
- module Noticed
7
- module Generators
8
- module Model
9
- class MysqlGenerator < BaseGenerator
10
- private
11
-
12
- def json_column_type
13
- "json"
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rails/generators/named_base"
4
- require_relative "base_generator"
5
-
6
- module Noticed
7
- module Generators
8
- module Model
9
- class PostgresqlGenerator < BaseGenerator
10
- private
11
-
12
- def json_column_type
13
- "jsonb"
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rails/generators/named_base"
4
- require_relative "base_generator"
5
-
6
- module Noticed
7
- module Generators
8
- module Model
9
- class Sqlite3Generator < BaseGenerator
10
- private
11
-
12
- def json_column_type
13
- "json"
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -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,95 +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, :logger
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
- # Set the default logger
40
- @logger = @options.fetch(:logger, Rails.logger)
41
-
42
- # Make notification aware of database record and recipient during delivery
43
- @notification.record = args[:record]
44
- @notification.recipient = args[:recipient]
45
- self
46
- end
47
-
48
- def perform(args)
49
- assign_args(args)
50
-
51
- return if (condition = @options[:if]) && !@notification.send(condition)
52
- return if (condition = @options[:unless]) && @notification.send(condition)
53
-
54
- run_callbacks :deliver do
55
- deliver
56
- end
57
- end
58
-
59
- def deliver
60
- raise NotImplementedError, "Delivery methods must implement a deliver method"
61
- end
62
-
63
- private
64
-
65
- # Helper method for making POST requests from delivery methods
66
- #
67
- # Usage:
68
- # post("http://example.com", basic_auth: {user:, pass:}, headers: {}, json: {}, form: {})
69
- #
70
- def post(url, args = {})
71
- basic_auth = args.delete(:basic_auth)
72
- headers = args.delete(:headers)
73
-
74
- request = HTTP
75
- request = request.basic_auth(user: basic_auth[:user], pass: basic_auth[:pass]) if basic_auth
76
- request = request.headers(headers) if headers
77
-
78
- response = request.post(url, args)
79
-
80
- if options[:debug]
81
- logger.debug("POST #{url}")
82
- logger.debug("Response: #{response.code}: #{response}")
83
- end
84
-
85
- if !options[:ignore_failure] && !response.status.success?
86
- puts response.status
87
- puts response.body
88
- raise ResponseUnsuccessful.new(response)
89
- end
90
-
91
- response
92
- end
93
- end
94
- end
95
- 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