noticed 1.2.13 → 1.2.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 177af67cbc18228368b1046461e97ec0046ab7be37dcd528d0834456184d3dbc
4
- data.tar.gz: 4b68f5ad47ac4cde2fb48f0738792c8d70332a0bf3b2a5bc4c6c0a830bd9b030
3
+ metadata.gz: deee1f4f64ac8d04fafc5b9a6fb0c25811361fa650a4a337e4e134159ff82a23
4
+ data.tar.gz: dae9d38da48b00c508460b7ccf3a797df8a69cea372f2738a374321bb9d6a033
5
5
  SHA512:
6
- metadata.gz: 127fb40a8e97d833c01d8f35586aee3ae5be6b673e20670306a5c94982ea4b2f358d7d471020460689483c326c7caf5a9f9bd3854b2c4052f47f2714a4aa8b92
7
- data.tar.gz: 5f285005c9e5553e9cde3203a7b7b646c3448edb9fdb0335e0286dc2bee14d09f7001644733caa8a5795f5291380fc4e65aa37ab5c89e9c752a54d2dd35a8a61
6
+ metadata.gz: 9a690b58c36dbf4651734192b59242f4c18cc9bbafeaed95c5570ed4991b48f49610b351f636fb2ca3d64120263b0780c8a15fd7ee9c4f8a7cc7099ab8e959c2
7
+ data.tar.gz: f6f765115f53b1e9ec03ecef10da432ce0794a11a6cc8cac444fe724db1ecdb6c4ce967edff8aa86d5f5edfef424f0b9e605939e0de8cf68b4b89bfb6ad2b207
data/README.md CHANGED
@@ -12,6 +12,7 @@ Currently, we support these notification delivery methods out of the box:
12
12
  * Email
13
13
  * ActionCable channels
14
14
  * Slack
15
+ * Microsoft Teams
15
16
  * Twilio (SMS)
16
17
  * Vonage / Nexmo (SMS)
17
18
 
@@ -86,6 +87,7 @@ class CommentNotification < Noticed::Base
86
87
  end
87
88
 
88
89
  # URL helpers are accessible in notifications
90
+ # Don't forget to set your default_url_options so Rails knows how to generate urls
89
91
  def url
90
92
  post_path(params[:post])
91
93
  end
@@ -104,6 +106,7 @@ end
104
106
 
105
107
  * `if: :method_name` - Calls `method_name`and cancels delivery method if `false` is returned
106
108
  * `unless: :method_name` - Calls `method_name`and cancels delivery method if `true` is returned
109
+ * `delay: ActiveSupport::Duration` - Delays the delivery for the given duration of time
107
110
 
108
111
  ##### Helper Methods
109
112
 
@@ -113,6 +116,12 @@ You can define helper methods inside your Notification object to make it easier
113
116
 
114
117
  Rails url helpers are included in notification classes by default so you have full access to them just like you would in your controllers and views.
115
118
 
119
+ Don't forget, you'll need to configure `default_url_options` in order for Rails to know what host and port to use when generating URLs.
120
+
121
+ ```ruby
122
+ Rails.application.routes.default_url_options[:host] = 'localhost:3000'
123
+ ```
124
+
116
125
  **Callbacks**
117
126
 
118
127
  Like ActiveRecord, notifications have several different types of callbacks.
@@ -246,6 +255,42 @@ Sends a Slack notification via webhook.
246
255
 
247
256
  Defaults to `Rails.application.credentials.slack[:notification_url]`
248
257
 
258
+ ### Microsoft Teams
259
+
260
+ Sends a Teams notification via webhook.
261
+
262
+ `deliver_by :microsoft_teams`
263
+
264
+ #### Options
265
+
266
+ * `format: :format_for_teams` - *Optional*
267
+
268
+ Use a custom method to define the payload sent to slack. Method should return a Hash.
269
+ Documentation for posting via Webhooks available at: https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook
270
+
271
+ ```ruby
272
+ {
273
+ title: "This is the title for the card",
274
+ text: "This is the body text for the card",
275
+ sections: [{activityTitle: "Section Title", activityText: "Section Text"}],
276
+ "potentialAction": [{
277
+ "@type": "OpenUri",
278
+ name: "Button Text",
279
+ targets: [{
280
+ os: "default",
281
+ uri: "https://example.com/foo/action"
282
+ }]
283
+ }]
284
+
285
+ }
286
+ ```
287
+
288
+ * `url: :url_for_teams_channel`: - *Optional*
289
+
290
+ Use a custom method to retrieve the MS Teams Webhook URL. Method should return a string.
291
+
292
+ Defaults to `Rails.application.credentials.microsoft_teams[:notification_url]`
293
+
249
294
  ### Twilio SMS
