|
| 1 | +# |
| 2 | +# Copyright 2017, Optimizely and contributors |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +module Optimizely |
| 17 | + class NotificationCenter |
| 18 | + attr_reader :notifications |
| 19 | + attr_reader :notification_id |
| 20 | + |
| 21 | + NOTIFICATION_TYPES = { |
| 22 | + ACTIVATE: 'ACTIVATE: experiment, user_id, attributes, variation, event', |
| 23 | + TRACK: 'TRACK: event_key, user_id, attributes, event_tags, event' |
| 24 | + }.freeze |
| 25 | + |
| 26 | + def initialize(logger, error_handler) |
| 27 | + @notification_id = 1 |
| 28 | + @notifications = {} |
| 29 | + NOTIFICATION_TYPES.values.each { |value| @notifications[value] = [] } |
| 30 | + @logger = logger |
| 31 | + @error_handler = error_handler |
| 32 | + end |
| 33 | + |
| 34 | + def add_notification_listener(notification_type, notification_callback) |
| 35 | + # Adds notification callback to the notification center |
| 36 | + |
| 37 | + # Args: |
| 38 | + # notification_type: one of the constants in NOTIFICATION_TYPES |
| 39 | + # notification_callback: function to call when the event is sent |
| 40 | + |
| 41 | + # Returns: |
| 42 | + # notification ID used to remove the notification |
| 43 | + |
| 44 | + return nil unless notification_type_valid?(notification_type) |
| 45 | + |
| 46 | + unless notification_callback |
| 47 | + @logger.log Logger::ERROR, 'Callback can not be empty.' |
| 48 | + return nil |
| 49 | + end |
| 50 | + |
| 51 | + unless notification_callback.is_a? Method |
| 52 | + @logger.log Logger::ERROR, 'Invalid notification callback given.' |
| 53 | + return nil |
| 54 | + end |
| 55 | + |
| 56 | + @notifications[notification_type].each do |notification| |
| 57 | + return -1 if notification[:callback] == notification_callback |
| 58 | + end |
| 59 | + @notifications[notification_type].push(notification_id: @notification_id, callback: notification_callback) |
| 60 | + notification_id = @notification_id |
| 61 | + @notification_id += 1 |
| 62 | + notification_id |
| 63 | + end |
| 64 | + |
| 65 | + def remove_notification_listener(notification_id) |
| 66 | + # Removes previously added notification callback |
| 67 | + |
| 68 | + # Args: |
| 69 | + # notification_id: |
| 70 | + # Returns: |
| 71 | + # The function returns true if found and removed, false otherwise |
| 72 | + unless notification_id |
| 73 | + @logger.log Logger::ERROR, 'Notification ID can not be empty.' |
| 74 | + return nil |
| 75 | + end |
| 76 | + @notifications.each do |key, _array| |
| 77 | + @notifications[key].each do |notification| |
| 78 | + if notification_id == notification[:notification_id] |
| 79 | + @notifications[key].delete(notification_id: notification_id, callback: notification[:callback]) |
| 80 | + return true |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + false |
| 85 | + end |
| 86 | + |
| 87 | + def clear_notifications(notification_type) |
| 88 | + # Removes notifications for a certain notification type |
| 89 | + # |
| 90 | + # Args: |
| 91 | + # notification_type: one of the constants in NOTIFICATION_TYPES |
| 92 | + |
| 93 | + return nil unless notification_type_valid?(notification_type) |
| 94 | + |
| 95 | + @notifications[notification_type] = [] |
| 96 | + @logger.log Logger::INFO, "All callbacks for notification type #{notification_type} have been removed." |
| 97 | + end |
| 98 | + |
| 99 | + def clean_all_notifications |
| 100 | + # Removes all notifications |
| 101 | + @notifications.keys.each { |key| @notifications[key] = [] } |
| 102 | + end |
| 103 | + |
| 104 | + def send_notifications(notification_type, *args) |
| 105 | + # Sends off the notification for the specific event. Uses var args to pass in a |
| 106 | + # arbitrary list of parameters according to which notification type was sent |
| 107 | + |
| 108 | + # Args: |
| 109 | + # notification_type: one of the constants in NOTIFICATION_TYPES |
| 110 | + # args: list of arguments to the callback |
| 111 | + return nil unless notification_type_valid?(notification_type) |
| 112 | + |
| 113 | + @notifications[notification_type].each do |notification| |
| 114 | + begin |
| 115 | + notification_callback = notification[:callback] |
| 116 | + notification_callback.call(*args) |
| 117 | + @logger.log Logger::INFO, "Notification #{notification_type} sent successfully." |
| 118 | + rescue => e |
| 119 | + @logger.log(Logger::ERROR, "Problem calling notify callback. Error: #{e}") |
| 120 | + return nil |
| 121 | + end |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + private |
| 126 | + |
| 127 | + def notification_type_valid?(notification_type) |
| 128 | + # Validates notification type |
| 129 | + |
| 130 | + # Args: |
| 131 | + # notification_type: one of the constants in NOTIFICATION_TYPES |
| 132 | + |
| 133 | + # Returns true if notification_type is valid, false otherwise |
| 134 | + |
| 135 | + unless notification_type |
| 136 | + @logger.log Logger::ERROR, 'Notification type can not be empty.' |
| 137 | + return false |
| 138 | + end |
| 139 | + |
| 140 | + unless @notifications.include?(notification_type) |
| 141 | + @logger.log Logger::ERROR, 'Invalid notification type.' |
| 142 | + @error_handler.handle_error InvalidNotificationType |
| 143 | + return false |
| 144 | + end |
| 145 | + true |
| 146 | + end |
| 147 | + end |
| 148 | +end |
0 commit comments