Class: AbstractNotifier::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/abstract_notifier/base.rb

Overview

Base class for notifiers

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**params) ⇒ Base

Returns a new instance of Base.



92
93
94
# File 'lib/abstract_notifier/base.rb', line 92

def initialize(**params)
  @params = params.freeze
end

Class Attribute Details

.driverObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/abstract_notifier/base.rb', line 33

def driver
  return @driver if instance_variable_defined?(:@driver)

  @driver =
    if superclass.respond_to?(:driver)
      superclass.driver
    else
      raise "Driver not found for #{name}. " \
            "Please, specify driver via `self.driver = MyDriver`"
    end
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



90
91
92
# File 'lib/abstract_notifier/base.rb', line 90

def params
  @params
end

Class Method Details

.action_methodsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/abstract_notifier/base.rb', line 74

def action_methods
  @action_methods ||= begin
    # All public instance methods of this class, including ancestors
    methods = (public_instance_methods(true) -
      # Except for public instance methods of Base and its ancestors
      Base.public_instance_methods(true) +
      # Be sure to include shadowed public instance methods of this class
      public_instance_methods(false))

    methods.map!(&:to_s)

    methods.to_set
  end
end

.async_adapterObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/abstract_notifier/base.rb', line 50

def async_adapter
  return @async_adapter if instance_variable_defined?(:@async_adapter)

  @async_adapter =
    if superclass.respond_to?(:async_adapter)
      superclass.async_adapter
    else
      AbstractNotifier.async_adapter
    end
end

.async_adapter=(args) ⇒ Object



45
46
47
48
# File 'lib/abstract_notifier/base.rb', line 45

def async_adapter=(args)
  adapter, options = Array(args)
  @async_adapter = AsyncAdapters.lookup(adapter, options)
end

.method_missing(method_name, *args) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/abstract_notifier/base.rb', line 61

def method_missing(method_name, *args)
  if action_methods.include?(method_name.to_s)
    new.public_send(method_name, *args)
  else
    super
  end
end

.respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/abstract_notifier/base.rb', line 69

def respond_to_missing?(method_name, _include_private = false)
  action_methods.include?(method_name.to_s) || super
end

Instance Method Details

#notification(**payload) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
100
# File 'lib/abstract_notifier/base.rb', line 96

def notification(**payload)
  raise ArgumentError, "Notification body must be present" if
    payload[:body].nil? || payload[:body].empty?
  Notification.new(self.class, payload)
end