250
295
 
251
296
  Sends an SMS notification via Twilio.
@@ -342,19 +387,24 @@ Delivery methods have access to the following methods and attributes:
342
387
 
343
388
  #### Validating options passed to Custom Delivery methods
344
389
 
345
- You can validate the options passed to the custom delivery method and raise validation errors. This is helpful for debugging to make sure valid and required options were passed in.
390
+ The presence of the delivery method options is automatically validated if using the `option(s)` method.
346
391
 
347
- To do this, simply override the `self.validate!(options)` method from the `Noticed::DeliveryMethods::Base` class in your Custom Delivery method.
392
+ If you want to validate that the passed options contain valid values, or to add any custom validations, override the `self.validate!(delivery_method_options)` method from the `Noticed::DeliveryMethods::Base` class.
348
393
 
349
394
  ```ruby
350
395
  class DeliveryMethods::Discord < Noticed::DeliveryMethods::Base
396
+ option :username # Requires the username option to be passed
397
+
351
398
  def deliver
352
399
  # Logic for sending a Discord notification
353
400
  end
354
401
 
355
- def self.validate!(options)
356
- unless options.key?(:sent_by)
357
- raise Noticed::ValidationError, 'the `sent_by` option is missing'
402
+ def self.validate!(delivery_method_options)
403
+ super # Don't forget to call super, otherwise option presence won't be validated
404
+
405
+   # Custom validations
406
+ if delivery_method_options[:username].blank?
407
+ raise Noticed::ValidationError, 'the `username` option must be present'
358
408
  end
359
409
  end
360
410
  end
@@ -370,7 +420,7 @@ To fix the error, the argument has to be passed correctly. For example:
370
420
 
371
421
  ```ruby
372
422
  class CommentNotification < Noticed::Base
373
- deliver_by :discord, class: 'DeliveryMethods::Discord', sent_by: User.admin.first
423
+ deliver_by :discord, class: 'DeliveryMethods::Discord', username: User.admin.username
374
424
  end
375
425
  ```
376
426
 
@@ -394,7 +444,7 @@ Rails 6.1+ can serialize Class and Module objects as arguments to ActiveJob. The
394
444
  deliver_by DeliveryMethods::Discord
395
445
  ```
396
446
 
397
- For Rails 6.0 and earlier, you must pass strings of the class names in the `deliver_by` options.
447
+ For Rails 6.0, you must pass strings of the class names in the `deliver_by` options.
398
448
 
399
449
  ```ruby
400
450
  deliver_by :discord, class: "DeliveryMethods::Discord"
@@ -428,10 +478,11 @@ Convert back into a Noticed notification object:
428
478
  @notification.to_notification
429
479
  ```
430
480
 
431
- Mark notification as read:
481
+ Mark notification as read / unread:
432
482
 
433
483
  ```ruby
434
484
  @notification.mark_as_read!
485
+ @notification.mark_as_unread!
435
486
  ```
436
487
 
437
488
  Check if read / unread:
@@ -471,7 +522,7 @@ class Post < ApplicationRecord
471
522
  end
