Skip to content

Commit 2a75b42

Browse files
authored
refactor: moved validated forced decision to decision service. (#293)
* moved validated forced decision to decision service * headers updated * find_forced_decision updated to get_forced_decision * added project config * function name corrected
1 parent be870fe commit 2a75b42

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

lib/optimizely.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2016-2021, Optimizely and contributors
4+
# Copyright 2016-2022, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -199,7 +199,7 @@ def decide(user_context, key, decide_options = [])
199199
experiment = nil
200200
decision_source = Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT']
201201
context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(key, nil)
202-
variation, reasons_received = user_context.find_validated_forced_decision(context)
202+
variation, reasons_received = @decision_service.validated_forced_decision(config, context, user_context)
203203
reasons.push(*reasons_received)
204204

205205
if variation

lib/optimizely/decision_service.rb

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2017-2021, Optimizely and contributors
4+
# Copyright 2017-2022, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -265,7 +265,7 @@ def get_variation_from_experiment_rule(project_config, flag_key, rule, user, opt
265265
reasons = []
266266

267267
context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(flag_key, rule['key'])
268-
variation, forced_reasons = user.find_validated_forced_decision(context)
268+
variation, forced_reasons = validated_forced_decision(project_config, context, user)
269269
reasons.push(*forced_reasons)
270270

271271
return [variation['id'], reasons] if variation
@@ -290,7 +290,7 @@ def get_variation_from_delivery_rule(project_config, flag_key, rules, rule_index
290290
skip_to_everyone_else = false
291291
rule = rules[rule_index]
292292
context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(flag_key, rule['key'])
293-
variation, forced_reasons = user.find_validated_forced_decision(context)
293+
variation, forced_reasons = validated_forced_decision(project_config, context, user)
294294
reasons.push(*forced_reasons)
295295

296296
return [variation, skip_to_everyone_else, reasons] if variation
@@ -417,6 +417,28 @@ def get_forced_variation(project_config, experiment_key, user_id)
417417
[variation, decide_reasons]
418418
end
419419

420+
def validated_forced_decision(project_config, context, user_context)
421+
decision = user_context.get_forced_decision(context)
422+
flag_key = context[:flag_key]
423+
rule_key = context[:rule_key]
424+
variation_key = decision ? decision[:variation_key] : decision
425+
reasons = []
426+
target = rule_key ? "flag (#{flag_key}), rule (#{rule_key})" : "flag (#{flag_key})"
427+
if variation_key
428+
variation = project_config.get_variation_from_flag(flag_key, variation_key, 'key')
429+
if variation
430+
reason = "Variation (#{variation_key}) is mapped to #{target} and user (#{user_context.user_id}) in the forced decision map."
431+
reasons.push(reason)
432+
return variation, reasons
433+
else
434+
reason = "Invalid variation is mapped to #{target} and user (#{user_context.user_id}) in the forced decision map."
435+
reasons.push(reason)
436+
end
437+
end
438+
439+
[nil, reasons]
440+
end
441+
420442
private
421443

422444
def get_whitelisted_variation_id(project_config, experiment_id, user_id)

lib/optimizely/optimizely_user_context.rb

+2-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2020, Optimizely and contributors
4+
# Copyright 2020-2022, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ class OptimizelyUserContext
2626
attr_reader :forced_decisions
2727
attr_reader :OptimizelyDecisionContext
2828
attr_reader :OptimizelyForcedDecision
29+
attr_reader :optimizely_client
2930

3031
OptimizelyDecisionContext = Struct.new(:flag_key, :rule_key)
3132
OptimizelyForcedDecision = Struct.new(:variation_key)
@@ -156,28 +157,6 @@ def remove_all_forced_decisions
156157
true
157158
end
158159

159-
def find_validated_forced_decision(context)
160-
decision = find_forced_decision(context)
161-
flag_key = context[:flag_key]
162-
rule_key = context[:rule_key]
163-
variation_key = decision ? decision[:variation_key] : decision
164-
reasons = []
165-
target = rule_key ? "flag (#{flag_key}), rule (#{rule_key})" : "flag (#{flag_key})"
166-
if variation_key
167-
variation = @optimizely_client.get_flag_variation(flag_key, variation_key, 'key')
168-
if variation
169-
reason = "Variation (#{variation_key}) is mapped to #{target} and user (#{@user_id}) in the forced decision map."
170-
reasons.push(reason)
171-
return variation, reasons
172-
else
173-
reason = "Invalid variation is mapped to #{target} and user (#{@user_id}) in the forced decision map."
174-
reasons.push(reason)
175-
end
176-
end
177-
178-
[nil, reasons]
179-
end
180-
181160
# Track an event
182161
#
183162
# @param event_key - Event key representing the event which needs to be recorded.

0 commit comments

Comments
 (0)