Class: Optimizely::CustomAttributeConditionEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/custom_attribute_condition_evaluator.rb

Constant Summary collapse

CUSTOM_ATTRIBUTE_CONDITION_TYPE =
'custom_attribute'
EXACT_MATCH_TYPE =

Conditional match types

'exact'
EXISTS_MATCH_TYPE =
'exists'
GREATER_THAN_MATCH_TYPE =
'gt'
LESS_THAN_MATCH_TYPE =
'lt'
SUBSTRING_MATCH_TYPE =
'substring'
EVALUATORS_BY_MATCH_TYPE =
{
  EXACT_MATCH_TYPE => :exact_evaluator,
  EXISTS_MATCH_TYPE => :exists_evaluator,
  GREATER_THAN_MATCH_TYPE => :greater_than_evaluator,
  LESS_THAN_MATCH_TYPE => :less_than_evaluator,
  SUBSTRING_MATCH_TYPE => :substring_evaluator
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_attributes, logger) ⇒ CustomAttributeConditionEvaluator

Returns a new instance of CustomAttributeConditionEvaluator.



42
43
44
45
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 42

def initialize(user_attributes, logger)
  @user_attributes = user_attributes
  @logger = logger
end

Instance Attribute Details

#user_attributesObject (readonly)

Returns the value of attribute user_attributes.



40
41
42
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 40

def user_attributes
  @user_attributes
end

Instance Method Details

#evaluate(leaf_condition) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 47

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 leaf_condition['type'] == CUSTOM_ATTRIBUTE_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']) && condition_match != EXISTS_MATCH_TYPE
    @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? && condition_match != EXISTS_MATCH_TYPE
    @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

  send(EVALUATORS_BY_MATCH_TYPE[condition_match], leaf_condition)
end

#exact_evaluator(condition) ⇒ Object



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
147
148
149
150
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 101

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)
    @logger.log(
      Logger::WARN,
      format(
        Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNEXPECTED_TYPE'],
        condition,
        user_provided_value.class,
        condition['name']
      )
    )
    return nil
  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



152
153
154
155
156
157
158
159
160
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 152

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



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 162

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

#less_than_evaluator(condition) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 176

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

#substring_evaluator(condition) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/optimizely/custom_attribute_condition_evaluator.rb', line 190

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

  unless user_provided_value.is_a?(String)
    @logger.log(
      Logger::WARN,
      format(
        Helpers::Constants::AUDIENCE_EVALUATION_LOGS['UNEXPECTED_TYPE'],
        condition,
        user_provided_value.class,
        condition['name']
      )
    )
    return nil
  end

  user_provided_value.include? condition_value
end