Class: Optimizely::DatafileProjectConfig

Inherits:
ProjectConfig show all
Defined in:
lib/optimizely/config/datafile_project_config.rb

Constant Summary collapse

RUNNING_EXPERIMENT_STATUS =

Representation of the Optimizely project config.

['Running'].freeze
RESERVED_ATTRIBUTE_PREFIX =
'$opt_'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datafile, logger, error_handler) ⇒ DatafileProjectConfig

Returns a new instance of DatafileProjectConfig.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/optimizely/config/datafile_project_config.rb', line 41

def initialize(datafile, logger, error_handler)
  # ProjectConfig init method to fetch and set project config data
  #
  # datafile - JSON string representing the project
  super()

  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)
  @integrations = config.fetch('integrations', [])
  @region = config.fetch('region', 'US')
  @holdouts = config.fetch('holdouts', [])

  # Default to US region if not specified
  @region = 'US' if @region.nil? || @region.empty?

  # 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')
  @attribute_id_map = generate_key_map(@attributes, 'id')
  @attribute_id_to_key_map = {}
  @attributes.each do |attribute|
    @attribute_id_to_key_map[attribute['id']] = attribute['key']
  end
  @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')
  @integration_key_map = generate_key_map(@integrations, 'key', first_value: true)
  @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 = {}
  @flag_variation_map = {}
  @holdout_id_map = {}
  @global_holdouts = {}
  @included_holdouts = {}
  @excluded_holdouts = {}
  @flag_holdouts_map = {}

  @holdouts.each do |holdout|
    next unless holdout['status'] == 'Running'

    @holdout_id_map[holdout['id']] = holdout

    if holdout['includedFlags'].nil? || holdout['includedFlags'].empty?
      @global_holdouts[holdout['id']] = holdout

      excluded_flags = holdout['excludedFlags']
      if excluded_flags && !excluded_flags.empty?
        excluded_flags.each do |flag_id|
          @excluded_holdouts[flag_id] ||= []
          @excluded_holdouts[flag_id] << holdout
        end
      end
    else
      holdout['includedFlags'].each do |flag_id|
        @included_holdouts[flag_id] ||= []
        @included_holdouts[flag_id] << holdout
      end
    end
  end

  @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

  if (odp_integration = @integration_key_map&.fetch('odp', nil))
    @public_key_for_odp = odp_integration['publicKey']
    @host_for_odp = odp_integration['host']
  end

  @all_segments = []
  @audience_id_map.each_value do |audience|
    @all_segments.concat Audience.get_segments(audience['conditions'])
  end

  @flag_variation_map = generate_feature_variation_map(@feature_flags)
  @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

    flag_id = feature_flag['id']
    applicable_holdouts = []

    applicable_holdouts.concat(@included_holdouts[flag_id]) if @included_holdouts[flag_id]

    @global_holdouts.each_value do |holdout|
      excluded_flag_ids = holdout['excludedFlags'] || []
      applicable_holdouts << holdout unless excluded_flag_ids.include?(flag_id)
    end

    @flag_holdouts_map[key] = applicable_holdouts unless applicable_holdouts.empty?
  end

  # Adding Holdout variations in variation id and key maps
  return unless @holdouts && !@holdouts.empty?

  @holdouts.each do |holdout|
    next unless holdout['status'] == 'Running'

    holdout_key = holdout['key']
    holdout_id = holdout['id']

    @variation_key_map[holdout_key] = {}
    @variation_id_map[holdout_key] = {}
    @variation_id_map_by_experiment_id[holdout_id] = {}
    @variation_key_map_by_experiment_id[holdout_id] = {}

    variations = holdout['variations']
    next unless variations && !variations.empty?

    variations.each do |variation|
      @variation_key_map[holdout_key][variation['key']] = variation
      @variation_id_map[holdout_key][variation['id']] = variation
      @variation_key_map_by_experiment_id[holdout_id][variation['key']] = variation
      @variation_id_map_by_experiment_id[holdout_id][variation['id']] = variation
    end
  end
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def 
  @account_id
