Class: Optimizely::NotificationCenter

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/notification_center.rb

Constant Summary collapse

NOTIFICATION_TYPES =
{
  ACTIVATE: 'ACTIVATE: experiment, user_id, attributes, variation, event',
  TRACK: 'TRACK: event_key, user_id, attributes, event_tags, event'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, error_handler) ⇒ NotificationCenter

Returns a new instance of NotificationCenter.



28
29
30
31
32
33
34
# File 'lib/optimizely/notification_center.rb', line 28

def initialize(logger, error_handler)
  @notification_id = 1
  @notifications = {}
  NOTIFICATION_TYPES.each_value { |value| @notifications[value] = [] }
  @logger = logger
  @error_handler = error_handler
end

Instance Attribute Details

#notification_idObject (readonly)

Returns the value of attribute notification_id.



21
22
23
# File 'lib/optimizely/notification_center.rb', line 21

def notification_id
  @notification_id
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



20
21
22
# File 'lib/optimizely/notification_center.rb', line 20

def notifications
  @notifications
end

Instance Method Details

#add_notification_listener(notification_type, notification_callback) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/optimizely/notification_center.rb', line 36

def add_notification_listener(notification_type, notification_callback)
  # Adds notification callback to the notification center

  # Args:
  #  notification_type: one of the constants in NOTIFICATION_TYPES
  #  notification_callback: function to call when the event is sent

  # Returns:
  #  notification ID used to remove the notification

  return nil unless notification_type_valid?(notification_type)

  unless notification_callback
    @logger.log Logger::ERROR, 'Callback can not be empty.'
    return nil
  end

  unless notification_callback.is_a? Method
    @logger.log Logger::ERROR, 'Invalid notification callback given.'
    return nil
  end

  @notifications[notification_type].each do |notification|
    return -1 if notification[:callback] == notification_callback
  end
  @notifications[notification_type].push(notification_id: @notification_id, callback: notification_callback)
  notification_id = @notification_id
  @notification_id += 1
  notification_id
end

#clean_all_notificationsObject



101
102
103
104
# File 'lib/optimizely/notification_center.rb', line 101

def clean_all_notifications
  # Removes all notifications
  @notifications.each_key { |key| @notifications[key] = [] }
end

#clear_notifications(notification_type) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/optimizely/notification_center.rb', line 89

def clear_notifications(notification_type)
  # Removes notifications for a certain notification type
  #
  # Args:
  #  notification_type: one of the constants in NOTIFICATION_TYPES

  return nil unless notification_type_valid?(notification_type)

  @notifications[notification_type] = []
  @logger.log Logger::INFO, "All callbacks for notification type #{notification_type} have been removed."
end

#remove_notification_listener(notification_id) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/optimizely/notification_center.rb', line 67

def remove_notification_listener(notification_id)
  # Removes previously added notification callback

  # Args:
  #  notification_id:
  # Returns:
  #  The function returns true if found and removed, false otherwise
  unless notification_id
    @logger.log Logger::ERROR, 'Notification ID can not be empty.'
    return nil
  end
  @notifications.each_key do |key|
    @notifications[key].each do |notification|
      if notification_id == notification[:notification_id]
        @notifications[key].delete(notification_id: notification_id, callback: notification[:callback])
        return true
      end
    end
  end
  false
end

#send_notifications(notification_type, *args) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/optimizely/notification_center.rb', line 106

def send_notifications(notification_type, *args)
  # Sends off the notification for the specific event.  Uses var args to pass in a
  # arbitrary list of parameters according to which notification type was sent

  # Args:
  #  notification_type: one of the constants in NOTIFICATION_TYPES
  #  args: list of arguments to the callback
  return nil unless notification_type_valid?(notification_type)

  @notifications[notification_type].each do |notification|
    begin
      notification_callback = notification[:callback]
      notification_callback.call(*args)
      @logger.log Logger::INFO, "Notification #{notification_type} sent successfully."
    rescue => e
      @logger.log(Logger::ERROR, "Problem calling notify callback. Error: #{e}")
      return nil
    end
  end
end