|
19 | 19 | require_relative 'optimizely/event_builder'
|
20 | 20 | require_relative 'optimizely/event_dispatcher'
|
21 | 21 | require_relative 'optimizely/exceptions'
|
| 22 | +require_relative 'optimizely/helpers/constants' |
22 | 23 | require_relative 'optimizely/helpers/group'
|
23 | 24 | require_relative 'optimizely/helpers/validator'
|
| 25 | +require_relative 'optimizely/helpers/variable_type' |
24 | 26 | require_relative 'optimizely/logger'
|
25 | 27 | require_relative 'optimizely/project_config'
|
26 | 28 |
|
@@ -237,8 +239,154 @@ def is_feature_enabled(feature_flag_key, user_id, attributes = nil)
|
237 | 239 | false
|
238 | 240 | end
|
239 | 241 |
|
| 242 | + def get_feature_variable_string(feature_flag_key, variable_key, user_id, attributes = nil) |
| 243 | + # Get the String value of the specified variable in the feature flag. |
| 244 | + # |
| 245 | + # feature_flag_key - String key of feature flag the variable belongs to |
| 246 | + # variable_key - String key of variable for which we are getting the string value |
| 247 | + # user_id - String user ID |
| 248 | + # attributes - Hash representing visitor attributes and values which need to be recorded. |
| 249 | + # |
| 250 | + # Returns the string variable value. |
| 251 | + # Returns nil if the feature flag or variable are not found. |
| 252 | + |
| 253 | + variable_value = get_feature_variable_for_type( |
| 254 | + feature_flag_key, |
| 255 | + variable_key, |
| 256 | + Optimizely::Helpers::Constants::VARIABLE_TYPES["STRING"], |
| 257 | + user_id, |
| 258 | + attributes |
| 259 | + ) |
| 260 | + |
| 261 | + return variable_value |
| 262 | + end |
| 263 | + |
| 264 | + def get_feature_variable_boolean(feature_flag_key, variable_key, user_id, attributes = nil) |
| 265 | + # Get the Boolean value of the specified variable in the feature flag. |
| 266 | + # |
| 267 | + # feature_flag_key - String key of feature flag the variable belongs to |
| 268 | + # variable_key - String key of variable for which we are getting the string value |
| 269 | + # user_id - String user ID |
| 270 | + # attributes - Hash representing visitor attributes and values which need to be recorded. |
| 271 | + # |
| 272 | + # Returns the boolean variable value. |
| 273 | + # Returns nil if the feature flag or variable are not found. |
| 274 | + |
| 275 | + variable_value = get_feature_variable_for_type( |
| 276 | + feature_flag_key, |
| 277 | + variable_key, |
| 278 | + Optimizely::Helpers::Constants::VARIABLE_TYPES["BOOLEAN"], |
| 279 | + user_id, |
| 280 | + attributes |
| 281 | + ) |
| 282 | + |
| 283 | + return variable_value |
| 284 | + end |
| 285 | + |
| 286 | + def get_feature_variable_double(feature_flag_key, variable_key, user_id, attributes = nil) |
| 287 | + # Get the Double value of the specified variable in the feature flag. |
| 288 | + # |
| 289 | + # feature_flag_key - String key of feature flag the variable belongs to |
| 290 | + # variable_key - String key of variable for which we are getting the string value |
| 291 | + # user_id - String user ID |
| 292 | + # attributes - Hash representing visitor attributes and values which need to be recorded. |
| 293 | + # |
| 294 | + # Returns the double variable value. |
| 295 | + # Returns nil if the feature flag or variable are not found. |
| 296 | + |
| 297 | + variable_value = get_feature_variable_for_type( |
| 298 | + feature_flag_key, |
| 299 | + variable_key, |
| 300 | + Optimizely::Helpers::Constants::VARIABLE_TYPES["DOUBLE"], |
| 301 | + user_id, |
| 302 | + attributes |
| 303 | + ) |
| 304 | + |
| 305 | + return variable_value |
| 306 | + end |
| 307 | + |
| 308 | + def get_feature_variable_integer(feature_flag_key, variable_key, user_id, attributes = nil) |
| 309 | + # Get the Integer value of the specified variable in the feature flag. |
| 310 | + # |
| 311 | + # feature_flag_key - String key of feature flag the variable belongs to |
| 312 | + # variable_key - String key of variable for which we are getting the string value |
| 313 | + # user_id - String user ID |
| 314 | + # attributes - Hash representing visitor attributes and values which need to be recorded. |
| 315 | + # |
| 316 | + # Returns the integer variable value. |
| 317 | + # Returns nil if the feature flag or variable are not found. |
| 318 | + |
| 319 | + variable_value = get_feature_variable_for_type( |
| 320 | + feature_flag_key, |
| 321 | + variable_key, |
| 322 | + Optimizely::Helpers::Constants::VARIABLE_TYPES["INTEGER"], |
| 323 | + user_id, |
| 324 | + attributes |
| 325 | + ) |
| 326 | + |
| 327 | + return variable_value |
| 328 | + end |
| 329 | + |
240 | 330 | private
|
241 | 331 |
|
| 332 | + def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type, user_id, attributes = nil) |
| 333 | + # Get the variable value for the given feature variable and cast it to the specified type |
| 334 | + # The default value is returned if the feature flag is not enabled for the user. |
| 335 | + # |
| 336 | + # feature_flag_key - String key of feature flag the variable belongs to |
| 337 | + # variable_key - String key of variable for which we are getting the string value |
| 338 | + # variable_type - String requested type for feature variable |
| 339 | + # user_id - String user ID |
| 340 | + # attributes - Hash representing visitor attributes and values which need to be recorded. |
| 341 | + # |
| 342 | + # Returns the type-casted variable value. |
| 343 | + # Returns nil if the feature flag or variable are not found. |
| 344 | + |
| 345 | + feature_flag = @config.get_feature_flag_from_key(feature_flag_key) |
| 346 | + unless feature_flag |
| 347 | + @logger.log(Logger::INFO, "No feature flag was found for key '#{feature_flag_key}'.") |
| 348 | + return nil |
| 349 | + end |
| 350 | + |
| 351 | + variable_value = nil |
| 352 | + variable = @config.get_feature_variable(feature_flag, variable_key) |
| 353 | + unless variable.nil? |
| 354 | + variable_value = variable['defaultValue'] |
| 355 | + |
| 356 | + decision = @decision_service.get_variation_for_feature(feature_flag, user_id, attributes) |
| 357 | + unless decision |
| 358 | + @logger.log(Logger::INFO, |
| 359 | + "User '#{user_id}' was not bucketed into any variation for feature flag '#{feature_flag_key}'. Returning the default variable value '#{variable_value}'.") |
| 360 | + else |
| 361 | + variation = decision['variation'] |
| 362 | + variation_variable_usages = @config.variation_id_to_variable_usage_map[variation['id']] |
| 363 | + variable_id = variable['id'] |
| 364 | + unless variation_variable_usages.key?(variable_id) |
| 365 | + variation_key = variation['key'] |
| 366 | + @logger.log(Logger::DEBUG, |
| 367 | + "Variable '#{variable_key}' is not used in variation '#{variation_key}'. Returning the default variable value '#{variable_value}'." |
| 368 | + ) |
| 369 | + else |
| 370 | + variable_value = variation_variable_usages[variable_id]['value'] |
| 371 | + @logger.log(Logger::INFO, |
| 372 | + "Got variable value '#{variable_value}' for variable '#{variable_key}' of feature flag '#{feature_flag_key}'.") |
| 373 | + end |
| 374 | + end |
| 375 | + end |
| 376 | + |
| 377 | + unless variable_value.nil? |
| 378 | + actual_variable_type = variable['type'] |
| 379 | + unless variable_type == actual_variable_type |
| 380 | + @logger.log(Logger::WARN, |
| 381 | + "Requested variable type '#{variable_type}' but variable '#{variable_key}' is of type '#{actual_variable_type}'.") |
| 382 | + end |
| 383 | + |
| 384 | + variable_value = Helpers::VariableType.cast_value_to_type(variable_value, variable_type, @logger) |
| 385 | + end |
| 386 | + |
| 387 | + return variable_value |
| 388 | + end |
| 389 | + |
242 | 390 | def get_valid_experiments_for_event(event_key, user_id, attributes)
|
243 | 391 | # Get the experiments that we should be tracking for the given event.
|
244 | 392 | #
|
|
0 commit comments