end

#all_segmentsObject (readonly)

Returns the value of attribute all_segments.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def all_segments
  @all_segments
end

#anonymize_ipObject (readonly)

Boolean - denotes if Optimizely should remove the last block of visitors’ IP address before storing event data



39
40
41
# File 'lib/optimizely/config/datafile_project_config.rb', line 39

def anonymize_ip
  @anonymize_ip
end

#attribute_id_mapObject (readonly)

Returns the value of attribute attribute_id_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def attribute_id_map
  @attribute_id_map
end

#attribute_id_to_key_mapObject (readonly)

Returns the value of attribute attribute_id_to_key_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def attribute_id_to_key_map
  @attribute_id_to_key_map
end

#attribute_key_mapObject (readonly)

Returns the value of attribute attribute_key_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def attribute_key_map
  @attribute_key_map
end

#attributesObject (readonly)

Returns the value of attribute attributes.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def attributes
  @attributes
end

#audience_id_mapObject (readonly)

Returns the value of attribute audience_id_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def audience_id_map
  @audience_id_map
end

#audiencesObject (readonly)

Returns the value of attribute audiences.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def audiences
  @audiences
end

#bot_filteringObject (readonly)

Returns the value of attribute bot_filtering.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def bot_filtering
  @bot_filtering
end

#datafileObject (readonly)

Returns the value of attribute datafile.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def datafile
  @datafile
end

#environment_keyObject (readonly)

Returns the value of attribute environment_key.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def environment_key
  @environment_key
end

#event_key_mapObject (readonly)

Returns the value of attribute event_key_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def event_key_map
  @event_key_map
end

#eventsObject (readonly)

Returns the value of attribute events.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def events
  @events
end

#excluded_holdoutsObject (readonly)

Returns the value of attribute excluded_holdouts.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def excluded_holdouts
  @excluded_holdouts
end

#experiment_feature_mapObject (readonly)

Returns the value of attribute experiment_feature_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def experiment_feature_map
  @experiment_feature_map
end

#experiment_id_mapObject (readonly)

Returns the value of attribute experiment_id_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def experiment_id_map
  @experiment_id_map
end

#experiment_key_mapObject (readonly)

Returns the value of attribute experiment_key_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def experiment_key_map
  @experiment_key_map
end

#experimentsObject (readonly)

Returns the value of attribute experiments.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def experiments
  @experiments
end

#feature_flag_key_mapObject (readonly)

Returns the value of attribute feature_flag_key_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def feature_flag_key_map
  @feature_flag_key_map
end

#feature_flagsObject (readonly)

Returns the value of attribute feature_flags.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def feature_flags
  @feature_flags
end

#feature_variable_key_mapObject (readonly)

Returns the value of attribute feature_variable_key_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def feature_variable_key_map
  @feature_variable_key_map
end

#flag_holdouts_mapObject (readonly)

Returns the value of attribute flag_holdouts_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def flag_holdouts_map
  @flag_holdouts_map
end

#flag_variation_mapObject (readonly)

Returns the value of attribute flag_variation_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def flag_variation_map
  @flag_variation_map
end

#global_holdoutsObject (readonly)

Returns the value of attribute global_holdouts.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def global_holdouts
  @global_holdouts
end

#group_id_mapObject (readonly)

Returns the value of attribute group_id_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def group_id_map
  @group_id_map
end

#groupsObject (readonly)

Returns the value of attribute groups.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def groups
  @groups
end

#holdout_id_mapObject (readonly)

Returns the value of attribute holdout_id_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def holdout_id_map
  @holdout_id_map
end

#holdoutsObject (readonly)

Returns the value of attribute holdouts.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def holdouts
  @holdouts
end

#host_for_odpObject (readonly)

Returns the value of attribute host_for_odp.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def host_for_odp
  @host_for_odp
end

#included_holdoutsObject (readonly)

Returns the value of attribute included_holdouts.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def included_holdouts
  @included_holdouts
