Skip to content

Commit 5ef3c76

Browse files
committed
[FSSDK-11140] Ruby: Update project config to track CMAB properties
1 parent 61a95c3 commit 5ef3c76

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

lib/optimizely/config/datafile_project_config.rb

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DatafileProjectConfig < ProjectConfig
2727
attr_reader :datafile, :account_id, :attributes, :audiences, :typed_audiences, :events,
2828
:experiments, :feature_flags, :groups, :project_id, :bot_filtering, :revision,
2929
:sdk_key, :environment_key, :rollouts, :version, :send_flag_decisions,
30-
:attribute_key_map, :audience_id_map, :event_key_map, :experiment_feature_map,
30+
:attribute_key_map, :attribute_id_to_key_map, :audience_id_map, :event_key_map, :experiment_feature_map,
3131
:experiment_id_map, :experiment_key_map, :feature_flag_key_map, :feature_variable_key_map,
3232
:group_id_map, :rollout_id_map, :rollout_experiment_id_map, :variation_id_map,
3333
:variation_id_to_variable_usage_map, :variation_key_map, :variation_id_map_by_experiment_id,
@@ -82,6 +82,10 @@ def initialize(datafile, logger, error_handler)
8282

8383
# Utility maps for quick lookup
8484
@attribute_key_map = generate_key_map(@attributes, 'key')
85+
@attribute_id_to_key_map = {}
86+
for attribute in @attributes
87+
@attribute_id_to_key_map[attribute['id']] = attribute['key']
88+
end
8589
@event_key_map = generate_key_map(@events, 'key')
8690
@group_id_map = generate_key_map(@groups, 'id')
8791
@group_id_map.each do |key, group|
@@ -440,6 +444,44 @@ def get_attribute_id(attribute_key)
440444
nil
441445
end
442446

447+
def get_attribute_by_key(attribute_key)
448+
# Get attribute for the provided attribute key.
449+
#
450+
# Args:
451+
# Attribute key for which attribute is to be fetched.
452+
#
453+
# Returns:
454+
# Attribute corresponding to the provided attribute key.
455+
attribute = @attribute_key_map[attribute_key]
456+
if attribute_key in @attribute_key_map
457+
return attribute
458+
end
459+
460+
invalid_attribute_error = InvalidAttributeError.new(attribute_key)
461+
@logger.log Logger::ERROR, invalid_attribute_error.message
462+
@error_handler.handle_error invalid_attribute_error
463+
nil
464+
end
465+
466+
def get_attribute_key_by_id(attribute_id)
467+
# Get attribute key for the provided attribute ID.
468+
#
469+
# Args:
470+
# Attribute ID for which attribute is to be fetched.
471+
#
472+
# Returns:
473+
# Attribute key corresponding to the provided attribute ID.
474+
attribute = @attribute_id_to_key_map[attribute_id]
475+
if attribute_id in @attribute_id_to_key_map
476+
return attribute
477+
end
478+
479+
invalid_attribute_error = InvalidAttributeError.new(attribute_id)
480+
@logger.log Logger::ERROR, invalid_attribute_error.message
481+
@error_handler.handle_error invalid_attribute_error
482+
nil
483+
end
484+
443485
def variation_id_exists?(experiment_id, variation_id)
444486
# Determines if a given experiment ID / variation ID pair exists in the datafile
445487
#

lib/optimizely/helpers/constants.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ module Constants
201201
},
202202
'forcedVariations' => {
203203
'type' => 'object'
204+
},
205+
'cmab' => {
206+
'type' => 'object',
204207
}
205208
},
206209
'required' => %w[
@@ -303,6 +306,20 @@ module Constants
303306
},
304307
'required' => %w[key]
305308
}
309+
},
310+
'cmab' => {
311+
'type' => 'object',
312+
'items' => {
313+
'type' => 'object',
314+
'properties' => {
315+
'attributeIds' => {
316+
'type' => 'array',
317+
},
318+
'trafficAllocation' => {
319+
'type' => 'integer',
320+
}
321+
}
322+
}
306323
}
307324
},
308325
'required' => %w[

lib/optimizely/project_config.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ def get_whitelisted_variations(experiment_id); end
8686

8787
def get_attribute_id(attribute_key); end
8888

89+
def get_attribute_by_key(attribute_key); end
90+
91+
def get_attribute_key_by_id(attribute_id); end
92+
8993
def variation_id_exists?(experiment_id, variation_id); end
9094

9195
def get_feature_flag_from_key(feature_flag_key); end

spec/config/datafile_project_config_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,23 @@
10781078
end
10791079
end
10801080

1081+
describe 'test_cmab_field_population' do
1082+
config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY))
1083+
config_dict['experiments'][0]['cmab'] = { 'attributeIds' => ['808797688', '808797689'], 'trafficAllocation' => 4000 }
1084+
config_dict['experiments'][0]['trafficAllocation'] = []
1085+
1086+
config_json = JSON.dump(config_dict)
1087+
project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler)
1088+
1089+
it 'Should return CMAB details' do
1090+
experiment = project_config.get_experiment_from_key('test_experiment')
1091+
expect(experiment['cmab']).to eq({'attributeIds' => ['808797688', '808797689'], 'trafficAllocation' => 4000})
1092+
1093+
experiment2 = project_config.get_experiment_from_key('test_experiment_2')
1094+
expect(experiment2['cmab']).to eq(nil)
1095+
end
1096+
end
1097+
10811098
describe '#feature_experiment' do
10821099
let(:config) { Optimizely::DatafileProjectConfig.new(config_body_JSON, logger, error_handler) }
10831100

0 commit comments

Comments
 (0)