Skip to content

Commit 5f45ea9

Browse files
rashidspaliabbasrizvi
authored andcommitted
feat(api): Feature variable APIs return default variable value when featureEnabled property is false. (#162)
1 parent 511b93d commit 5f45ea9

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

.rubocop.yml

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Metrics/AbcSize:
1212
Metrics/BlockLength:
1313
Enabled: false
1414

15+
Metrics/BlockNesting:
16+
Enabled: false
17+
1518
Metrics/ClassLength:
1619
Enabled: false
1720

lib/optimizely.rb

+12-7
Original file line numberDiff line numberDiff line change
@@ -499,15 +499,20 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
499499
source_string = Optimizely::DecisionService::DECISION_SOURCE_EXPERIMENT
500500
end
501501
feature_enabled = variation['featureEnabled']
502-
variation_variable_usages = @config.variation_id_to_variable_usage_map[variation['id']]
503-
variable_id = variable['id']
504-
if variation_variable_usages&.key?(variable_id)
505-
variable_value = variation_variable_usages[variable_id]['value']
506-
@logger.log(Logger::INFO,
507-
"Got variable value '#{variable_value}' for variable '#{variable_key}' of feature flag '#{feature_flag_key}'.")
502+
if feature_enabled == true
503+
variation_variable_usages = @config.variation_id_to_variable_usage_map[variation['id']]
504+
variable_id = variable['id']
505+
if variation_variable_usages&.key?(variable_id)
506+
variable_value = variation_variable_usages[variable_id]['value']
507+
@logger.log(Logger::INFO,
508+
"Got variable value '#{variable_value}' for variable '#{variable_key}' of feature flag '#{feature_flag_key}'.")
509+
else
510+
@logger.log(Logger::DEBUG,
511+
"Variable '#{variable_key}' is not used in variation '#{variation['key']}'. Returning the default variable value '#{variable_value}'.")
512+
end
508513
else
509514
@logger.log(Logger::DEBUG,
510-
"Variable '#{variable_key}' is not used in variation '#{variation['key']}'. Returning the default variable value '#{variable_value}'.")
515+
"Feature '#{feature_flag_key}' for variation '#{variation['key']}' is not enabled. Returning the default variable value '#{variable_value}'.")
511516
end
512517
else
513518
@logger.log(Logger::INFO,

spec/project_spec.rb

+17-7
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,7 @@ class InvalidErrorHandler; end
20352035
user_id = 'test_user'
20362036
user_attributes = {}
20372037

2038-
it 'should call decision listener with correct variable type and value, when user in experiment and feature is not enabled' do
2038+
it 'should call decision listener with default variable type and value, when user in experiment and feature is not enabled' do
20392039
integer_feature = project_instance.config.feature_flag_key_map['integer_single_variable_feature']
20402040
experiment_to_return = project_instance.config.experiment_id_map[integer_feature['experimentIds'][0]]
20412041
variation_to_return = experiment_to_return['variations'][0]
@@ -2056,7 +2056,7 @@ class InvalidErrorHandler; end
20562056
feature_enabled: false,
20572057
variable_key: 'integer_variable',
20582058
variable_type: 'integer',
2059-
variable_value: 42,
2059+
variable_value: 7,
20602060
source: 'EXPERIMENT',
20612061
source_experiment_key: 'test_experiment_integer_feature',
20622062
source_variation_key: 'control'
@@ -2069,7 +2069,12 @@ class InvalidErrorHandler; end
20692069
'integer',
20702070
user_id,
20712071
nil
2072-
)).to eq(42)
2072+
)).to eq(7)
2073+
2074+
expect(spy_logger).to have_received(:log).once.with(
2075+
Logger::DEBUG,
2076+
"Feature 'integer_single_variable_feature' for variation 'control' is not enabled. Returning the default variable value '7'."
2077+
)
20732078
end
20742079

20752080
it 'should call decision listener with correct variable type and value, when user in experiment and feature is enabled' do
@@ -2146,7 +2151,7 @@ class InvalidErrorHandler; end
21462151
)).to eq(true)
21472152
end
21482153

2149-
it 'should call listener with correct variable type and value, when user in rollout and feature is not enabled' do
2154+
it 'should call listener with default variable type and value, when user in rollout and feature is not enabled' do
21502155
experiment_to_return = config_body['rollouts'][0]['experiments'][1]
21512156
variation_to_return = experiment_to_return['variations'][0]
21522157
decision_to_return = Optimizely::DecisionService::Decision.new(
@@ -2165,7 +2170,7 @@ class InvalidErrorHandler; end
21652170
feature_enabled: false,
21662171
variable_key: 'boolean_variable',
21672172
variable_type: 'boolean',
2168-
variable_value: false,
2173+
variable_value: true,
21692174
source: 'ROLLOUT',
21702175
source_experiment_key: nil,
21712176
source_variation_key: nil
@@ -2178,10 +2183,15 @@ class InvalidErrorHandler; end
21782183
'boolean',
21792184
user_id,
21802185
user_attributes
2181-
)).to eq(false)
2186+
)).to eq(true)
2187+
2188+
expect(spy_logger).to have_received(:log).once.with(
2189+
Logger::DEBUG,
2190+
"Feature 'boolean_single_variable_feature' for variation '177773' is not enabled. Returning the default variable value 'true'."
2191+
)
21822192
end
21832193

2184-
it 'should call listener with correct variable type and value, when user neither in experiment nor in rollout' do
2194+
it 'should call listener with default variable type and value, when user neither in experiment nor in rollout' do
21852195
allow(project_instance.decision_service).to receive(:get_variation_for_feature).and_return(nil)
21862196

21872197
expect(project_instance.notification_center).to receive(:send_notifications).once.with(

0 commit comments

Comments
 (0)