end

#integration_key_mapObject (readonly)

Returns the value of attribute integration_key_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def integration_key_map
  @integration_key_map
end

#integrationsObject (readonly)

Returns the value of attribute integrations.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def integrations
  @integrations
end

#project_idObject (readonly)

Returns the value of attribute project_id.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def project_id
  @project_id
end

#public_key_for_odpObject (readonly)

Returns the value of attribute public_key_for_odp.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def public_key_for_odp
  @public_key_for_odp
end

#regionObject (readonly)

Returns the value of attribute region.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def region
  @region
end

#revisionObject (readonly)

Returns the value of attribute revision.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def revision
  @revision
end

#rollout_experiment_id_mapObject (readonly)

Returns the value of attribute rollout_experiment_id_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def rollout_experiment_id_map
  @rollout_experiment_id_map
end

#rollout_id_mapObject (readonly)

Returns the value of attribute rollout_id_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def rollout_id_map
  @rollout_id_map
end

#rolloutsObject (readonly)

Returns the value of attribute rollouts.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def rollouts
  @rollouts
end

#sdk_keyObject (readonly)

Returns the value of attribute sdk_key.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def sdk_key
  @sdk_key
end

#send_flag_decisionsObject (readonly)

Returns the value of attribute send_flag_decisions.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def send_flag_decisions
  @send_flag_decisions
end

#typed_audiencesObject (readonly)

Returns the value of attribute typed_audiences.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def typed_audiences
  @typed_audiences
end

#variation_id_mapObject (readonly)

Returns the value of attribute variation_id_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def variation_id_map
  @variation_id_map
end

#variation_id_map_by_experiment_idObject (readonly)

Returns the value of attribute variation_id_map_by_experiment_id.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def variation_id_map_by_experiment_id
  @variation_id_map_by_experiment_id
end

#variation_id_to_variable_usage_mapObject (readonly)

Returns the value of attribute variation_id_to_variable_usage_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def variation_id_to_variable_usage_map
  @variation_id_to_variable_usage_map
end

#variation_key_mapObject (readonly)

Returns the value of attribute variation_key_map.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def variation_key_map
  @variation_key_map
end

#variation_key_map_by_experiment_idObject (readonly)

Returns the value of attribute variation_key_map_by_experiment_id.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def variation_key_map_by_experiment_id
  @variation_key_map_by_experiment_id
end

#versionObject (readonly)

Returns the value of attribute version.



27
28
29
# File 'lib/optimizely/config/datafile_project_config.rb', line 27

def version
  @version
end

Class Method Details

.create(datafile, logger, error_handler, skip_json_validation) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/optimizely/config/datafile_project_config.rb', line 255

def self.create(datafile, logger, error_handler, skip_json_validation)
  # Looks up and sets datafile and config based on response body.
  #
  # datafile - JSON string representing the Optimizely project.
  # logger - Provides a logger instance.
  # error_handler - Provides a handle_error method to handle exceptions.
  # skip_json_validation - Optional boolean param which allows skipping JSON schema
  #                       validation upon object invocation. By default JSON schema validation will be performed.
  # Returns instance of DatafileProjectConfig, nil otherwise.
  logger ||= SimpleLogger.new
  if !skip_json_validation && !Helpers::Validator.datafile_valid?(datafile)
    logger.log(Logger::ERROR, InvalidInputError.new('datafile').message)
    return nil
  end

  begin
    config = new(datafile, logger, error_handler)
  rescue StandardError => e
    error_to_handle = e.instance_of?(InvalidDatafileVersionError) ? e : InvalidInputError.new('datafile')
    error_msg = error_to_handle.message

    logger.log(Logger::ERROR, error_msg)
    error_handler.handle_error error_to_handle
    return nil
  end

  config
end

Instance Method Details

#experiment_running?(experiment) ⇒ Boolean

Returns:

  • (Boolean)


284
285
286
287
288
289
290
291
# File 'lib/optimizely/config/datafile_project_config.rb', line 284

