Class: Optimizely::NotificationCenter
- Inherits:
- 
      Object
      
        - Object
- Optimizely::NotificationCenter
 
- Defined in:
- lib/optimizely/notification_center.rb
Constant Summary collapse
- NOTIFICATION_TYPES =
- { # DEPRECATED: ACTIVATE notification type is deprecated since relase 3.1.0. ACTIVATE: 'ACTIVATE: experiment, user_id, attributes, variation, event', DECISION: 'DECISION: type, user_id, attributes, decision_info', LOG_EVENT: 'LOG_EVENT: type, log_event', OPTIMIZELY_CONFIG_UPDATE: 'optimizely_config_update', TRACK: 'TRACK: event_key, user_id, attributes, event_tags, event' }.freeze 
Instance Attribute Summary collapse
- #notification_id ⇒ Object readonly
- #notifications ⇒ Object readonly
Instance Method Summary collapse
- 
  
    
      #add_notification_listener(notification_type, notification_callback = nil) { ... } ⇒ notification ID 
    
    
  
  
  
  
  
  
  
  
  
    Adds notification callback to the notification center. 
- 
  
    
      #clean_all_notifications  ⇒ Object 
    
    
  
  
  
  
  
  
  deprecated
  
  
    Deprecated. Use #clear_all_notification_listeners instead. 
- 
  
    
      #clear_all_notification_listeners  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Removes all notifications. 
- 
  
    
      #clear_notification_listeners(notification_type)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Removes notifications for a certain notification type. 
- 
  
    
      #clear_notifications(notification_type)  ⇒ Object 
    
    
  
  
  
  
  
  
  deprecated
  
  
    Deprecated. Use #clear_notification_listeners instead. 
- 
  
    
      #initialize(logger, error_handler)  ⇒ NotificationCenter 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of NotificationCenter. 
- #notification_count(notification_type) ⇒ Object
- 
  
    
      #remove_notification_listener(notification_id)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Removes previously added notification callback. 
- 
  
    
      #send_notifications(notification_type, *args)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sends off the notification for the specific event. 
Constructor Details
#initialize(logger, error_handler) ⇒ NotificationCenter
Returns a new instance of NotificationCenter.
| 32 33 34 35 36 37 38 | # File 'lib/optimizely/notification_center.rb', line 32 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_id ⇒ Object (readonly)
| 21 22 23 | # File 'lib/optimizely/notification_center.rb', line 21 def notification_id @notification_id end | 
#notifications ⇒ Object (readonly)
| 21 22 23 | # File 'lib/optimizely/notification_center.rb', line 21 def notifications @notifications end | 
Instance Method Details
#add_notification_listener(notification_type, notification_callback = nil) { ... } ⇒ notification ID
Adds notification callback to the notification center
| 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | # File 'lib/optimizely/notification_center.rb', line 48 def add_notification_listener(notification_type, notification_callback = nil, &block) return nil unless notification_type_valid?(notification_type) if notification_callback && block_given? @logger.log Logger::ERROR, 'Callback and block are mutually exclusive.' return nil end notification_callback ||= block unless notification_callback @logger.log Logger::ERROR, 'Callback can not be empty.' return nil end unless notification_callback.respond_to? :call @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_notifications ⇒ Object
Use #clear_all_notification_listeners instead.
| 117 118 119 120 | # File 'lib/optimizely/notification_center.rb', line 117 def clean_all_notifications @logger.log Logger::WARN, "'clean_all_notifications' is deprecated. Call 'clear_all_notification_listeners' instead." clear_all_notification_listeners end | 
#clear_all_notification_listeners ⇒ Object
Removes all notifications
| 123 124 125 | # File 'lib/optimizely/notification_center.rb', line 123 def clear_all_notification_listeners @notifications.each_key { |key| @notifications[key] = [] } end | 
#clear_notification_listeners(notification_type) ⇒ Object
Removes notifications for a certain notification type
| 109 110 111 112 113 114 | # File 'lib/optimizely/notification_center.rb', line 109 def clear_notification_listeners(notification_type) 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 | 
#clear_notifications(notification_type) ⇒ Object
Use #clear_notification_listeners instead.
| 100 101 102 103 | # File 'lib/optimizely/notification_center.rb', line 100 def clear_notifications(notification_type) @logger.log Logger::WARN, "'clear_notifications' is deprecated. Call 'clear_notification_listeners' instead." clear_notification_listeners(notification_type) end | 
#notification_count(notification_type) ⇒ Object
| 147 148 149 | # File 'lib/optimizely/notification_center.rb', line 147 def notification_count(notification_type) @notifications.include?(notification_type) ? @notifications[notification_type].count : 0 end | 
#remove_notification_listener(notification_id) ⇒ Boolean
Removes previously added notification callback
| 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | # File 'lib/optimizely/notification_center.rb', line 83 def remove_notification_listener(notification_id) 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
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
| 134 135 136 137 138 139 140 141 142 143 144 145 | # File 'lib/optimizely/notification_center.rb', line 134 def send_notifications(notification_type, *args) return nil unless notification_type_valid?(notification_type) @notifications[notification_type].each do |notification| 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 |