noticed 3.1.0 → 3.2.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: 5a62b2258520a1d287a6ab2ff9b73288e37ebd7d4b18994c0b13bcfb591e9440
4
- data.tar.gz: ecf9a251b05942f6b32c3730b923d59e16e4eb9de169e361b1ce0cb2989750f1
3
+ metadata.gz: fc2c6ab467bf073d0c2a6a12ffc732e954fcd54d3f0099610453cc392029663f
4
+ data.tar.gz: 3d2aa10edb60dd0fe9305e17a1900085cac0f11fcd6a7c047197f39ffa14c5e7
5
5
  SHA512:
6
- metadata.gz: 75edf989bdb4bc2fd7ee445f2852107fdfd629696affeae51d7e3d407ebdd97669b5d11bf8bbc79d92c4dc22dfde7bddcdd528c6f52c0230e1b169ef716d95cb
7
- data.tar.gz: d34cdd0a3e905188d6efe9852b1b199148e9b4e02e0677d446fbf48ab94fb98d6bc87bf9b7917280649b6182f5e7ef9e9d64af0cd43ef1db4ea45b23d4517225
6
+ metadata.gz: bfc027430d08fdfd7c42a10cac55a68b6f615e4a3a925f8e65dae39af4c6d324a50732c470c71b50f7ad809b27c0359a7978da19c14f3a4314fc946ab7ec8698
7
+ data.tar.gz: db2689fd76253d9af75f2e9db3fa09178e49bd047dfb5bc8d71d68c25abeedd0a0bf45d239efcf8a816b93aae34b84885fc87b98133a636e361061e9edd2499a
data/README.md CHANGED
@@ -76,6 +76,20 @@ To start, generate a Notifier:
76
76
  rails generate noticed:notifier NewCommentNotifier
77
77
  ```
78
78
 
79
+ The generator also creates `app/notifiers/application_notifier.rb`, unless `ApplicationNotifier` is already defined (for example, by an engine). To inherit from another class instead, pass `--parent`:
80
+
81
+ ```bash
82
+ rails generate noticed:notifier NewCommentNotifier --parent BaseNotifier
83
+ ```
84
+
85
+ To change the parent for every generated notifier, set it in `config/application.rb`:
86
+
87
+ ```ruby
88
+ config.generators do |g|
89
+ g.notifier parent: "BaseNotifier"
90
+ end
91
+ ```
92
+
79
93
  ### Usage Contents
80
94
  - [Notifier Objects](#notifier-objects)
81
95
  - [Delivery Method Configuration](#delivery-method-configuration)
@@ -612,7 +626,7 @@ If you want to build your own delivery method to deliver notifications to a spec
612
626
  rails generate noticed:delivery_method Discord
613
627
  ```
614
628
 
615
- 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.
629
+ This will generate a new `DeliveryMethods::Discord` class inside the `app/notifiers/delivery_methods` folder, which can be used to deliver notifications to Discord, plus an `ApplicationDeliveryMethod` if your app doesn’t already define one.
616
630
 
