Class: AbstractNotifier::Base

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

Overview

Base class for notifiers

Defined Under Namespace

Classes: ParamsProxy

Constant Summary

Constants included from Callbacks

Callbacks::CALLBACK_TERMINATOR

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notification_name, **params) ⇒ Base

Returns a new instance of Base.



180
181
182
183
# File 'lib/abstract_notifier/base.rb', line 180

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

Class Attribute Details

.driverObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/abstract_notifier/base.rb', line 84

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

#notification_nameObject (readonly)

Returns the value of attribute notification_name.



178
179
180
# File 'lib/abstract_notifier/base.rb', line 178

def notification_name
  @notification_name
end

#paramsObject (readonly)

Returns the value of attribute params.



178
179
180
# File 'lib/abstract_notifier/base.rb', line 178

def params
  @params
end

Class Method Details

.action_methodsObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/abstract_notifier/base.rb', line 162

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



101
102
103
104
105
106
107
108
109
110
# File 'lib/abstract_notifier/base.rb', line 101

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



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

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

.default(method_name = nil, **hargs, &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/abstract_notifier/base.rb', line 112

def default(method_name = nil, **hargs, &block)
  return @defaults_generator = block if block

  return @defaults_generator = proc { send(method_name) } unless method_name.nil?

  @default_params =
    if superclass.respond_to?(:default_params)
      superclass.default_params.merge(hargs).freeze
    else
      hargs.freeze
    end
end

.default_paramsObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/abstract_notifier/base.rb', line 134

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

  @default_params =
    if superclass.respond_to?(:default_params)
      superclass.default_params.dup
    else
      {}
    end
end

.defaults_generatorObject



125
126
127
128
129
130
131
132
# File 'lib/abstract_notifier/base.rb', line 125

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

  @defaults_generator =
    if superclass.respond_to?(:defaults_generator)
      superclass.defaults_generator
    end
end

.method_missing(method_name, *args, **kwargs) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/abstract_notifier/base.rb', line 145

def method_missing(method_name, *args, **kwargs)
  if action_methods.include?(method_name.to_s)
    NotificationDelivery.new(self, method_name, args:, kwargs:)
  else
    super
  end
end

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

Returns:

  • (Boolean)


157
158
159
# File 'lib/abstract_notifier/base.rb', line 157

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

.with(params) ⇒ Object



153
154
155
# File 'lib/abstract_notifier/base.rb', line 153

def with(params)
  ParamsProxy.new(self, params)
end

Instance Method Details

#deliver!(notification) ⇒ Object



189
190
191
# File 'lib/abstract_notifier/base.rb', line 189

def deliver!(notification)
  self.class.driver.call(notification.payload)
end

#notification(**payload) ⇒ Object

Raises:

  • (ArgumentError)


193
194
195
196
197
198
199
200
201
202
# File 'lib/abstract_notifier/base.rb', line 193

def notification(**payload)
  merge_defaults!(payload)

  payload[:body] = implicit_payload_body unless payload.key?(:body)

  raise ArgumentError, "Notification body must be present" if
    payload[:body].nil? || payload[:body].empty?

  @notification = Notification.new(payload)
end

#process_actionObject



185
186
187
# File 'lib/abstract_notifier/base.rb', line 185

def process_action(...)
  public_send(...)
end