def experiment_running?(experiment)
  # Determine if experiment corresponding to given key is running
  #
  # experiment - Experiment
  #
  # Returns true if experiment is running
  RUNNING_EXPERIMENT_STATUS.include?(experiment['status'])
end

#feature_experiment?(experiment_id) ⇒ Boolean

Returns:

  • (Boolean)


618
619
620
621
622
623
624
625
626
# File 'lib/optimizely/config/datafile_project_config.rb', line 618

def feature_experiment?(experiment_id)
  # Determines if given experiment is a feature test.
  #
  # experiment_id - String experiment ID
  #
  # Returns true if experiment belongs to  any feature,
  #              false otherwise.
  @experiment_feature_map.key?(experiment_id)
end

#get_attribute_by_key(attribute_key) ⇒ Object



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/optimizely/config/datafile_project_config.rb', line 520

def get_attribute_by_key(attribute_key)
  # Get attribute for the provided attribute key.
  #
  # Args:
  #   Attribute key for which attribute is to be fetched.
  #
  # Returns:
  #   Attribute corresponding to the provided attribute key.
  attribute = @attribute_key_map[attribute_key]
  return attribute if attribute

  invalid_attribute_error = InvalidAttributeError.new(attribute_key)
  @logger.log Logger::ERROR, invalid_attribute_error.message
  @error_handler.handle_error invalid_attribute_error
  nil
end

#get_attribute_id(attribute_key) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/optimizely/config/datafile_project_config.rb', line 495

def get_attribute_id(attribute_key)
  # Get attribute ID for the provided attribute key.
  #
  # Args:
  #   Attribute key for which attribute is to be fetched.
  #
  # Returns:
  #   Attribute ID corresponding to the provided attribute key.
  attribute = @attribute_key_map[attribute_key]
  has_reserved_prefix = attribute_key.to_s.start_with?(RESERVED_ATTRIBUTE_PREFIX)
  unless attribute.nil?
    if has_reserved_prefix
      @logger.log(Logger::WARN, "Attribute '#{attribute_key}' unexpectedly has reserved prefix '#{RESERVED_ATTRIBUTE_PREFIX}'; "\
                  'using attribute ID instead of reserved attribute name.')
    end
    return attribute['id']
  end
  return attribute_key if has_reserved_prefix

  invalid_attribute_error = InvalidAttributeError.new(attribute_key)
  @logger.log Logger::ERROR, invalid_attribute_error.message
  @error_handler.handle_error invalid_attribute_error
  nil
end

#get_attribute_key_by_id(attribute_id) ⇒ Object



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/optimizely/config/datafile_project_config.rb', line 537

def get_attribute_key_by_id(attribute_id)
  # Get attribute key for the provided attribute ID.
  #
  # Args:
  #   Attribute ID for which attribute is to be fetched.
  #
  # Returns:
  #   Attribute key corresponding to the provided attribute ID.
  attribute = @attribute_id_to_key_map[attribute_id]
  return attribute if attribute

  invalid_attribute_error = InvalidAttributeError.new(attribute_id)
  @logger.log Logger::ERROR, invalid_attribute_error.message
  @error_handler.handle_error invalid_attribute_error
  nil
end

#get_audience_from_id(audience_id) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/optimizely/config/datafile_project_config.rb', line 357

def get_audience_from_id(audience_id)
  # Get audience for the provided audience ID
  #
  # audience_id - ID of the audience
  #
  # Returns the audience

  audience = @audience_id_map[audience_id]
  return audience if audience

  invalid_audience_error = InvalidAudienceError.new(audience_id)
  @logger.log Logger::ERROR, invalid_audience_error.message
  @error_handler.handle_error invalid_audience_error
  nil
end

#get_event_from_key(event_key) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/optimizely/config/datafile_project_config.rb', line 341