472
523
  ```
473
524
 
474
- ##### Polymorphic Assocation
525
+ ##### Polymorphic Association
475
526
 
476
527
  If your notification is only associated with one model or you're using a `text` column for your params column , then a polymorphic association is what you'll want to use.
477
528
 
@@ -15,7 +15,7 @@ module Noticed
15
15
  argument :attributes, type: :array, default: [], banner: "field:type field:type"
16
16
 
17
17
  def generate_notification
18
- generate :model, name, "recipient:references{polymorphic}", "type", params_column, "read_at:datetime", *attributes
18
+ generate :model, name, "recipient:references{polymorphic}", "type", params_column, "read_at:datetime:index", *attributes
19
19
  end
20
20
 
21
21
  def add_noticed_model
@@ -43,7 +43,7 @@ module Noticed
43
43
 
44
44
  def params_column
45
45
  case ActiveRecord::Base.configurations.configs_for(spec_name: "primary").config["adapter"]
46
- when "mysql"
46
+ when "mysql2"
47
47
  "params:json"
48
48
  when "postgresql"
49
49
  "params:jsonb"
@@ -8,6 +8,7 @@ module Noticed
8
8
  autoload :Model, "noticed/model"
9
9
  autoload :TextCoder, "noticed/text_coder"
10
10
  autoload :Translation, "noticed/translation"
11
+ autoload :NotificationChannel, "noticed/notification_channel"
11
12
 
12
13
  module DeliveryMethods
13
14
  autoload :Base, "noticed/delivery_methods/base"
@@ -15,6 +16,7 @@ module Noticed
15
16
  autoload :Database, "noticed/delivery_methods/database"
16
17
  autoload :Email, "noticed/delivery_methods/email"
17
18
  autoload :Slack, "noticed/delivery_methods/slack"
19
+ autoload :MicrosoftTeams, "noticed/delivery_methods/microsoft_teams"
18
20
  autoload :Test, "noticed/delivery_methods/test"
19
21
  autoload :Twilio, "noticed/delivery_methods/twilio"
20
22
  autoload :Vonage, "noticed/delivery_methods/vonage"
@@ -32,7 +32,7 @@ module Noticed
32
32
  def params(*names)
33
33
  param_names.concat Array.wrap(names)
34
34
  end
35
- alias param params
35
+ alias_method :param, :params
36
36
  end
37
37
 
38
38
  def initialize(params = {})
@@ -85,9 +85,6 @@ module Noticed
85
85
 
86
86
  # Actually runs an individual delivery
87
87
  def run_delivery_method(delivery_method, recipient:, enqueue:)
88
- return if (delivery_method_name = delivery_method.dig(:options, :if)) && !send(delivery_method_name)
89
- return if (delivery_method_name = delivery_method.dig(:options, :unless)) && send(delivery_method_name)
90
-
91
88
  args = {
92
89
  notification_class: self.class.name,
93
90
  options: delivery_method[:options],
@@ -98,7 +95,15 @@ module Noticed
98
95
 
99
96
  run_callbacks delivery_method[:name] do
100
97
  method = delivery_method_for(delivery_method[:name], delivery_method[:options])
101
- enqueue ? method.perform_later(args) : method.perform_now(args)
98
+
99
+ # Always perfrom later if a delay is present
100
+ if (delay = delivery_method.dig(:options, :delay))
101
+ method.set(wait: delay).perform_later(args)
102
+ elsif enqueue
103
+ method.perform_later(args)
104
+ else
105
+ method.perform_now(args)
106
+ end
102
107
  end
103
108
  end
104
109
 
@@ -106,7 +111,7 @@ module Noticed
106
111
  if options[:class]
107
112
  options[:class].constantize
108
113
  else
109
- "Noticed::DeliveryMethods::#{name.to_s.classify}".constantize
114
+ "Noticed::DeliveryMethods::#{name.to_s.camelize}".constantize
110
115
  end
111
116
  end
112
117
 
@@ -17,18 +17,18 @@ module Noticed
17
17
 
18
18
  def channel
19
19
  @channel ||= begin
20
- value = options[:channel]
21
- case value
22
- when String
23
- value.constantize
24
- when Symbol
25
- notification.send(value)
26
- when Class
27
- value
28
- else
29
- Noticed::NotificationChannel
30
- end
31
- end
20
+ value = options[:channel]
21
+ case value
22
+ when String
23
+ value.constantize
24
+ when Symbol
25
+ notification.send(value)
26
+ when Class
27
+ value
28
+ else
29
+ Noticed::NotificationChannel
30
+ end
31
+ end
32
32
  end
33
33
  end
34
34
  end
@@ -4,11 +4,35 @@ module Noticed
4
4
  extend ActiveModel::Callbacks
5
5
  define_model_callbacks :deliver
6
6
 
7
- attr_reader :notification, :options, :recipient, :record
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
8
31
 
9
32
  def perform(args)
10
33
  @notification = args[:notification_class].constantize.new(args[:params])
11
34
  @options = args[:options]
35
+ @params = args[:params]
12
36
  @recipient = args[:recipient]
13
37
  @record = args[:record]
14
38
 
@@ -16,6 +40,9 @@ module Noticed
16
40
  @notification.record = args[:record]
17
41
  @notification.recipient = args[:recipient]
18
42
 
43
+ return if (condition = @options[:if]) && !@notification.send(condition)
44
+ return if (condition = @options[:unless]) && @notification.send(condition)
45
+
19
46
  run_callbacks :deliver do
20
47
  deliver
21
48
  end
@@ -25,11 +52,6 @@ module Noticed
25
52
  raise NotImplementedError, "Delivery methods must implement a deliver method"
26
53
  end
27
54
 
28
- def self.validate!(options)
29
- # Override this method in your custom DeliveryMethod class to validate the options
30
- # and raise error, if invalid.
31
- end
32
-
33
55
  private
34
56
 
35
57
  # Helper method for making POST requests from delivery methods
@@ -1,14 +1,10 @@
1
1
  module Noticed
2
2
  module DeliveryMethods
3
3
  class Email < Base
4
- def deliver
5
- mailer.with(format).send(method.to_sym).deliver_later
6
- end
4
+ option :mailer
7
5
 
8
- def self.validate!(options)
9
- unless options.key?(:mailer)
10
- raise ValidationError, "email delivery method requires a 'mailer' to be specified"
11
- end
6
+ def deliver
7
+ mailer.with(format).send(method.to_sym).deliver_now
12
8
  end
13
9
 
14
10
  private
@@ -0,0 +1,32 @@
1
+ module Noticed
2
+ module DeliveryMethods
3
+ class MicrosoftTeams < Base
4
+ def deliver
5
+ 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
+ title: notification.params[:title],
16
+ text: notification.params[:text],
17
+ sections: notification.params[:sections],
18
+ potentialAction: notification.params[:notification_action]
19
+ }
20
+ end
21
+ end
22
+
23
+ def url
24
+ if (method = options[:url])
25
+ notification.send(method)
26
+ else
27
+ Rails.application.credentials.microsoft_teams[:notification_url]
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -7,6 +7,8 @@ module Noticed
7
7
  if !options[:ignore_failure] && status != "0"
8
8
  raise ResponseUnsuccessful.new(response)
9
9
  end
10
+
11
+ response
10
12
  end
11
13
 
12
14
  private
@@ -32,16 +32,20 @@ module Noticed
32
32
  # Rehydrate the database notification into the Notification object for rendering
33
33
  def to_notification
34
34
  @_notification ||= begin
35
- instance = type.constantize.with(params)
36
- instance.record = self
37
- instance
38
- end
35
+ instance = type.constantize.with(params)
36
+ instance.record = self
37
+ instance
38
+ end
39
39
  end
40
40
 
41
41
  def mark_as_read!
42
42
  update(read_at: Time.current)
43
43
  end
44
44
 
45
+ def mark_as_unread!
46
+ update(read_at: nil)
47
+ end
48
+
45
49
  def unread?
46
50
  !read?
47
51
  end
@@ -10,7 +10,7 @@ module Noticed
10
10
  def translate(key, **options)
11
11
  I18n.translate(scope_translation_key(key), **options)
12
12
  end
13
- alias t translate
13
+ alias_method :t, :translate
14
14
 
15
15
  def scope_translation_key(key)
16
16
  if key.to_s.start_with?(".")
@@ -1,3 +1,3 @@
1
1
  module Noticed
2
- VERSION = "1.2.13"
2
+ VERSION = "1.2.18"
3
3
  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.2.13
4
+ version: 1.2.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-25 00:00:00.000000000 Z
11
+ date: 2020-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: mocha
70
+ name: webmock
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -91,7 +91,6 @@ files:
91
91
  - MIT-LICENSE
92
92
  - README.md
93
93
  - Rakefile
94
- - app/channels/noticed/notification_channel.rb
95
94
  - lib/generators/noticed/delivery_method_generator.rb
96
95
  - lib/generators/noticed/model_generator.rb
97
96
  - lib/generators/noticed/notification_generator.rb
@@ -105,12 +104,14 @@ files:
105
104
  - lib/noticed/delivery_methods/base.rb
106
105
  - lib/noticed/delivery_methods/database.rb
107
106
  - lib/noticed/delivery_methods/email.rb
107
+ - lib/noticed/delivery_methods/microsoft_teams.rb
108
108
  - lib/noticed/delivery_methods/slack.rb
109
109
  - lib/noticed/delivery_methods/test.rb
110
110
  - lib/noticed/delivery_methods/twilio.rb
111
111
  - lib/noticed/delivery_methods/vonage.rb
112
112
  - lib/noticed/engine.rb
113
113
  - lib/noticed/model.rb
114
+ - lib/noticed/notification_channel.rb
114
115
  - lib/noticed/text_coder.rb
115
116
  - lib/noticed/translation.rb
116
117
  - lib/noticed/version.rb
@@ -119,7 +120,7 @@ homepage: https://github.com/excid3/noticed
119
120
  licenses:
120
121
  - MIT
121
122
  metadata: {}
122
- post_install_message:
123
+ post_install_message:
123
124
  rdoc_options: []
124
125
  require_paths:
125
126
  - lib
@@ -134,8 +135,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
135
  - !ruby/object:Gem::Version
135
136
  version: '0'
136
137
  requirements: []
137
- rubygems_version: 3.1.2
138
- signing_key:
138
+ rubygems_version: 3.1.4
139
+ signing_key:
139
140
  specification_version: 4
140
141
  summary: Notifications for Ruby on Rails applications
141
142
  test_files: []