class Optimizely::OptimizelyConfig

Public Class Methods

new(project_config) click to toggle source
# File lib/optimizely/optimizely_config.rb, line 20
def initialize(project_config)
  @project_config = project_config
end

Public Instance Methods

config() click to toggle source
# File lib/optimizely/optimizely_config.rb, line 24
def config
  experiments_map_object = experiments_map
  features_map = get_features_map(experiments_map_object)
  {
    'datafile' => @project_config.datafile,
    'experimentsMap' => experiments_map_object,
    'featuresMap' => features_map,
    'revision' => @project_config.revision
  }
end

Private Instance Methods

experiments_map() click to toggle source
# File lib/optimizely/optimizely_config.rb, line 37
def experiments_map
  feature_variables_map = @project_config.feature_flags.reduce({}) do |result_map, feature|
    result_map.update(feature['id'] => feature['variables'])
  end
  @project_config.experiments.reduce({}) do |experiments_map, experiment|
    experiments_map.update(
      experiment['key'] => {
        'id' => experiment['id'],
        'key' => experiment['key'],
        'variationsMap' => experiment['variations'].reduce({}) do |variations_map, variation|
          variation_object = {
            'id' => variation['id'],
            'key' => variation['key'],
            'variablesMap' => get_merged_variables_map(variation, experiment['id'], feature_variables_map)
          }
          variation_object['featureEnabled'] = variation['featureEnabled'] if @project_config.feature_experiment?(experiment['id'])
          variations_map.update(variation['key'] => variation_object)
        end
      }
    )
  end
end
get_features_map(all_experiments_map) click to toggle source
# File lib/optimizely/optimizely_config.rb, line 92
def get_features_map(all_experiments_map)
  @project_config.feature_flags.reduce({}) do |features_map, feature|
    features_map.update(
      feature['key'] => {
        'id' => feature['id'],
        'key' => feature['key'],
        'experimentsMap' => feature['experimentIds'].reduce({}) do |experiments_map, experiment_id|
          experiment_key = @project_config.experiment_id_map[experiment_id]['key']
          experiments_map.update(experiment_key => all_experiments_map[experiment_key])
        end,
        'variablesMap' => feature['variables'].reduce({}) do |variables, variable|
          variables.update(
            variable['key'] => {
              'id' => variable['id'],
              'key' => variable['key'],
              'type' => variable['type'],
              'value' => variable['defaultValue']
            }
          )
        end
      }
    )
  end
end
get_merged_variables_map(variation, experiment_id, feature_variables_map) click to toggle source

Merges feature key and type from feature variables to variation variables.

# File lib/optimizely/optimizely_config.rb, line 61
def get_merged_variables_map(variation, experiment_id, feature_variables_map)
  feature_ids = @project_config.experiment_feature_map[experiment_id]
  return {} unless feature_ids

  experiment_feature_variables = feature_variables_map[feature_ids[0]]
  # temporary variation variables map to get values to merge.
  temp_variables_id_map = {}
  if variation['variables']
    temp_variables_id_map = variation['variables'].reduce({}) do |variables_map, variable|
      variables_map.update(
        variable['id'] => {
          'id' => variable['id'],
          'value' => variable['value']
        }
      )
    end
  end
  experiment_feature_variables.reduce({}) do |variables_map, feature_variable|
    variation_variable = temp_variables_id_map[feature_variable['id']]
    variable_value = variation['featureEnabled'] && variation_variable ? variation_variable['value'] : feature_variable['defaultValue']
    variables_map.update(
      feature_variable['key'] => {
        'id' => feature_variable['id'],
        'key' => feature_variable['key'],
        'type' => feature_variable['type'],
        'value' => variable_value
      }
    )
  end
end