class Optimizely::NotificationCenter
Constants
- NOTIFICATION_TYPES
Attributes
@api no-doc
@api no-doc
Public Class Methods
# 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
Public Instance Methods
Adds notification callback to the notification center
@param notification_type - One of the constants in NOTIFICATION_TYPES
@param notification_callback [lambda, Method, Callable] (default: block) - Called when the event is sent @yield Block to be used as callback if callback omitted.
@return [notification ID] Used to remove the notification
# 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
@deprecated Use {#clear_all_notification_listeners} instead.
# 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
Removes all notifications
# File lib/optimizely/notification_center.rb, line 123 def clear_all_notification_listeners @notifications.each_key { |key| @notifications[key] = [] } end
Removes notifications for a certain notification type
@param notification_type - one of the constants in NOTIFICATION_TYPES
# 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
@deprecated Use {#clear_notification_listeners} instead.
# 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
# File lib/optimizely/notification_center.rb, line 149 def notification_count(notification_type) @notifications.include?(notification_type) ? @notifications[notification_type].count : 0 end
Removes previously added notification callback
@param notification_id
@return [Boolean] true if found and removed, false otherwise
# 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
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
@param notification_type - one of the constants in NOTIFICATION_TYPES
@param args - list of arguments to the callback
@api no-doc
# 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| 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
Private Instance Methods
# File lib/optimizely/notification_center.rb, line 155 def notification_type_valid?(notification_type) # Validates notification type # Args: # notification_type: one of the constants in NOTIFICATION_TYPES # Returns true if notification_type is valid, false otherwise unless notification_type @logger.log Logger::ERROR, 'Notification type can not be empty.' return false end unless @notifications.include?(notification_type) @logger.log Logger::ERROR, 'Invalid notification type.' @error_handler.handle_error InvalidNotificationType return false end true end