noticed 2.7.0 → 3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 409bc0222c2ca902d1cf99a7015cfe24ca3f0c9b1e765ff97af2ae327ce8598f
4
- data.tar.gz: 022f80d4007f6026d874cf1ba68898028e294989d84204db476c53864ffd43aa
3
+ metadata.gz: 10b19f81b81ae5471493e73857b55fbb1086545cca11f58cd40935597a39b5a1
4
+ data.tar.gz: 6a3e5502295f8152f0d05b4150a7c272ec48595adb986c81112aea3d09fce891
5
5
  SHA512:
6
- metadata.gz: 7229791c39a6edb4ae4abff40a8bbbb6f727167235714b1b8ecaab2769ab9770771059ddccbe1f8f8372ac233ad2cb6ca55679dadd328d19795934305f8f35ca
7
- data.tar.gz: ddf2c02c68f2310845067f73596dbc504083e94be646acf5ce7e98f2c67e0a98d794672358abc019d4565302b5140bbd036959f13ab3e5590f904e65d5715c29
6
+ metadata.gz: ca14522f41fbbc56b6764fb818a7d283a49d94e31d1dfcddbe8042429612d6a7f41e42df53867bfc4175fd66fc7aa5079c116db3b62707b5f87ea884c5dd5a5f
7
+ data.tar.gz: c0b7e78c47afa9df9a642f18717aa15c9c37a1823110b05c9c6b82987f6de0791ca82437f4b7e552ced05c38274b16d90478b9af29999791c2bc66ddd29fe37b
data/README.md CHANGED
@@ -4,9 +4,6 @@
4
4
 
