Class: Optimizely::UserConditionEvaluator
- Inherits:
-
Object
- Object
- Optimizely::UserConditionEvaluator
- Defined in:
- lib/optimizely/user_condition_evaluator.rb
Constant Summary collapse
- CONDITION_TYPES =
%w[custom_attribute third_party_dimension].freeze
- EXACT_MATCH_TYPE =
Conditional match types
'exact'
- EXISTS_MATCH_TYPE =
'exists'
- GREATER_THAN_MATCH_TYPE =
'gt'
- GREATER_EQUAL_MATCH_TYPE =
'ge'
- LESS_THAN_MATCH_TYPE =
'lt'
- LESS_EQUAL_MATCH_TYPE =
'le'
- SUBSTRING_MATCH_TYPE =
'substring'
- SEMVER_EQ =
'semver_eq'
- SEMVER_GE =
'semver_ge'
- SEMVER_GT =
'semver_gt'
- SEMVER_LE =
'semver_le'
- SEMVER_LT =
'semver_lt'
- QUALIFIED_MATCH_TYPE =
'qualified'
- EVALUATORS_BY_MATCH_TYPE =
{ EXACT_MATCH_TYPE => :exact_evaluator, EXISTS_MATCH_TYPE => :exists_evaluator, GREATER_THAN_MATCH_TYPE => :greater_than_evaluator, GREATER_EQUAL_MATCH_TYPE => :greater_than_or_equal_evaluator, LESS_THAN_MATCH_TYPE => :less_than_evaluator, LESS_EQUAL_MATCH_TYPE => :less_than_or_equal_evaluator, SUBSTRING_MATCH_TYPE => :substring_evaluator, SEMVER_EQ => :semver_equal_evaluator, SEMVER_GE => :semver_greater_than_or_equal_evaluator, SEMVER_GT => :semver_greater_than_evaluator, SEMVER_LE => :semver_less_than_or_equal_evaluator, SEMVER_LT => :semver_less_than_evaluator, QUALIFIED_MATCH_TYPE => :qualified_evaluator }.freeze
Instance Attribute Summary collapse
-
#user_attributes ⇒ Object
readonly
Returns the value of attribute user_attributes.
Instance Method Summary collapse
- #evaluate(leaf_condition) ⇒ Object
- #exact_evaluator(condition) ⇒ Object
- #exists_evaluator(condition) ⇒ Object
- #greater_than_evaluator(condition) ⇒ Object
- #greater_than_or_equal_evaluator(condition) ⇒ Object
-
#initialize(user_context, logger) ⇒ UserConditionEvaluator
constructor
A new instance of UserConditionEvaluator.
- #less_than_evaluator(condition) ⇒ Object
- #less_than_or_equal_evaluator(condition) ⇒ Object
- #qualified_evaluator(condition) ⇒ Object
- #semver_equal_evaluator(condition) ⇒ Object
- #semver_greater_than_evaluator(condition) ⇒ Object
- #semver_greater_than_or_equal_evaluator(condition) ⇒ Object
- #semver_less_than_evaluator(condition) ⇒ Object
- #semver_less_than_or_equal_evaluator(condition) ⇒ Object
- #substring_evaluator(condition) ⇒ Object
Constructor Details
#initialize(user_context, logger) ⇒ UserConditionEvaluator
Returns a new instance of UserConditionEvaluator.
60 61 62 63 64 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 60 def initialize(user_context, logger) @user_context = user_context @user_attributes = user_context.user_attributes @logger = logger end |
Instance Attribute Details
#user_attributes ⇒ Object (readonly)
Returns the value of attribute user_attributes.
58 59 60 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 58 def user_attributes @user_attributes end |
Instance Method Details
#evaluate(leaf_condition) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 66 def evaluate(leaf_condition) # Top level method to evaluate audience conditions. # # conditions - Nested array of and/or conditions. # Example: ['and', operand_1, ['or', operand_2, operand_3]] # # Returns boolean if the given user attributes match/don't match the given conditions, # nil if the given conditions can't be evaluated. unless CONDITION_TYPES.include? leaf_condition['type'] @logger.log( Logger::WARN, format(Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNKNOWN_CONDITION_TYPE'], leaf_condition) ) return nil end condition_match = leaf_condition['match'] || EXACT_MATCH_TYPE if !@user_attributes.key?(leaf_condition['name']) && ![EXISTS_MATCH_TYPE, QUALIFIED_MATCH_TYPE].include?(condition_match) @logger.log( Logger::DEBUG, format( Helpers::Constants::AUDIENCE_EVALUATION_LOGS['MISSING_ATTRIBUTE_VALUE'], leaf_condition, leaf_condition['name'] ) ) return nil end if @user_attributes[leaf_condition['name']].nil? && ![EXISTS_MATCH_TYPE, QUALIFIED_MATCH_TYPE].include?(condition_match) @logger.log( Logger::DEBUG, format( Helpers::Constants::AUDIENCE_EVALUATION_LOGS['NULL_ATTRIBUTE_VALUE'], leaf_condition, leaf_condition['name'] ) ) return nil end unless EVALUATORS_BY_MATCH_TYPE.include?(condition_match) @logger.log( Logger::WARN, format(Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNKNOWN_MATCH_TYPE'], leaf_condition) ) return nil end begin send(EVALUATORS_BY_MATCH_TYPE[condition_match], leaf_condition) rescue InvalidAttributeType condition_name = leaf_condition['name'] user_value = @user_attributes[condition_name] @logger.log( Logger::WARN, format( Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNEXPECTED_TYPE'], leaf_condition, user_value.class, condition_name ) ) nil rescue InvalidSemanticVersion condition_name = leaf_condition['name'] @logger.log( Logger::WARN, format( Helpers::Constants::AUDIENCE_EVALUATION_LOGS['INVALID_SEMANTIC_VERSION'], leaf_condition, condition_name ) ) nil end end |
#exact_evaluator(condition) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 148 def exact_evaluator(condition) # Evaluate the given exact match condition for the given user attributes. # # Returns boolean true if numbers values matched, i.e 2 is equal to 2.0 # true if the user attribute value is equal (===) to the condition value, # false if the user attribute value is not equal (!==) to the condition value, # nil if the condition value or user attribute value has an invalid type, # or if there is a mismatch between the user attribute type and the condition value type. condition_value = condition['value'] user_provided_value = @user_attributes[condition['name']] if !value_type_valid_for_exact_conditions?(condition_value) || (condition_value.is_a?(Numeric) && !Helpers::Validator.finite_number?(condition_value)) @logger.log( Logger::WARN, format(Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNKNOWN_CONDITION_VALUE'], condition) ) return nil end if !value_type_valid_for_exact_conditions?(user_provided_value) || !Helpers::Validator.same_types?(condition_value, user_provided_value) raise InvalidAttributeType end if user_provided_value.is_a?(Numeric) && !Helpers::Validator.finite_number?(user_provided_value) @logger.log( Logger::WARN, format( Helpers::Constants::AUDIENCE_EVALUATION_LOGS['INFINITE_ATTRIBUTE_VALUE'], condition, condition['name'] ) ) return nil end condition_value == user_provided_value end |
#exists_evaluator(condition) ⇒ Object
190 191 192 193 194 195 196 197 198 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 190 def exists_evaluator(condition) # Evaluate the given exists match condition for the given user attributes. # Returns boolean true if both: # 1) the user attributes have a value for the given condition, and # 2) the user attribute value is neither nil nor undefined # Returns false otherwise !@user_attributes[condition['name']].nil? end |
#greater_than_evaluator(condition) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 200 def greater_than_evaluator(condition) # Evaluate the given greater than match condition for the given user attributes. # Returns boolean true if the user attribute value is greater than the condition value, # false if the user attribute value is less than or equal to the condition value, # nil if the condition value isn't a number or the user attribute value isn't a number. condition_value = condition['value'] user_provided_value = @user_attributes[condition['name']] return nil unless valid_numeric_values?(user_provided_value, condition_value, condition) user_provided_value > condition_value end |
#greater_than_or_equal_evaluator(condition) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 214 def greater_than_or_equal_evaluator(condition) # Evaluate the given greater than or equal match condition for the given user attributes. # Returns boolean true if the user attribute value is greater than or equal to the condition value, # false if the user attribute value is less than the condition value, # nil if the condition value isn't a number or the user attribute value isn't a number. condition_value = condition['value'] user_provided_value = @user_attributes[condition['name']] return nil unless valid_numeric_values?(user_provided_value, condition_value, condition) user_provided_value >= condition_value end |
#less_than_evaluator(condition) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 228 def less_than_evaluator(condition) # Evaluate the given less than match condition for the given user attributes. # Returns boolean true if the user attribute value is less than the condition value, # false if the user attribute value is greater than or equal to the condition value, # nil if the condition value isn't a number or the user attribute value isn't a number. condition_value = condition['value'] user_provided_value = @user_attributes[condition['name']] return nil unless valid_numeric_values?(user_provided_value, condition_value, condition) user_provided_value < condition_value end |
#less_than_or_equal_evaluator(condition) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 242 def less_than_or_equal_evaluator(condition) # Evaluate the given less than or equal match condition for the given user attributes. # Returns boolean true if the user attribute value is less than or equal to the condition value, # false if the user attribute value is greater than the condition value, # nil if the condition value isn't a number or the user attribute value isn't a number. condition_value = condition['value'] user_provided_value = @user_attributes[condition['name']] return nil unless valid_numeric_values?(user_provided_value, condition_value, condition) user_provided_value <= condition_value end |
#qualified_evaluator(condition) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 333 def qualified_evaluator(condition) # Evaluate the given match condition for the given user qualified segments. # Returns boolean true if condition value is in the user's qualified segments, # false if the condition value is not in the user's qualified segments, # nil if the condition value isn't a string. condition_value = condition['value'] unless condition_value.is_a?(String) @logger.log( Logger::WARN, format(Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNKNOWN_CONDITION_VALUE'], condition) ) return nil end @user_context.qualified_for?(condition_value) end |
#semver_equal_evaluator(condition) ⇒ Object
278 279 280 281 282 283 284 285 286 287 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 278 def semver_equal_evaluator(condition) # Evaluate the given semantic version equal match target version for the user version. # Returns boolean true if the user version is equal to the target version, # false if the user version is not equal to the target version target_version = condition['value'] user_version = @user_attributes[condition['name']] SemanticVersion.compare_user_version_with_target_version(target_version, user_version).zero? end |
#semver_greater_than_evaluator(condition) ⇒ Object
289 290 291 292 293 294 295 296 297 298 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 289 def semver_greater_than_evaluator(condition) # Evaluate the given semantic version greater than match target version for the user version. # Returns boolean true if the user version is greater than the target version, # false if the user version is less than or equal to the target version target_version = condition['value'] user_version = @user_attributes[condition['name']] SemanticVersion.compare_user_version_with_target_version(target_version, user_version).positive? end |
#semver_greater_than_or_equal_evaluator(condition) ⇒ Object
300 301 302 303 304 305 306 307 308 309 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 300 def semver_greater_than_or_equal_evaluator(condition) # Evaluate the given semantic version greater than or equal to match target version for the user version. # Returns boolean true if the user version is greater than or equal to the target version, # false if the user version is less than the target version target_version = condition['value'] user_version = @user_attributes[condition['name']] SemanticVersion.compare_user_version_with_target_version(target_version, user_version) >= 0 end |
#semver_less_than_evaluator(condition) ⇒ Object
311 312 313 314 315 316 317 318 319 320 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 311 def semver_less_than_evaluator(condition) # Evaluate the given semantic version less than match target version for the user version. # Returns boolean true if the user version is less than the target version, # false if the user version is greater than or equal to the target version target_version = condition['value'] user_version = @user_attributes[condition['name']] SemanticVersion.compare_user_version_with_target_version(target_version, user_version).negative? end |
#semver_less_than_or_equal_evaluator(condition) ⇒ Object
322 323 324 325 326 327 328 329 330 331 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 322 def semver_less_than_or_equal_evaluator(condition) # Evaluate the given semantic version less than or equal to match target version for the user version. # Returns boolean true if the user version is less than or equal to the target version, # false if the user version is greater than the target version target_version = condition['value'] user_version = @user_attributes[condition['name']] SemanticVersion.compare_user_version_with_target_version(target_version, user_version) <= 0 end |
#substring_evaluator(condition) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/optimizely/user_condition_evaluator.rb', line 256 def substring_evaluator(condition) # Evaluate the given substring match condition for the given user attributes. # Returns boolean true if the condition value is a substring of the user attribute value, # false if the condition value is not a substring of the user attribute value, # nil if the condition value isn't a string or the user attribute value isn't a string. condition_value = condition['value'] user_provided_value = @user_attributes[condition['name']] unless condition_value.is_a?(String) @logger.log( Logger::WARN, format(Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNKNOWN_CONDITION_VALUE'], condition) ) return nil end raise InvalidAttributeType unless user_provided_value.is_a?(String) user_provided_value.include? condition_value end |