def get_event_from_key(event_key)
  # Get event for the provided event key.
  #
  # event_key - Event key for which event is to be determined.
  #
  # Returns Event corresponding to the provided event key.

  event = @event_key_map[event_key]
  return event if event

  invalid_event_error = InvalidEventError.new(event_key)
  @logger.log Logger::ERROR, invalid_event_error.message
  @error_handler.handle_error invalid_event_error
  nil
end

#get_experiment_from_id(experiment_id) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/optimizely/config/datafile_project_config.rb', line 309

def get_experiment_from_id(experiment_id)
  # Retrieves experiment ID for a given key
  #
  # experiment_id - String id representing the experiment
  #
  # Returns Experiment or nil if not found

  experiment = @experiment_id_map[experiment_id]
  return experiment if experiment

  invalid_experiment_error = InvalidExperimentError.new(experiment_id: experiment_id)
  @logger.log Logger::ERROR, invalid_experiment_error.message
  @error_handler.handle_error invalid_experiment_error
  nil
end

#get_experiment_from_key(experiment_key) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/optimizely/config/datafile_project_config.rb', line 293

def get_experiment_from_key(experiment_key)
  # Retrieves experiment ID for a given key
  #
  # experiment_key - String key representing the experiment
  #
  # Returns Experiment or nil if not found

  experiment = @experiment_key_map[experiment_key]
  return experiment if experiment

  invalid_experiment_error = InvalidExperimentError.new(experiment_key: experiment_key)
  @logger.log Logger::ERROR, invalid_experiment_error.message
  @error_handler.handle_error invalid_experiment_error
  nil
end

#get_experiment_key(experiment_id) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/optimizely/config/datafile_project_config.rb', line 325

def get_experiment_key(experiment_id)
  # Retrieves experiment key for a given ID.
  #
  # experiment_id - String ID representing the experiment.
  #
  # Returns String key.

  experiment = @experiment_id_map[experiment_id]
  return experiment['key'] unless experiment.nil?

  invalid_experiment_error = InvalidExperimentError.new(experiment_id: experiment_id)
  @logger.log Logger::ERROR, invalid_experiment_error.message
  @error_handler.handle_error invalid_experiment_error
  nil
end

#get_feature_flag_from_key(feature_flag_key) ⇒ Object



576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/optimizely/config/datafile_project_config.rb', line 576

def get_feature_flag_from_key(feature_flag_key)
  # Retrieves the feature flag with the given key
  #
  # feature_flag_key - String feature key
  #
  # Returns feature flag if found, otherwise nil
  feature_flag = @feature_flag_key_map[feature_flag_key]
  return feature_flag if feature_flag

  @logger.log Logger::ERROR, "Feature flag key '#{feature_flag_key}' is not in datafile."
  nil
end

#get_feature_variable(feature_flag, variable_key) ⇒ Object



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/optimizely/config/datafile_project_config.rb', line 589

def get_feature_variable(feature_flag, variable_key)
  # Retrieves the variable with the given key for the given feature
  #
  # feature_flag - The feature flag for which we are retrieving the variable
  # variable_key - String variable key
  #
  # Returns variable if found, otherwise nil
  feature_flag_key = feature_flag['key']
  variable = @feature_variable_key_map[feature_flag_key][variable_key]
  return variable if variable

  @logger.log Logger::ERROR, "No feature variable was found for key '#{variable_key}' in feature flag "\
              "'#{feature_flag_key}'."
  nil
end

#get_holdout(holdout_id) ⇒ Object



651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/optimizely/config/datafile_project_config.rb', line 651

def get_holdout(holdout_id)
  # Helper method to get holdout from holdout ID
  #
  # holdout_id - ID of the holdout
  #
  # Returns the holdout

  holdout = @holdout_id_map[holdout_id]
  return holdout if holdout

  @logger.log Logger::ERROR, "Holdout with ID '#{holdout_id}' not found."
  nil
end

#get_holdouts_for_flag(flag_id) ⇒ Object



638
639
640
641
642
643
644
645
646
647
648
649
# File 'lib/optimizely/config/datafile_project_config.rb', line 638