5
5
  [![Build Status](https://github.com/excid3/noticed/workflows/Tests/badge.svg)](https://github.com/excid3/noticed/actions) [![Gem Version](https://badge.fury.io/rb/noticed.svg)](https://badge.fury.io/rb/noticed)
6
6
 
7
- > [!IMPORTANT]
8
- > **⚠️ Upgrading from V1? Read the [Upgrade Guide](https://github.com/excid3/noticed/blob/main/UPGRADE.md)!**
9
-
10
7
  Noticed is a gem that allows your application to send notifications of varying types, over various mediums, to various recipients. Be it a Slack notification to your own team when some internal event occurs or a notification to your user, sent as a text message, email, and real-time UI element in the browser, Noticed supports all of the above (at the same time)!
11
8
 
12
9
  Noticed implements two top-level types of delivery methods:
@@ -30,6 +27,7 @@ Bulk deliveries are typically used to push notifications to other platforms wher
30
27
  Delivery methods we officially support:
31
28
 
32
29
  * [ActionCable](docs/delivery_methods/action_cable.md)
30
+ * [Action Push Native](docs/delivery_methods/action_push_native.md)
33
31
  * [Apple Push Notification Service](docs/delivery_methods/ios.md)
34
32
  * [Email](docs/delivery_methods/email.md)
35
33
  * [Firebase Cloud Messaging](docs/delivery_methods/fcm.md) (iOS, Android, and web clients)
@@ -493,6 +491,27 @@ module MyApp
493
491
  end
494
492
  ```
495
493
 
494
+ #### Tip: Custom properties on a notification per recipient
495
+
496
+ In order to have recipient-specific settings on the notification, override the `recipient_attributes_for(recipient)` method in your notifier:
497
+
498
+ ```ruby
499
+ class CommentNotifier < ApplicationNotifier
500
+ #...
501
+ def recipient_attributes_for(recipient)
502
+ data = super
503
+ data[:priority] = if recipient.participant?
504
+ "high"
505
+ else
506
+ "low"
507
+ end
508
+ data
509
+ end
510
+ end
511
+ ```
512
+
513
+ Assuming you have a `priority` column in the `noticed_notifications` table, it will be set to the value from the hash here. The default properties of the hash are `recipient_type` and `recipient_id`.
514
+
496
515
  ## ✅ Best Practices
497
516
 
498
517
  ### Renaming Notifiers
@@ -573,7 +592,9 @@ You can mix and match the options and delivery methods to suit your application
573
592
 
574
593
  If you want to build your own delivery method to deliver notifications to a specific service or medium that Noticed doesn’t (or doesn’t _yet_) support, you’re welcome to do so! To generate a custom delivery method, simply run
575
594
 
576
- `rails generate noticed:delivery_method Discord`
595
+ ```bash
596
+ rails generate noticed:delivery_method Discord
597
+ ```
577
598
 
578
599
  This will generate a new `ApplicationDeliveryMethod` and `DeliveryMethods::Discord` class inside the `app/notifiers/delivery_methods` folder, which can be used to deliver notifications to Discord.
579
600
 
@@ -586,7 +607,6 @@ class DeliveryMethods::Discord < ApplicationDeliveryMethod
586
607
  # Logic for sending the notification
587
608
  end
588
609
  end
589
-
590
610
  ```
591
611
 
592
612
  You can use the custom delivery method thus created by adding a `deliver_by` line with a unique name and `class` option in your notification class.
@@ -597,6 +617,12 @@ class MyNotifier < Noticed::Event
597
617
  end
598
618
  ```
599
619
 
620
+ You can also generate bulk delivery methods with the `--bulk` flag:
621
+
622
+ ```bash
623
+ rails generate noticed:delivery_method Discord --bulk
624
+ ```
625
+
600
626
  <details>
601
627
  <summary>Turbo Stream Custom Delivery Method Example</summary>
602
628
 
@@ -118,9 +118,9 @@ module Noticed
118
118
  def evaluate_recipients
119
119
  return unless _recipients
120
120
 
121
- if _recipients.respond_to?(:call)
121
+ if _recipients.respond_to?(:call, true)
122
122
  instance_exec(&_recipients)
123
- elsif _recipients.is_a?(Symbol) && respond_to?(_recipients)
123
+ elsif _recipients.is_a?(Symbol) && respond_to?(_recipients, true)
124
124
  send(_recipients)
125
125
  end
126
126
  end
@@ -11,9 +11,16 @@ module Noticed
11
11
 
12
12
  desc "Generates a class for a custom delivery method with the given NAME."
13
13
 
14
+ class_option :bulk, desc: "Generate as a bulk delivery method", type: :boolean, default: false
15
+
14
16
  def generate_notification
15
- template "application_delivery_method.rb", "app/notifiers/application_delivery_method.rb"
16
- template "delivery_method.rb", "app/notifiers/delivery_methods/#{singular_name}.rb"
17
+ if options[:bulk]
18
+ template "application_bulk_delivery_method.rb", "app/notifiers/application_bulk_delivery_method.rb"
19
+ template "bulk_delivery_method.rb", "app/notifiers/bulk_delivery_methods/#{singular_name}.rb"
20
+ else
21
+ template "application_delivery_method.rb", "app/notifiers/application_delivery_method.rb"
22
+ template "delivery_method.rb", "app/notifiers/delivery_methods/#{singular_name}.rb"
23
+ end
17
24
  end
18
25
  end
19
26
  end
@@ -0,0 +1,2 @@
1
+ class ApplicationBulkDeliveryMethod < Noticed::BulkDeliveryMethod
2
+ end
@@ -0,0 +1,14 @@
1
+ class BulkDeliveryMethods::<%= class_name %> < ApplicationBulkDeliveryMethod
2
+ # To use this delivery method, specify the class option in your notifier.
3
+ #
4
+ # class MyNotifer < ApplicationNotifier
5
+ # bulk_deliver_by :<%= file_path %>, class: "<%= class_name %>"
6
+ # end
7
+
8
+ # Specify required options for the deliver_by config block
9
+ # required_options :foo, :bar
10
+
11
+ def deliver
12
+ # Logic for sending the notification
13
+ end
14
+ end
@@ -1,6 +1,12 @@
1
1
  class DeliveryMethods::<%= class_name %> < ApplicationDeliveryMethod
2
- # Specify the config options your delivery method requires in its config block
3
- required_options # :foo, :bar
2
+ # To use this delivery method, specify the class option in your notifier.
3
+ #
4
+ # class MyNotifer < ApplicationNotifier
5
+ # deliver_by :<%= file_path %>, class: "<%= class_name %>"
6
+ # end
7
+
8
+ # Specify required options for the deliver_by config block
9
+ # required_options :foo, :bar
4
10
 
5
11
  def deliver
6
12
  # Logic for sending the notification
@@ -21,4 +21,10 @@ class <%= class_name %>Notifier < ApplicationNotifier
21
21
  # Add required params
22
22
  #
23
23
  # required_param :message
24
+
25
+ # Compute recipients without having to pass them in
26
+ #
27
+ # recipients do
28
+ # params[:record].thread.all_authors
29
+ # end
24
30
  end
@@ -29,6 +29,8 @@ module Noticed
29
29
  request.body = json.to_json
30
30
  elsif (form = args.delete(:form))
31
31
  request.form_data = form
32
+ elsif (body = args.delete(:body))
33
+ request.body = body
32
34
  end
33
35
 
34
36
  logger.debug("POST #{url}")
@@ -4,6 +4,7 @@ module Noticed
4
4
  include RequiredOptions
5
5
 
6
6
  extend ActiveModel::Callbacks
7
+
7
8
  define_model_callbacks :deliver
8
9
 
9
10
  class_attribute :logger, default: Rails.logger
@@ -10,7 +10,8 @@ module Noticed
10
10
  basic_auth: evaluate_option(:basic_auth),
11
11
  headers: evaluate_option(:headers),
12
12
  json: evaluate_option(:json),
13
- form: evaluate_option(:form)
13
+ form: evaluate_option(:form),
14
+ body: evaluate_option(:body)
14
15
  )
15
16
  end
16
17
  end
data/lib/noticed/coder.rb CHANGED
@@ -11,7 +11,11 @@ module Noticed
11
11
 
12
12
  def self.dump(data)
13
13
  return if data.nil?
14
- ActiveJob::Arguments.send(:serialize_argument, data)
14
+ if ActiveJob::Arguments.respond_to?(:serialize_argument, true)
15
+ ActiveJob::Arguments.send(:serialize_argument, data)
16
+ else
17
+ ActiveJob::Arguments.serialize(data)
18
+ end
15
19
  end
16
20
  end
17
21
  end
@@ -4,6 +4,7 @@ module Noticed
4
4
  include RequiredOptions
5
5
 
6
6
  extend ActiveModel::Callbacks
7
+
7
8
  define_model_callbacks :deliver
8
9
 
9
10
  class_attribute :logger, default: Rails.logger
@@ -0,0 +1,24 @@
1
+ module Noticed
2
+ module DeliveryMethods
3
+ class ActionPushNative < DeliveryMethod
4
+ required_options :devices, :format
5
+
6
+ def deliver
7
+ notification = (!!evaluate_option(:silent)) ? notification_class.silent : notification_class
8
+
9
+ notification
10
+ .with_apple(evaluate_option(:with_apple))
11
+ .with_google(evaluate_option(:with_google))
12
+ .with_data(evaluate_option(:with_data))
13
+ .new(**evaluate_option(:format))
14
+ .deliver_later_to(evaluate_option(:devices))
15
+ end
16
+
17
+ def notification_class
18
+ fetch_constant(:class) || ApplicationPushNotification
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ ActiveSupport.run_load_hooks :noticed_delivery_methods_action_push_native, Noticed::DeliveryMethods::ActionPushNative
@@ -7,7 +7,9 @@ module Noticed
7
7
  mailer = fetch_constant(:mailer)
8
8
  email = evaluate_option(:method)
9
9
  args = evaluate_option(:args) || []
10
- mail = mailer.with(params).public_send(email, *args)
10
+ kwargs = evaluate_option(:kwargs) || {}
11
+ mail = mailer.with(params).public_send(email, *args, **kwargs)
12
+
11
13
  (!!evaluate_option(:enqueue)) ? mail.deliver_later : mail.deliver_now
12
14
  end
13
15
 
@@ -18,6 +18,8 @@ module Noticed
18
18
  rescue Noticed::ResponseUnsuccessful => exception
19
19
  if bad_token?(exception.response) && config[:invalid_token]
20
20
  notification.instance_exec(device_token, &config[:invalid_token])
21
+ elsif config[:error_handler]
22
+ notification.instance_exec(exception.response, &config[:error_handler])
21
23
  else
22
24
  raise
23
25
  end
@@ -9,7 +9,8 @@ module Noticed
9
9
  basic_auth: evaluate_option(:basic_auth),
10
10
  headers: evaluate_option(:headers),
11
11
  json: evaluate_option(:json),
12
- form: evaluate_option(:form)
12
+ form: evaluate_option(:form),
13
+ body: evaluate_option(:body)
13
14
  )
14
15
  end
15
16
  end
@@ -2,6 +2,10 @@ module Noticed
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace Noticed
4
4
 
5
+ initializer "noticed.deprecator" do |app|
6
+ app.deprecators[:noticed] = Noticed.deprecator if app.respond_to?(:deprecators)
7
+ end
8
+
5
9
  initializer "noticed.has_notifications" do
6
10
  ActiveSupport.on_load(:active_record) do
7
11
  include Noticed::HasNotifications
@@ -1,3 +1,3 @@
1
1
  module Noticed
2
- VERSION = "2.7.0"
2
+ VERSION = "3.0.0"
3
3
  end
data/lib/noticed.rb CHANGED
@@ -1,53 +1,34 @@
1
1
  require "noticed/version"
2
2
  require "noticed/engine"
3
3
 
4
+ require "zeitwerk"
5
+
6
+ loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
7
+ loader.ignore("#{__dir__}/generators")
8
+ loader.do_not_eager_load("#{__dir__}/noticed/bulk_delivery_methods")
9
+ loader.do_not_eager_load("#{__dir__}/noticed/delivery_methods")
10
+ loader.do_not_eager_load("#{__dir__}/noticed/notification_channel.rb")
11
+ loader.setup
12
+
4
13
  module Noticed
5
14
  include ActiveSupport::Deprecation::DeprecatedConstantAccessor
6
15
 
7
16
  def self.deprecator # :nodoc:
8
- @deprecator ||= ActiveSupport::Deprecation.new
17
+ @deprecator ||= ActiveSupport::Deprecation.new("3.0", "Noticed")
9
18
  end
10
19
 
11
20
  deprecate_constant :Base, "Noticed::Event", deprecator: deprecator
12
21
 
13
- autoload :ApiClient, "noticed/api_client"
14
- autoload :BulkDeliveryMethod, "noticed/bulk_delivery_method"
15
- autoload :Coder, "noticed/coder"
16
- autoload :DeliveryMethod, "noticed/delivery_method"
17
- autoload :HasNotifications, "noticed/has_notifications"
18
- autoload :NotificationChannel, "noticed/notification_channel"
19
- autoload :RequiredOptions, "noticed/required_options"
20
- autoload :Translation, "noticed/translation"
21
-
22
- module BulkDeliveryMethods
23
- autoload :Bluesky, "noticed/bulk_delivery_methods/bluesky"
24
- autoload :Discord, "noticed/bulk_delivery_methods/discord"
25
- autoload :Slack, "noticed/bulk_delivery_methods/slack"
26
- autoload :Test, "noticed/bulk_delivery_methods/test"
27
- autoload :Webhook, "noticed/bulk_delivery_methods/webhook"
28
- end
29
-
30
22
  module DeliveryMethods
31
23
  include ActiveSupport::Deprecation::DeprecatedConstantAccessor
32
- deprecate_constant :Base, "Noticed::DeliveryMethod", deprecator: Noticed.deprecator
33
24
 
34
- autoload :ActionCable, "noticed/delivery_methods/action_cable"
35
- autoload :Email, "noticed/delivery_methods/email"
36
- autoload :Fcm, "noticed/delivery_methods/fcm"
37
- autoload :Ios, "noticed/delivery_methods/ios"
38
- autoload :MicrosoftTeams, "noticed/delivery_methods/microsoft_teams"
39
- autoload :Slack, "noticed/delivery_methods/slack"
40
- autoload :Test, "noticed/delivery_methods/test"
41
- autoload :TwilioMessaging, "noticed/delivery_methods/twilio_messaging"
42
- autoload :VonageSms, "noticed/delivery_methods/vonage_sms"
43
- autoload :Webhook, "noticed/delivery_methods/webhook"
25
+ deprecate_constant :Base, "Noticed::DeliveryMethod", deprecator: Noticed.deprecator
44
26
  end
45
27
 
46
28
  mattr_accessor :parent_class
47
29
  @@parent_class = "Noticed::ApplicationJob"
48
30
 
49
- class ValidationError < StandardError
50
- end
31
+ class ValidationError < StandardError; end
51
32
 
52
33
  class ResponseUnsuccessful < StandardError
53
34
  attr_reader :response
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: noticed
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
@@ -50,8 +50,10 @@ files:
50
50
  - lib/generators/noticed/install_generator.rb
51
51
  - lib/generators/noticed/notifier_generator.rb
52
52
  - lib/generators/noticed/templates/README
53
+ - lib/generators/noticed/templates/application_bulk_delivery_method.rb.tt
53
54
  - lib/generators/noticed/templates/application_delivery_method.rb.tt
54
55
  - lib/generators/noticed/templates/application_notifier.rb.tt
56
+ - lib/generators/noticed/templates/bulk_delivery_method.rb.tt
55
57
  - lib/generators/noticed/templates/delivery_method.rb.tt
56
58
  - lib/generators/noticed/templates/notifier.rb.tt
57
59
  - lib/noticed.rb
@@ -65,6 +67,7 @@ files:
65
67
  - lib/noticed/coder.rb
66
68
  - lib/noticed/delivery_method.rb
67
69
  - lib/noticed/delivery_methods/action_cable.rb
70
+ - lib/noticed/delivery_methods/action_push_native.rb
68
71
  - lib/noticed/delivery_methods/discord.rb
69
72
  - lib/noticed/delivery_methods/email.rb
70
73
  - lib/noticed/delivery_methods/fcm.rb
@@ -99,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
102
  - !ruby/object:Gem::Version
100
103
  version: '0'
101
104
  requirements: []
102
- rubygems_version: 3.6.9
105
+ rubygems_version: 4.0.3
103
106
  specification_version: 4
104
107
  summary: Notifications for Ruby on Rails applications
105
108
  test_files: []