Class: Noticed::DeliveryMethod

Inherits:
ApplicationJob show all
Includes:
ApiClient, RequiredOptions
Defined in:
lib/noticed/delivery_method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApiClient

#post_request

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/noticed/delivery_method.rb', line 8

def config
  @config
end

#eventObject (readonly)

Returns the value of attribute event.



8
9
10
# File 'lib/noticed/delivery_method.rb', line 8

def event
  @event
end

#notificationObject (readonly)

Returns the value of attribute notification.



8
9
10
# File 'lib/noticed/delivery_method.rb', line 8

def notification
  @notification
end

Instance Method Details

#deliverObject

Raises:

  • (NotImplementedError)


24
25
26
# File 'lib/noticed/delivery_method.rb', line 24

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

#evaluate_option(name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/noticed/delivery_method.rb', line 33

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)
    event.send(option, self)

  # Return the value
  else
    option
  end
end

#fetch_constant(name) ⇒ Object



28
29
30
31
# File 'lib/noticed/delivery_method.rb', line 28

def fetch_constant(name)
  option = config[name]
  option.is_a?(String) ? option.constantize : option
end

#perform(delivery_method_name, notification, overrides: {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/noticed/delivery_method.rb', line 11

def perform(delivery_method_name, notification, overrides: {})
  @notification = notification
  @event = notification.event

  # 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)

  deliver
end