def get_holdouts_for_flag(flag_id)
  # Helper method to get holdouts from an applied feature flag
  #
  # flag_id - (REQUIRED) ID of the feature flag
  #         This parameter is required and should not be null/nil
  #
  # Returns the holdouts that apply for a specific flag

  return [] if @holdouts.nil? || @holdouts.empty?

  @flag_holdouts_map[flag_id] || []
end

#get_rollout_from_id(rollout_id) ⇒ Object



605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/optimizely/config/datafile_project_config.rb', line 605

def get_rollout_from_id(rollout_id)
  # Retrieves the rollout with the given ID
  #
  # rollout_id - String rollout ID
  #
  # Returns the rollout if found, otherwise nil
  rollout = @rollout_id_map[rollout_id]
  return rollout if rollout

  @logger.log Logger::ERROR, "Rollout with ID '#{rollout_id}' is not in the datafile."
  nil
end

#get_rules_for_flag(feature_flag) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/optimizely/config/datafile_project_config.rb', line 237

def get_rules_for_flag(feature_flag)
  # Retrieves rules for a given feature flag
  #
  # feature_flag - String key representing the feature_flag
  #
  # Returns rules in feature flag
  rules = feature_flag['experimentIds'].map { |exp_id| @experiment_id_map[exp_id] }
  rollout = feature_flag['rolloutId'].empty? ? nil : @rollout_id_map[feature_flag['rolloutId']]

  if rollout
    rollout_experiments = rollout.fetch('experiments')
    rollout_experiments.each do |exp|
      rules.push(exp)
    end
  end
  rules
end

#get_variation_from_flag(flag_key, target_value, attribute) ⇒ Object



373
374
375
376
377
378
# File 'lib/optimizely/config/datafile_project_config.rb', line 373

def get_variation_from_flag(flag_key, target_value, attribute)
  variations = @flag_variation_map[flag_key]
  return variations.select { |variation| variation[attribute] == target_value }.first if variations

  nil
end

#get_variation_from_id(experiment_key, variation_id) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/optimizely/config/datafile_project_config.rb', line 380

def get_variation_from_id(experiment_key, variation_id)
  # Get variation given experiment key and variation ID
  #
  # experiment_key - Key representing parent experiment of variation
  # variation_id - ID of the variation
  #
  # Returns the variation or nil if not found

  variation_id_map = @variation_id_map[experiment_key]
  if variation_id_map
    variation = variation_id_map[variation_id]
    return variation if variation

    invalid_variation_error = InvalidVariationError.new(variation_id: variation_id)
    @logger.log Logger::ERROR, invalid_variation_error.message
    @error_handler.handle_error invalid_variation_error
    return nil
  end

  invalid_experiment_error = InvalidExperimentError.new(experiment_key: experiment_key)
  @logger.log Logger::ERROR, invalid_experiment_error.message
  @error_handler.handle_error invalid_experiment_error
  nil
end

#get_variation_from_id_by_experiment_id(experiment_id, variation_id) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/optimizely/config/datafile_project_config.rb', line 405

def get_variation_from_id_by_experiment_id(experiment_id, variation_id)
  # Get variation given experiment ID and variation ID
  #
  # experiment_id - ID representing parent experiment of variation
  # variation_id - ID of the variation
  #
  # Returns the variation or nil if not found

  variation_id_map_by_experiment_id = @variation_id_map_by_experiment_id[experiment_id]
  if variation_id_map_by_experiment_id
    variation = variation_id_map_by_experiment_id[variation_id]
    return variation if variation

    invalid_variation_error = InvalidVariationError.new(variation_id: variation_id)
    @logger.log Logger::ERROR, invalid_variation_error.message
    @error_handler.handle_error invalid_variation_error
    return nil
  end

  invalid_experiment_error = InvalidExperimentError.new(experiment_id: experiment_id)
  @logger.log Logger::ERROR, invalid_experiment_error.message
  @error_handler.handle_error invalid_experiment_error
  nil
end

