Method: Optimizely::DatafileProjectConfig#initialize
- Defined in:
- lib/optimizely/config/datafile_project_config.rb
#initialize(datafile, logger, error_handler) ⇒ DatafileProjectConfig
Returns a new instance of DatafileProjectConfig.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/optimizely/config/datafile_project_config.rb', line 64 def initialize(datafile, logger, error_handler) # ProjectConfig init method to fetch and set project config data # # datafile - JSON string representing the project config = JSON.parse(datafile) @datafile = datafile @error_handler = error_handler @logger = logger @version = config['version'] raise InvalidDatafileVersionError, @version unless Helpers::Constants::SUPPORTED_VERSIONS.value?(@version) @account_id = config['accountId'] @attributes = config.fetch('attributes', []) @audiences = config.fetch('audiences', []) @typed_audiences = config.fetch('typedAudiences', []) @events = config.fetch('events', []) @experiments = config['experiments'] @feature_flags = config.fetch('featureFlags', []) @groups = config.fetch('groups', []) @project_id = config['projectId'] @anonymize_ip = config.key?('anonymizeIP') ? config['anonymizeIP'] : false @bot_filtering = config['botFiltering'] @revision = config['revision'] @sdk_key = config.fetch('sdkKey', '') @environment_key = config.fetch('environmentKey', '') @rollouts = config.fetch('rollouts', []) @send_flag_decisions = config.fetch('sendFlagDecisions', false) # Json type is represented in datafile as a subtype of string for the sake of backwards compatibility. # Converting it to a first-class json type while creating Project Config @feature_flags.each do |feature_flag| feature_flag['variables'].each do |variable| if variable['type'] == 'string' && variable['subType'] == 'json' variable['type'] = 'json' variable.delete('subType') end end end # Utility maps for quick lookup @attribute_key_map = generate_key_map(@attributes, 'key') @event_key_map = generate_key_map(@events, 'key') @group_id_map = generate_key_map(@groups, 'id') @group_id_map.each do |key, group| exps = group.fetch('experiments') exps.each do |exp| @experiments.push(exp.merge('groupId' => key)) end end @experiment_key_map = generate_key_map(@experiments, 'key') @experiment_id_map = generate_key_map(@experiments, 'id') @audience_id_map = generate_key_map(@audiences, 'id') @audience_id_map = @audience_id_map.merge(generate_key_map(@typed_audiences, 'id')) unless @typed_audiences.empty? @variation_id_map = {} @variation_key_map = {} @variation_id_map_by_experiment_id = {} @variation_key_map_by_experiment_id = {} @variation_id_to_variable_usage_map = {} @variation_id_to_experiment_map = {} @experiment_id_map.each_value do |exp| # Excludes experiments from rollouts variations = exp.fetch('variations') variations.each do |variation| variation_id = variation['id'] @variation_id_to_experiment_map[variation_id] = exp end end @rollout_id_map = generate_key_map(@rollouts, 'id') # split out the experiment key map for rollouts @rollout_experiment_id_map = {} @rollout_id_map.each_value do |rollout| exps = rollout.fetch('experiments') @rollout_experiment_id_map = @rollout_experiment_id_map.merge(generate_key_map(exps, 'id')) end @all_experiments = @experiment_id_map.merge(@rollout_experiment_id_map) @all_experiments.each do |id, exp| variations = exp.fetch('variations') variations.each do |variation| variation_id = variation['id'] variation['featureEnabled'] = variation['featureEnabled'] == true variation_variables = variation['variables'] next if variation_variables.nil? @variation_id_to_variable_usage_map[variation_id] = generate_key_map(variation_variables, 'id') end @variation_id_map[exp['key']] = generate_key_map(variations, 'id') @variation_key_map[exp['key']] = generate_key_map(variations, 'key') @variation_id_map_by_experiment_id[id] = generate_key_map(variations, 'id') @variation_key_map_by_experiment_id[id] = generate_key_map(variations, 'key') end @feature_flag_key_map = generate_key_map(@feature_flags, 'key') @experiment_feature_map = {} @feature_variable_key_map = {} @feature_flag_key_map.each do |key, feature_flag| @feature_variable_key_map[key] = generate_key_map(feature_flag['variables'], 'key') feature_flag['experimentIds'].each do |experiment_id| @experiment_feature_map[experiment_id] = [feature_flag['id']] end end end |