Class: Optimizely::ProjectConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/project_config.rb

Constant Summary collapse

'https://cdn.optimizely.com/json/%{project_id}.json'
REVENUE_GOAL_KEY =
'Total Revenue'
REQUEST_TIMEOUT =
10
RUNNING_EXPERIMENT_STATUS =
['Running']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datafile, logger, error_handler) ⇒ ProjectConfig

Returns a new instance of ProjectConfig.



40
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
# File 'lib/optimizely/project_config.rb', line 40

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

  config = JSON.load(datafile)

  @error_handler = error_handler
  @logger = logger
  @version = config['version']
  @account_id = config['accountId']
  @project_id = config['projectId']
  if @version == V1_CONFIG_VERSION
    @attributes = config['dimensions']
  else
    @attributes = config['attributes']
  end
  @events = config['events']
  @experiments = config['experiments']
  @revision = config['revision']
  @audiences = config['audiences']
  @groups = config.fetch('groups', [])

  # Utility maps for quick lookup
  @attribute_key_map = generate_key_map(@attributes, 'key')
  @event_key_map = generate_key_map(@events, 'key')
  @group_key_map = generate_key_map(@groups, 'id')
  @group_key_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')
  @variation_id_map = {}
  @variation_key_map = {}
  @experiment_key_map.each do |key, exp|
    variations = exp.fetch('variations')
    @variation_id_map[key] = generate_key_map(variations, 'id')
    @variation_key_map[key] = generate_key_map(variations, 'key')
  end
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



21
22
23
# File 'lib/optimizely/project_config.rb', line 21

def 
  @account_id
end

#attribute_key_mapObject (readonly)

Returns the value of attribute attribute_key_map.



30
31
32
# File 'lib/optimizely/project_config.rb', line 30

def attribute_key_map
  @attribute_key_map
end

#attributesObject (readonly)

Returns the value of attribute attributes.



23
24
25
# File 'lib/optimizely/project_config.rb', line 23

def attributes
  @attributes
end

#audience_id_mapObject (readonly)

Returns the value of attribute audience_id_map.



31
32
33
# File 'lib/optimizely/project_config.rb', line 31

def audience_id_map
  @audience_id_map
end

#audiencesObject (readonly)

Returns the value of attribute audiences.



28
29
30
# File 'lib/optimizely/project_config.rb', line 28

def audiences
  @audiences
end

#error_handlerObject (readonly)

Gets project config attributes.



17
18
19
# File 'lib/optimizely/project_config.rb', line 17

def error_handler
  @error_handler
end

#event_key_mapObject (readonly)

Returns the value of attribute event_key_map.



32
33
34
# File 'lib/optimizely/project_config.rb', line 32

def event_key_map
  @event_key_map
end

#eventsObject (readonly)

Returns the value of attribute events.



24
25
26
# File 'lib/optimizely/project_config.rb', line 24

def events
  @events
end

#experiment_id_mapObject (readonly)

Returns the value of attribute experiment_id_map.



33
34
35
# File 'lib/optimizely/project_config.rb', line 33

def experiment_id_map
  @experiment_id_map
end

#experiment_key_mapObject (readonly)

Returns the value of attribute experiment_key_map.



34
35
36
# File 'lib/optimizely/project_config.rb', line 34

def experiment_key_map
  @experiment_key_map
end

#experimentsObject (readonly)

Returns the value of attribute experiments.



25
26
27
# File 'lib/optimizely/project_config.rb', line 25

def experiments
  @experiments
end

#group_key_mapObject (readonly)

Returns the value of attribute group_key_map.



35
36
37
# File 'lib/optimizely/project_config.rb', line 35

def group_key_map
  @group_key_map
end

#groupsObject (readonly)

Returns the value of attribute groups.



26
27
28
# File 'lib/optimizely/project_config.rb', line 26

def groups
  @groups
end

#loggerObject (readonly)

Returns the value of attribute logger.



18
19
20
# File 'lib/optimizely/project_config.rb', line 18

def logger
  @logger
end

#project_idObject (readonly)

Returns the value of attribute project_id.



22
23
24
# File 'lib/optimizely/project_config.rb', line 22

def project_id
  @project_id
end

#revisionObject (readonly)

Returns the value of attribute revision.



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

def revision
  @revision
end

#variation_id_mapObject (readonly)

Returns the value of attribute variation_id_map.



37
38
39
# File 'lib/optimizely/project_config.rb', line 37

def variation_id_map
  @variation_id_map
end

#variation_key_mapObject (readonly)

Returns the value of attribute variation_key_map.



38
39
40
# File 'lib/optimizely/project_config.rb', line 38

def variation_key_map
  @variation_key_map
end

#versionObject (readonly)

Returns the value of attribute version.



20
21
22
# File 'lib/optimizely/project_config.rb', line 20

def version
  @version
end

Instance Method Details

#experiment_running?(experiment_key) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/optimizely/project_config.rb', line 85

def experiment_running?(experiment_key)
  # Determine if experiment corresponding to given key is running
  #
  # experiment_key - String key representing the experiment
  #
  # Returns true if experiment is running
  experiment = @experiment_key_map[experiment_key]
  return RUNNING_EXPERIMENT_STATUS.include?(experiment['status']) if experiment
  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
  nil
