File: delivery_method.rb

package info (click to toggle)
ruby-noticed 3.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 324 kB
  • sloc: ruby: 1,186; makefile: 4
file content (63 lines) | stat: -rw-r--r-- 1,816 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module Noticed
  class DeliveryMethod < Noticed.parent_class.constantize
    include ApiClient
    include RequiredOptions

    extend ActiveModel::Callbacks

    define_model_callbacks :deliver

    class_attribute :logger, default: Rails.logger

    attr_reader :config, :event, :notification
    delegate :recipient, to: :notification
    delegate :record, :params, to: :event

    def perform(delivery_method_name, notification, recipient: nil, params: {}, overrides: {})
      # Ephemeral notifications
      if notification.is_a? String
        @notification = notification.constantize.new_with_params(recipient, params)
        @event = @notification.event
      else
        @notification = notification
        @event = notification.event
      end

      # Look up config from Notifier and merge overrides
      @config = event.delivery_methods.fetch(delivery_method_name).config.merge(overrides)

      return false if config.has_key?(:if) && !evaluate_option(:if)
      return false if config.has_key?(:unless) && evaluate_option(:unless)

      run_callbacks :deliver do
        deliver
      end
    end

    def deliver
      raise NotImplementedError, "Delivery methods must implement the `deliver` method"
    end

    def fetch_constant(name)
      option = evaluate_option(name)
      option.is_a?(String) ? option.constantize : option
    end

    def evaluate_option(name)
      option = config[name]

      # Evaluate Proc within the context of the Notification
      if option&.respond_to?(:call)
        notification.instance_exec(&option)

      # Call method if symbol and matching method on Notifier
      elsif option.is_a?(Symbol) && event.respond_to?(option, true)
        event.send(option, notification)

      # Return the value
      else
        option
      end
    end
  end
end