16
16
# limitations under the License.
17
17
#
18
18
require 'json'
19
- require_relative './custom_attribute_condition_evaluator '
19
+ require_relative './user_condition_evaluator '
20
20
require_relative 'condition_tree_evaluator'
21
21
require_relative 'helpers/constants'
22
22
23
23
module Optimizely
24
24
module Audience
25
25
module_function
26
26
27
- def user_meets_audience_conditions? ( config , experiment , attributes , logger , logging_hash = nil , logging_key = nil )
27
+ def user_meets_audience_conditions? ( config , experiment , user_context , logger , logging_hash = nil , logging_key = nil )
28
28
# Determine for given experiment/rollout rule if user satisfies the audience conditions.
29
29
#
30
30
# config - Representation of the Optimizely project config.
31
31
# experiment - Experiment/Rollout rule in which user is to be bucketed.
32
- # attributes - Hash representing user attributes which will be used in determining if
33
- # the audience conditions are met.
32
+ # user_context - Optimizely user context instance
34
33
# logger - Provides a logger instance.
35
34
# logging_hash - Optional string representing logs hash inside Helpers::Constants.
36
35
# This defaults to 'EXPERIMENT_AUDIENCE_EVALUATION_LOGS'.
@@ -57,12 +56,10 @@ def user_meets_audience_conditions?(config, experiment, attributes, logger, logg
57
56
return true , decide_reasons
58
57
end
59
58
60
- attributes ||= { }
59
+ user_condition_evaluator = UserConditionEvaluator . new ( user_context , logger )
61
60
62
- custom_attr_condition_evaluator = CustomAttributeConditionEvaluator . new ( attributes , logger )
63
-
64
- evaluate_custom_attr = lambda do |condition |
65
- return custom_attr_condition_evaluator . evaluate ( condition )
61
+ evaluate_user_conditions = lambda do |condition |
62
+ return user_condition_evaluator . evaluate ( condition )
66
63
end
67
64
68
65
evaluate_audience = lambda do |audience_id |
@@ -75,7 +72,7 @@ def user_meets_audience_conditions?(config, experiment, attributes, logger, logg
75
72
decide_reasons . push ( message )
76
73
77
74
audience_conditions = JSON . parse ( audience_conditions ) if audience_conditions . is_a? ( String )
78
- result = ConditionTreeEvaluator . evaluate ( audience_conditions , evaluate_custom_attr )
75
+ result = ConditionTreeEvaluator . evaluate ( audience_conditions , evaluate_user_conditions )
79
76
result_str = result . nil? ? 'UNKNOWN' : result . to_s . upcase
80
77
message = format ( logs_hash [ 'AUDIENCE_EVALUATION_RESULT' ] , audience_id , result_str )
81
78
logger . log ( Logger ::DEBUG , message )
@@ -93,5 +90,38 @@ def user_meets_audience_conditions?(config, experiment, attributes, logger, logg
93
90
94
91
[ eval_result , decide_reasons ]
95
92
end
93
+
94
+ def get_segments ( conditions )
95
+ # Return any audience segments from provided conditions.
96
+ #
97
+ # conditions - Nested array of and/or conditions.
98
+ # Example: ['and', operand_1, ['or', operand_2, operand_3]]
99
+ #
100
+ # Returns unique array of segment names.
101
+ conditions = JSON . parse ( conditions ) if conditions . is_a? ( String )
102
+ @parse_segments . call ( conditions ) . uniq
103
+ end
104
+
105
+ @parse_segments = lambda { |conditions |
106
+ # Return any audience segments from provided conditions.
107
+ # Helper function for get_segments.
108
+ #
109
+ # conditions - Nested array of and/or conditions.
110
+ # Example: ['and', operand_1, ['or', operand_2, operand_3]]
111
+ #
112
+ # Returns array of segment names.
113
+ segments = [ ]
114
+
115
+ conditions . each do |condition |
116
+ case condition
117
+ when Array
118
+ segments . concat @parse_segments . call ( condition )
119
+ when Hash
120
+ segments . push ( condition [ 'value' ] ) if condition . fetch ( 'match' , nil ) == 'qualified'
121
+ end
122
+ end
123
+
124
+ segments
125
+ }
96
126
end
97
127
end
0 commit comments