end

#get_attribute_id(attribute_key) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/optimizely/project_config.rb', line 252

def get_attribute_id(attribute_key)
  attribute = @attribute_key_map[attribute_key]
  return attribute['id'] if attribute
  @logger.log Logger::ERROR, "Attribute key '#{attribute_key}' is not in datafile."
  @error_handler.handle_error InvalidAttributeError
  nil
end

#get_audience_conditions_from_id(audience_id) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/optimizely/project_config.rb', line 174

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

  audience = @audience_id_map[audience_id]
  return audience['conditions'] if audience
  @logger.log Logger::ERROR, "Audience '#{audience_id}' is not in datafile."
  @error_handler.handle_error InvalidAudienceError
  nil
end

#get_audience_ids_for_experiment(experiment_key) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/optimizely/project_config.rb', line 160

def get_audience_ids_for_experiment(experiment_key)
  # Get audience IDs for the experiment
  #
  # experiment_key - Experiment key for which audience IDs are to be determined
  #
  # Returns audience IDs corresponding to the experiment.

  experiment = @experiment_key_map[experiment_key]
  return experiment['audienceIds'] if experiment
  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
  nil
end

#get_experiment_group_id(experiment_key) ⇒ Object



245
246
247
248
249
250
# File 'lib/optimizely/project_config.rb', line 245

def get_experiment_group_id(experiment_key)
  experiment = @experiment_key_map[experiment_key]
  return experiment['groupId'] if experiment
  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
end

#get_experiment_id(experiment_key) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/optimizely/project_config.rb', line 98

def get_experiment_id(experiment_key)
  # Retrieves experiment ID for a given key
  #
  # experiment_key - String key representing the experiment
  #
  # Returns String ID

  experiment = @experiment_key_map[experiment_key]
  return experiment['id'] if experiment
  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
  nil
end

#get_experiment_ids_for_goal(goal_key) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/optimizely/project_config.rb', line 132

def get_experiment_ids_for_goal(goal_key)
  # Get experiment IDs for the provided goal key.
  #
  # goal_key - Goal key for which experiment IDs are to be retrieved.
  #
  # Returns array of all experiment IDs for the goal.

  goal = @event_key_map[goal_key]
  return goal['experimentIds'] if goal
  @logger.log Logger::ERROR, "Event '#{goal_key}' is not in datafile."
  @error_handler.handle_error InvalidGoalError
  []
end

#get_forced_variations(experiment_key) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/optimizely/project_config.rb', line 232

def get_forced_variations(experiment_key)
  # Retrieves forced variations for a given experiment Key
  #
  # experiment_key - String Key representing the experiment
  #
  # Returns forced variations for the experiment or nil

  experiment = @experiment_key_map[experiment_key]
  return experiment['forcedVariations'] if experiment
  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
end

#get_goal_keysObject



112
113
114
115
116
117
118
119
120
# File 'lib/optimizely/project_config.rb', line 112

def get_goal_keys
  # Retrieves all goals in the project except 'Total Revenue'
  #
  # Returns array of all goal keys except 'Total Revenue'

  goal_keys = @event_key_map.keys
  goal_keys.delete(REVENUE_GOAL_KEY) if goal_keys.include?(REVENUE_GOAL_KEY)
  goal_keys
end

#get_revenue_goal_idObject



122
123
124
125
126
127
128
129
130
# File 'lib/optimizely/project_config.rb', line 122

def get_revenue_goal_id
  # Get ID of the revenue goal for the project
  #
  # Returns revenue goal ID

  revenue_goal = @event_key_map[REVENUE_GOAL_KEY]
  return revenue_goal['id'] if revenue_goal
  nil
end

#get_traffic_allocation(experiment_key) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/optimizely/project_config.rb', line 146

def get_traffic_allocation(experiment_key)
  # Retrieves traffic allocation for a given experiment Key
  #
  # experiment_key - String Key representing the experiment
  #
  # Returns traffic allocation for the experiment or nil

  experiment = @experiment_key_map[experiment_key]
  return experiment['trafficAllocation'] if experiment
  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
  nil
end

#get_variation_id_from_key(experiment_key, variation_key) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/optimizely/project_config.rb', line 210

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
    @logger.log Logger::ERROR, "Variation key '#{variation_key}' is not in datafile."
    @error_handler.handle_error InvalidVariationError
    return nil
  end

  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
  nil
end

#get_variation_key_from_id(experiment_key, variation_id) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/optimizely/project_config.rb', line 188

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

  variation_id_map = @variation_id_map[experiment_key]
  if variation_id_map
    variation = variation_id_map[variation_id]
    return variation['key'] if variation
    @logger.log Logger::ERROR, "Variation id '#{variation_id}' is not in datafile."
    @error_handler.handle_error InvalidVariationError
    return nil
  end

  @logger.log Logger::ERROR, "Experiment key '#{experiment_key}' is not in datafile."
  @error_handler.handle_error InvalidExperimentError
  nil
end