Class: Noticed::BulkDeliveryMethod

Inherits:
ApplicationJob show all
Extended by:
ActiveModel::Callbacks
Includes:
ApiClient, RequiredOptions
Defined in:
lib/noticed/bulk_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.



11
12
13
# File 'lib/noticed/bulk_delivery_method.rb', line 11

def config
  @config
end

#eventObject (readonly)

Returns the value of attribute event.



11
12
13
# File 'lib/noticed/bulk_delivery_method.rb', line 11

def event
  @event
end

Instance Method Details

#deliverObject

Raises:

  • (NotImplementedError)


31
32
33
# File 'lib/noticed/bulk_delivery_method.rb', line 31

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

#evaluate_option(name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/noticed/bulk_delivery_method.rb', line 40

def evaluate_option(name)
  option = config[name]

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

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

  # Return the value
  else
    option
  end
end

#fetch_constant(name) ⇒ Object



35
36
37
38
# File 'lib/noticed/bulk_delivery_method.rb', line 35

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

#perform(delivery_method_name, event, recipient: nil, params: {}, overrides: {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/noticed/bulk_delivery_method.rb', line 13

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

  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