Class: Wupee::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/wupee/notifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Notifier

Returns a new instance of Notifier.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/wupee/notifier.rb', line 5

def initialize(opts = {})
  @attached_object = opts[:attached_object]

  receiver_arg = opts[:receiver] || opts[:receivers]
  receiver(receiver_arg) if receiver_arg

  @subject_vars = opts[:subject_vars] || {}
  @locals = opts[:locals] || {}

  @deliver_when = opts[:deliver]
  notif_type(opts[:notif_type]) if opts[:notif_type]
end

Instance Attribute Details

#attached_object(attached_object) ⇒ Object (readonly)

Returns the value of attribute attached_object.



3
4
5
# File 'lib/wupee/notifier.rb', line 3

def attached_object
  @attached_object
end

#deliver_whenObject (readonly)

Returns the value of attribute deliver_when.



3
4
5
# File 'lib/wupee/notifier.rb', line 3

def deliver_when
  @deliver_when
end

#locals(locals = {}) ⇒ Object (readonly)

Returns the value of attribute locals.



3
4
5
# File 'lib/wupee/notifier.rb', line 3

def locals
  @locals
end

#notification_typeObject (readonly)

Returns the value of attribute notification_type.



3
4
5
# File 'lib/wupee/notifier.rb', line 3

def notification_type
  @notification_type
end

#receiver_sObject (readonly)

Returns the value of attribute receiver_s.



3
4
5
# File 'lib/wupee/notifier.rb', line 3

def receiver_s
  @receiver_s
end

#subject_vars(subject_vars = {}) ⇒ Object (readonly)

Returns the value of attribute subject_vars.



3
4
5
# File 'lib/wupee/notifier.rb', line 3

def subject_vars
  @subject_vars
end

Instance Method Details

#deliver(deliver_method) ⇒ Object



38
39
40
# File 'lib/wupee/notifier.rb', line 38

def deliver(deliver_method)
  @deliver_when = deliver_method
end

#executeObject

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wupee/notifier.rb', line 50

def execute
  raise ArgumentError.new('receiver or receivers is missing') if @receiver_s.nil?
  raise ArgumentError.new('notif_type is missing') if @notification_type.nil?

  notif_type_configs = Wupee::NotificationTypeConfiguration.includes(:receiver).where(receiver: @receiver_s, notification_type: @notification_type)

  notif_type_configs.each do |notif_type_config|
    notification = Wupee::Notification.new(receiver: notif_type_config.receiver, notification_type: @notification_type, attached_object: @attached_object)
    notification.is_read = true unless notif_type_config.wants_notification?
    notification.save!

    subject_interpolations = interpolate_vars(@subject_vars, notification)
    locals_interpolations = interpolate_vars(@locals, notification)

    send_email(notification, subject_interpolations, locals_interpolations) if notif_type_config.wants_email?
  end
end

#notif_type(notif_type) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/wupee/notifier.rb', line 18

def notif_type(notif_type)
  if notif_type.is_a?(Wupee::NotificationType)
    @notification_type = notif_type
  else
    @notification_type = Wupee::NotificationType.find_by!(name: notif_type)
  end
end

#receiver(receiver) ⇒ Object



30
31
32
# File 'lib/wupee/notifier.rb', line 30

def receiver(receiver)
  @receiver_s = [*receiver]
end

#receivers(receivers) ⇒ Object



34
35
36
# File 'lib/wupee/notifier.rb', line 34

def receivers(receivers)
  receiver(receivers)
end