617
631
  ```ruby
618
632
  class DeliveryMethods::Discord < ApplicationDeliveryMethod
@@ -15,13 +15,22 @@ module Noticed
15
15
 
16
16
  def generate_notification
17
17
  if options[:bulk]
18
- template "application_bulk_delivery_method.rb", "app/notifiers/application_bulk_delivery_method.rb"
18
+ generate_abstract_class "ApplicationBulkDeliveryMethod"
19
19
  template "bulk_delivery_method.rb", "app/notifiers/bulk_delivery_methods/#{singular_name}.rb"
20
20
  else
21
- template "application_delivery_method.rb", "app/notifiers/application_delivery_method.rb"
21
+ generate_abstract_class "ApplicationDeliveryMethod"
22
22
  template "delivery_method.rb", "app/notifiers/delivery_methods/#{singular_name}.rb"
23
23
  end
24
24
  end
25
+
26
+ private
27
+
28
+ # The abstract class may be defined elsewhere, such as in an engine
29
+ def generate_abstract_class(name)
30
+ path = "app/notifiers/#{name.underscore}.rb"
31
+ return if name.safe_constantize || File.exist?(File.join(destination_root, path))
32
+ template "#{name.underscore}.rb", path
33
+ end
25
34
  end
26
35
  end
27
36
  end
@@ -13,9 +13,15 @@ module Noticed
13
13
 
14
14
  desc "Generates a notification with the given NAME."
15
15
 
16
+ class_option :parent, type: :string, default: "ApplicationNotifier", desc: "The parent class for the generated notifier"
17
+
18
+ # ApplicationNotifier may be defined elsewhere, such as in an engine
16
19
  def generate_abstract_class
17
- return if File.exist?("app/notifiers/application_notifier.rb")
18
- template "application_notifier.rb", "app/notifiers/application_notifier.rb"
20
+ return unless parent == "ApplicationNotifier"
21
+
22
+ path = "app/notifiers/application_notifier.rb"
23
+ return if parent.safe_constantize || File.exist?(File.join(destination_root, path))
24
+ template "application_notifier.rb", path
19
25
  end
20
26
 
21
27
  def generate_notification
@@ -27,6 +33,10 @@ module Noticed
27
33
  def file_name # :doc:
28
34
  @_file_name ||= super.sub(/_notifier\z/i, "")
29
35
  end
36
+
37
+ def parent
38
+ options[:parent]
39
+ end
30
40
  end
31
41
  end
32
42
  end
@@ -2,7 +2,7 @@
2
2
  #
3
3
  # <%= class_name %>Notifier.with(record: @post, message: "New post").deliver(User.all)
4
4
 
5
- class <%= class_name %>Notifier < ApplicationNotifier
5
+ class <%= class_name %>Notifier < <%= parent %>
6
6
  # Add your delivery methods
7
7
  #
8
8
  # deliver_by :email do |config|
@@ -4,6 +4,18 @@ module Noticed
4
4
  module ApiClient
5
5
  extend ActiveSupport::Concern
6
6
 
7
+ # Webhook URLs whose path carries the credential. The first group is kept and the second is
8
+ # replaced with [FILTERED] in logs and error messages.
9
+ #
10
+ # Discord https://discord.com/api/webhooks/{id}/{token}[/slack|/github]
11
+ # Slack https://hooks.slack.com/services/T000/B000/{token}
12
+ # Microsoft Teams https://{tenant}.webhook.office.com/webhookb2/{group}@{tenant}/IncomingWebhook/... (everything after is filtered)
13
+ CREDENTIAL_PATHS = [
14
+ %r{(discord(?:app)?\.com/api/(?:v\d+/)?webhooks/\d+/)([^/]+)},
15
+ %r{(hooks\.slack\.com/(?:services/)?T[^/]+/B[^/]+/)([^/]+)},
16
+ %r{(/webhookb2/[^/]+/IncomingWebhook/)(.+)}
17
+ ]
18
+
7
19
  # Helper method for making POST requests from delivery methods
8
20
  #
9
21
  # Usage:
@@ -34,7 +46,7 @@ module Noticed
34
46
  end
35
47
 
36
48
  # Bodies aren't logged since they can contain credentials and access tokens
37
- logger.debug("POST #{url}")
49
+ logger.debug("POST #{ApiClient.filtered_url(url)}")
38
50
  response = http.request(request)
39
51
  logger.debug("Response: #{response.code}")
40
52
 
@@ -42,5 +54,23 @@ module Noticed
42
54
 
43
55
  response
44
56
  end
57
+
58
+ # Returns the URL with credentials replaced by [FILTERED], like Rails does for request parameters
59
+ #
60
+ # Noticed::ApiClient.filtered_url("https://discord.com/api/webhooks/123/abc?wait=true")
61
+ # # => "https://discord.com/api/webhooks/123/[FILTERED]?wait=true"
62
+ def self.filtered_url(url)
63
+ path, query = url.to_s.split("?", 2)
64
+ CREDENTIAL_PATHS.each { |pattern| path = path.sub(pattern) { "#{$1}[FILTERED]" } }
65
+
66
+ if query
67
+ # Application's filter_parameters plus sig, the secret of Microsoft Teams Workflows URLs:
68
+ # https://prod-00.westus.logic.azure.com/workflows/{id}/triggers/manual/paths/invoke?api-version=...&sp=...&sv=...&sig={secret}
69
+ parameter_filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters + [:sig])
70
+ query = query.gsub(/([^&=]+)=([^&]*)/) { "#{$1}=#{parameter_filter.filter_param($1, $2)}" }
71
+ end
72
+
73
+ query ? "#{path}?#{query}" : path
74
+ end
45
75
  end
46
76
  end
@@ -1,3 +1,3 @@
1
1
  module Noticed
2
- VERSION = "3.1.0"
2
+ VERSION = "3.2.0"
3
3
  end
data/lib/noticed.rb CHANGED
@@ -35,10 +35,10 @@ module Noticed
35
35
 
36
36
  def initialize(response, url, args)
37
37
  @response = response
38
- @url = url
38
+ @url = ApiClient.filtered_url(url)
39
39
  @args = args
40
40
 
41
- super("POST request to #{url} returned #{response.code} response:\n#{response.body.inspect}")
41
+ super("POST request to #{@url} returned #{response.code} response:\n#{response.body.inspect}")
42
42
  end
43
43
  end
44
44
  end
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: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver