1616# limitations under the License.
1717#
1818require 'json'
19- require_relative './custom_attribute_condition_evaluator '
19+ require_relative './user_condition_evaluator '
2020require_relative 'condition_tree_evaluator'
2121require_relative 'helpers/constants'
2222
2323module Optimizely
2424 module Audience
2525 module_function
2626
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 )
2828 # Determine for given experiment/rollout rule if user satisfies the audience conditions.
2929 #
3030 # config - Representation of the Optimizely project config.
3131 # 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
3433 # logger - Provides a logger instance.
3534 # logging_hash - Optional string representing logs hash inside Helpers::Constants.
3635 # This defaults to 'EXPERIMENT_AUDIENCE_EVALUATION_LOGS'.
@@ -57,12 +56,10 @@ def user_meets_audience_conditions?(config, experiment, attributes, logger, logg
5756 return true , decide_reasons
5857 end
5958
60- attributes ||= { }
59+ user_condition_evaluator = UserConditionEvaluator . new ( user_context , logger )
6160
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 )
6663 end
6764
6865 evaluate_audience = lambda do |audience_id |
@@ -75,7 +72,7 @@ def user_meets_audience_conditions?(config, experiment, attributes, logger, logg
7572 decide_reasons . push ( message )
7673
7774 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 )
7976 result_str = result . nil? ? 'UNKNOWN' : result . to_s . upcase
8077 message = format ( logs_hash [ 'AUDIENCE_EVALUATION_RESULT' ] , audience_id , result_str )
8178 logger . log ( Logger ::DEBUG , message )
@@ -93,5 +90,38 @@ def user_meets_audience_conditions?(config, experiment, attributes, logger, logg
9390
9491 [ eval_result , decide_reasons ]
9592 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+ }
96126 end
97127end
0 commit comments