#get_variation_id_from_key(experiment_key, variation_key) ⇒ Object



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/optimizely/config/datafile_project_config.rb', line 455

def get_variation_id_from_key(experiment_key, variation_key)
  # Get variation ID given experiment key and variation key
  #
  # experiment_key - Key representing parent experiment of variation
  # variation_key - Key of the variation
  #
  # Returns ID of the variation

  variation_key_map = @variation_key_map[experiment_key]
  if variation_key_map
    variation = variation_key_map[variation_key]
    return variation['id'] if variation

    invalid_variation_error = InvalidVariationError.new(variation_key: variation_key)
    @logger.log Logger::ERROR, invalid_variation_error.message
    @error_handler.handle_error invalid_variation_error
    return nil
  end

  invalid_experiment_error = InvalidExperimentError.new(experiment_key: experiment_key)
  @logger.log Logger::ERROR, invalid_experiment_error.message
  @error_handler.handle_error invalid_experiment_error
  nil
end

#get_variation_id_from_key_by_experiment_id(experiment_id, variation_key) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/optimizely/config/datafile_project_config.rb', line 430

def get_variation_id_from_key_by_experiment_id(experiment_id, variation_key)
  # Get variation given experiment ID and variation key
  #
  # experiment_id - ID representing parent experiment of variation
  # variation_key - Key of the variation
  #
  # Returns the variation or nil if not found

  variation_key_map = @variation_key_map_by_experiment_id[experiment_id]
  if variation_key_map
    variation = variation_key_map[variation_key]
    return variation['id'] if variation

    invalid_variation_error = InvalidVariationError.new(variation_key: variation_key)
    @logger.log Logger::ERROR, invalid_variation_error.message
    @error_handler.handle_error invalid_variation_error
    return nil
  end

  invalid_experiment_error = InvalidExperimentError.new(experiment_id: experiment_id)
  @logger.log Logger::ERROR, invalid_experiment_error.message
  @error_handler.handle_error invalid_experiment_error
  nil
end

#get_whitelisted_variations(experiment_id) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/optimizely/config/datafile_project_config.rb', line 480

def get_whitelisted_variations(experiment_id)
  # Retrieves whitelisted variations for a given experiment id
  #
  # experiment_id - String id representing the experiment
  #
  # Returns whitelisted variations for the experiment or nil

  experiment = @experiment_id_map[experiment_id]
  return experiment['forcedVariations'] if experiment

  invalid_experiment_error = InvalidExperimentError.new(experiment_id: experiment_id)
  @logger.log Logger::ERROR, invalid_experiment_error.message
  @error_handler.handle_error invalid_experiment_error
end

#rollout_experiment?(experiment_id) ⇒ Boolean

Returns:

  • (Boolean)


628
629
630
631
632
633
634
635
636
# File 'lib/optimizely/config/datafile_project_config.rb', line 628

def rollout_experiment?(experiment_id)
  # Determines if given experiment is a rollout test.
  #
  # experiment_id - String experiment ID
  #
  # Returns true if experiment belongs to  any rollout,
  #              false otherwise.
  @rollout_experiment_id_map.key?(experiment_id)
end

#variation_id_exists?(experiment_id, variation_id) ⇒ Boolean

Returns:

  • (Boolean)


554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'lib/optimizely/config/datafile_project_config.rb', line 554

def variation_id_exists?(experiment_id, variation_id)
  # Determines if a given experiment ID / variation ID pair exists in the datafile
  #
  # experiment_id - String experiment ID
  # variation_id - String variation ID
  #
  # Returns true if variation is in datafile

  experiment_key = get_experiment_key(experiment_id)
  variation_id_map = @variation_id_map[experiment_key]
  if variation_id_map
    variation = variation_id_map[variation_id]
    return true if variation

    invalid_variation_error = InvalidVariationError.new(variation_id: variation_id)
    @logger.log Logger::ERROR, invalid_variation_error.message
    @error_handler.handle_error invalid_variation_error
  end

  false
end