|
19 | 19 | require_relative 'optimizely/config/datafile_project_config'
|
20 | 20 | require_relative 'optimizely/config_manager/http_project_config_manager'
|
21 | 21 | require_relative 'optimizely/config_manager/static_project_config_manager'
|
| 22 | +require_relative 'optimizely/decide/optimizely_decide_option' |
| 23 | +require_relative 'optimizely/decide/optimizely_decision' |
| 24 | +require_relative 'optimizely/decide/optimizely_decision_message' |
22 | 25 | require_relative 'optimizely/decision_service'
|
23 | 26 | require_relative 'optimizely/error_handler'
|
24 | 27 | require_relative 'optimizely/event_builder'
|
|
34 | 37 | require_relative 'optimizely/logger'
|
35 | 38 | require_relative 'optimizely/notification_center'
|
36 | 39 | require_relative 'optimizely/optimizely_config'
|
| 40 | +require_relative 'optimizely/optimizely_user_context' |
37 | 41 |
|
38 | 42 | module Optimizely
|
39 | 43 | class Project
|
| 44 | + include Optimizely::Decide |
| 45 | + |
40 | 46 | attr_reader :notification_center
|
41 | 47 | # @api no-doc
|
42 | 48 | attr_reader :config_manager, :decision_service, :error_handler, :event_dispatcher,
|
@@ -67,12 +73,21 @@ def initialize(
|
67 | 73 | sdk_key = nil,
|
68 | 74 | config_manager = nil,
|
69 | 75 | notification_center = nil,
|
70 |
| - event_processor = nil |
| 76 | + event_processor = nil, |
| 77 | + default_decide_options = [] |
71 | 78 | )
|
72 | 79 | @logger = logger || NoOpLogger.new
|
73 | 80 | @error_handler = error_handler || NoOpErrorHandler.new
|
74 | 81 | @event_dispatcher = event_dispatcher || EventDispatcher.new(logger: @logger, error_handler: @error_handler)
|
75 | 82 | @user_profile_service = user_profile_service
|
| 83 | + @default_decide_options = [] |
| 84 | + |
| 85 | + if default_decide_options.is_a? Array |
| 86 | + @default_decide_options = default_decide_options.clone |
| 87 | + else |
| 88 | + @logger.log(Logger::DEBUG, 'Provided default decide options is not an array.') |
| 89 | + @default_decide_options = [] |
| 90 | + end |
76 | 91 |
|
77 | 92 | begin
|
78 | 93 | validate_instantiation_options
|
@@ -107,6 +122,174 @@ def initialize(
|
107 | 122 | end
|
108 | 123 | end
|
109 | 124 |
|
| 125 | + # Create a context of the user for which decision APIs will be called. |
| 126 | + # |
| 127 | + # A user context will be created successfully even when the SDK is not fully configured yet. |
| 128 | + # |
| 129 | + # @param user_id - The user ID to be used for bucketing. |
| 130 | + # @param attributes - A Hash representing user attribute names and values. |
| 131 | + # |
| 132 | + # @return [OptimizelyUserContext] An OptimizelyUserContext associated with this OptimizelyClient. |
| 133 | + # @return [nil] If user attributes are not in valid format. |
| 134 | + |
| 135 | + def create_user_context(user_id, attributes = nil) |
| 136 | + # We do not check for is_valid here as a user context can be created successfully |
| 137 | + # even when the SDK is not fully configured. |
| 138 | + |
| 139 | + # validate user_id |
| 140 | + return nil unless Optimizely::Helpers::Validator.inputs_valid?( |
| 141 | + { |
| 142 | + user_id: user_id |
| 143 | + }, @logger, Logger::ERROR |
| 144 | + ) |
| 145 | + |
| 146 | + # validate attributes |
| 147 | + return nil unless user_inputs_valid?(attributes) |
| 148 | + |
| 149 | + user_context = OptimizelyUserContext.new(self, user_id, attributes) |
| 150 | + user_context |
| 151 | + end |
| 152 | + |
| 153 | + def decide(user_context, key, decide_options = []) |
| 154 | + # raising on user context as it is internal and not provided directly by the user. |
| 155 | + raise if user_context.class != OptimizelyUserContext |
| 156 | + |
| 157 | + reasons = [] |
| 158 | + |
| 159 | + # check if SDK is ready |
| 160 | + unless is_valid |
| 161 | + @logger.log(Logger::ERROR, InvalidProjectConfigError.new('decide').message) |
| 162 | + reasons.push(OptimizelyDecisionMessage::SDK_NOT_READY) |
| 163 | + return OptimizelyDecision.new(flag_key: key, user_context: user_context, reasons: reasons) |
| 164 | + end |
| 165 | + |
| 166 | + # validate that key is a string |
| 167 | + unless key.is_a?(String) |
| 168 | + @logger.log(Logger::ERROR, 'Provided key is invalid') |
| 169 | + reasons.push(format(OptimizelyDecisionMessage::FLAG_KEY_INVALID, key)) |
| 170 | + return OptimizelyDecision.new(flag_key: key, user_context: user_context, reasons: reasons) |
| 171 | + end |
| 172 | + |
| 173 | + # validate that key maps to a feature flag |
| 174 | + config = project_config |
| 175 | + feature_flag = config.get_feature_flag_from_key(key) |
| 176 | + unless feature_flag |
| 177 | + @logger.log(Logger::ERROR, "No feature flag was found for key '#{key}'.") |
| 178 | + reasons.push(format(OptimizelyDecisionMessage::FLAG_KEY_INVALID, key)) |
| 179 | + return OptimizelyDecision.new(flag_key: key, user_context: user_context, reasons: reasons) |
| 180 | + end |
| 181 | + |
| 182 | + # merge decide_options and default_decide_options |
| 183 | + if decide_options.is_a? Array |
| 184 | + decide_options += @default_decide_options |
| 185 | + else |
| 186 | + @logger.log(Logger::DEBUG, 'Provided decide options is not an array. Using default decide options.') |
| 187 | + decide_options = @default_decide_options |
| 188 | + end |
| 189 | + |
| 190 | + # Create Optimizely Decision Result. |
| 191 | + user_id = user_context.user_id |
| 192 | + attributes = user_context.user_attributes |
| 193 | + variation_key = nil |
| 194 | + feature_enabled = false |
| 195 | + rule_key = nil |
| 196 | + flag_key = key |
| 197 | + all_variables = {} |
| 198 | + decision_event_dispatched = false |
| 199 | + experiment = nil |
| 200 | + decision_source = Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT'] |
| 201 | + |
| 202 | + decision = @decision_service.get_variation_for_feature(config, feature_flag, user_id, attributes, decide_options, reasons) |
| 203 | + |
| 204 | + # Send impression event if Decision came from a feature test and decide options doesn't include disableDecisionEvent |
| 205 | + if decision.is_a?(Optimizely::DecisionService::Decision) |
| 206 | + experiment = decision.experiment |
| 207 | + rule_key = experiment['key'] |
| 208 | + variation = decision['variation'] |
| 209 | + variation_key = variation['key'] |
| 210 | + feature_enabled = variation['featureEnabled'] |
| 211 | + decision_source = decision.source |
| 212 | + end |
| 213 | + |
| 214 | + unless decide_options.include? OptimizelyDecideOption::DISABLE_DECISION_EVENT |
| 215 | + if decision_source == Optimizely::DecisionService::DECISION_SOURCES['FEATURE_TEST'] || config.send_flag_decisions |
| 216 | + send_impression(config, experiment, variation_key || '', flag_key, rule_key || '', feature_enabled, decision_source, user_id, attributes) |
| 217 | + decision_event_dispatched = true |
| 218 | + end |
| 219 | + end |
| 220 | + |
| 221 | + # Generate all variables map if decide options doesn't include excludeVariables |
| 222 | + unless decide_options.include? OptimizelyDecideOption::EXCLUDE_VARIABLES |
| 223 | + feature_flag['variables'].each do |variable| |
| 224 | + variable_value = get_feature_variable_for_variation(key, feature_enabled, variation, variable, user_id) |
| 225 | + all_variables[variable['key']] = Helpers::VariableType.cast_value_to_type(variable_value, variable['type'], @logger) |
| 226 | + end |
| 227 | + end |
| 228 | + |
| 229 | + should_include_reasons = decide_options.include? OptimizelyDecideOption::INCLUDE_REASONS |
| 230 | + |
| 231 | + # Send notification |
| 232 | + @notification_center.send_notifications( |
| 233 | + NotificationCenter::NOTIFICATION_TYPES[:DECISION], |
| 234 | + Helpers::Constants::DECISION_NOTIFICATION_TYPES['FLAG'], |
| 235 | + user_id, (attributes || {}), |
| 236 | + flag_key: flag_key, |
| 237 | + enabled: feature_enabled, |
| 238 | + variables: all_variables, |
| 239 | + variation_key: variation_key, |
| 240 | + rule_key: rule_key, |
| 241 | + reasons: should_include_reasons ? reasons : [], |
| 242 | + decision_event_dispatched: decision_event_dispatched |
| 243 | + ) |
| 244 | + |
| 245 | + OptimizelyDecision.new( |
| 246 | + variation_key: variation_key, |
| 247 | + enabled: feature_enabled, |
| 248 | + variables: all_variables, |
| 249 | + rule_key: rule_key, |
| 250 | + flag_key: flag_key, |
| 251 | + user_context: user_context, |
| 252 | + reasons: should_include_reasons ? reasons : [] |
| 253 | + ) |
| 254 | + end |
| 255 | + |
| 256 | + def decide_all(user_context, decide_options = []) |
| 257 | + # raising on user context as it is internal and not provided directly by the user. |
| 258 | + raise if user_context.class != OptimizelyUserContext |
| 259 | + |
| 260 | + # check if SDK is ready |
| 261 | + unless is_valid |
| 262 | + @logger.log(Logger::ERROR, InvalidProjectConfigError.new('decide_all').message) |
| 263 | + return {} |
| 264 | + end |
| 265 | + |
| 266 | + keys = [] |
| 267 | + project_config.feature_flags.each do |feature_flag| |
| 268 | + keys.push(feature_flag['key']) |
| 269 | + end |
| 270 | + decide_for_keys(user_context, keys, decide_options) |
| 271 | + end |
| 272 | + |
| 273 | + def decide_for_keys(user_context, keys, decide_options = []) |
| 274 | + # raising on user context as it is internal and not provided directly by the user. |
| 275 | + raise if user_context.class != OptimizelyUserContext |
| 276 | + |
| 277 | + # check if SDK is ready |
| 278 | + unless is_valid |
| 279 | + @logger.log(Logger::ERROR, InvalidProjectConfigError.new('decide_for_keys').message) |
| 280 | + return {} |
| 281 | + end |
| 282 | + |
| 283 | + enabled_flags_only = (!decide_options.nil? && (decide_options.include? OptimizelyDecideOption::ENABLED_FLAGS_ONLY)) || (@default_decide_options.include? OptimizelyDecideOption::ENABLED_FLAGS_ONLY) |
| 284 | + |
| 285 | + decisions = {} |
| 286 | + keys.each do |key| |
| 287 | + decision = decide(user_context, key, decide_options) |
| 288 | + decisions[key] = decision unless enabled_flags_only && !decision.enabled |
| 289 | + end |
| 290 | + decisions |
| 291 | + end |
| 292 | + |
110 | 293 | # Buckets visitor and sends impression event to Optimizely.
|
111 | 294 | #
|
112 | 295 | # @param experiment_key - Experiment which needs to be activated.
|
|
0 commit comments