Class: Noticed::DeliveryMethods::Base

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks
Defined in:
lib/noticed/delivery_methods/base.rb

Direct Known Subclasses

ActionCable, Database, Email, MicrosoftTeams, Slack, Test, Twilio, Vonage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#notificationObject (readonly)

Returns the value of attribute notification.



9
10
11
# File 'lib/noticed/delivery_methods/base.rb', line 9

def notification
  @notification
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/noticed/delivery_methods/base.rb', line 9

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/noticed/delivery_methods/base.rb', line 9

def params
  @params
end

#recipientObject (readonly)

Returns the value of attribute recipient.



9
10
11
# File 'lib/noticed/delivery_methods/base.rb', line 9

def recipient
  @recipient
end

#recordObject (readonly)

Returns the value of attribute record.



9
10
11
# File 'lib/noticed/delivery_methods/base.rb', line 9

def record
  @record
end

Class Method Details

.inherited(base) ⇒ Object

Copy option names from parent



13
14
15
16
# File 'lib/noticed/delivery_methods/base.rb', line 13

def inherited(base) # :nodoc:
  base.option_names = option_names.dup
  super
end

.options(*names) ⇒ Object Also known as: option



18
19
20
# File 'lib/noticed/delivery_methods/base.rb', line 18

def options(*names)
  option_names.concat Array.wrap(names)
end

.validate!(delivery_method_options) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/noticed/delivery_methods/base.rb', line 23

def validate!(delivery_method_options)
  option_names.each do |option_name|
    unless delivery_method_options.key? option_name
      raise ValidationError, "option `#{option_name}` must be set for #{name}"
    end
  end
end

Instance Method Details

#assign_args(args) ⇒ Object



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

def assign_args(args)
  @notification = args.fetch(:notification_class).constantize.new(args[:params])
  @options = args[:options] || {}
  @params = args[:params]
  @recipient = args[:recipient]
  @record = args[:record]

  # Make notification aware of database record and recipient during delivery
  @notification.record = args[:record]
  @notification.recipient = args[:recipient]
  self
end

#deliverObject

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/noticed/delivery_methods/base.rb', line 56

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

#perform(args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/noticed/delivery_methods/base.rb', line 45

def perform(args)
  assign_args(args)

  return if (condition = @options[:if]) && !@notification.send(condition)
  return if (condition = @options[:unless]) && @notification.send(condition)

  run_callbacks :deliver do
    deliver
  end
end