Class: Optimizely::ProjectConfig
- Inherits:
-
Object
- Object
- Optimizely::ProjectConfig
- Defined in:
- lib/optimizely/project_config.rb
Constant Summary collapse
- PROJECT_CONFIG_LINK_TEMPLATE =
Representation of the Optimizely project config.
'https://cdn.optimizely.com/json/%{project_id}.json'- REVENUE_GOAL_KEY =
'Total Revenue'- REQUEST_TIMEOUT =
10- RUNNING_EXPERIMENT_STATUS =
['Running']
Instance Attribute Summary collapse
-
#account_id ⇒ Object
readonly
Returns the value of attribute account_id.
-
#attribute_key_map ⇒ Object
readonly
Returns the value of attribute attribute_key_map.
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#audience_id_map ⇒ Object
readonly
Returns the value of attribute audience_id_map.
-
#audiences ⇒ Object
readonly
Returns the value of attribute audiences.
-
#error_handler ⇒ Object
readonly
Gets project config attributes.
-
#event_key_map ⇒ Object
readonly
Returns the value of attribute event_key_map.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#experiment_id_map ⇒ Object
readonly
Returns the value of attribute experiment_id_map.
-
#experiment_key_map ⇒ Object
readonly
Returns the value of attribute experiment_key_map.
-
#experiments ⇒ Object
readonly
Returns the value of attribute experiments.
-
#group_key_map ⇒ Object
readonly
Returns the value of attribute group_key_map.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#parsing_succeeded ⇒ Object
readonly
Returns the value of attribute parsing_succeeded.
-
#project_id ⇒ Object
readonly
Returns the value of attribute project_id.
-
#revision ⇒ Object
readonly
Returns the value of attribute revision.
-
#variation_id_map ⇒ Object
readonly
Returns the value of attribute variation_id_map.
-
#variation_key_map ⇒ Object
readonly
Returns the value of attribute variation_key_map.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #experiment_running?(experiment_key) ⇒ Boolean
- #get_attribute_id(attribute_key) ⇒ Object
- #get_audience_conditions_from_id(audience_id) ⇒ Object
- #get_audience_ids_for_experiment(experiment_key) ⇒ Object
- #get_experiment_group_id(experiment_key) ⇒ Object
- #get_experiment_id(experiment_key) ⇒ Object
- #get_experiment_ids_for_event(event_key) ⇒ Object
- #get_experiment_key(experiment_id) ⇒ Object
- #get_forced_variations(experiment_key) ⇒ Object
- #get_traffic_allocation(experiment_key) ⇒ Object
- #get_variation_id_from_key(experiment_key, variation_key) ⇒ Object
- #get_variation_key_from_id(experiment_key, variation_id) ⇒ Object
-
#initialize(datafile, logger, error_handler) ⇒ ProjectConfig
constructor
A new instance of ProjectConfig.
- #parsing_succeeded? ⇒ Boolean
- #user_in_forced_variation?(experiment_key, user_id) ⇒ Boolean
- #variation_id_exists?(experiment_id, variation_id) ⇒ Boolean
Constructor Details
#initialize(datafile, logger, error_handler) ⇒ ProjectConfig
Returns a new instance of ProjectConfig.
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 |
# File 'lib/optimizely/project_config.rb', line 59 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) @parsing_succeeded = false @error_handler = error_handler @logger = logger @version = config['version'] if UNSUPPORTED_VERSIONS.include?(@version) return end @account_id = config['accountId'] @project_id = config['projectId'] @attributes = config['attributes'] @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 @parsing_succeeded = true end |
Instance Attribute Details
#account_id ⇒ Object (readonly)
Returns the value of attribute account_id.
40 41 42 |
# File 'lib/optimizely/project_config.rb', line 40 def account_id @account_id end |
#attribute_key_map ⇒ Object (readonly)
Returns the value of attribute attribute_key_map.
49 50 51 |
# File 'lib/optimizely/project_config.rb', line 49 def attribute_key_map @attribute_key_map end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
42 43 44 |
# File 'lib/optimizely/project_config.rb', line 42 def attributes @attributes end |
#audience_id_map ⇒ Object (readonly)
Returns the value of attribute audience_id_map.
50 51 52 |
# File 'lib/optimizely/project_config.rb', line 50 def audience_id_map @audience_id_map end |
#audiences ⇒ Object (readonly)
Returns the value of attribute audiences.
47 48 49 |
# File 'lib/optimizely/project_config.rb', line 47 def audiences @audiences end |
#error_handler ⇒ Object (readonly)
Gets project config attributes.
35 36 37 |
# File 'lib/optimizely/project_config.rb', line 35 def error_handler @error_handler end |
#event_key_map ⇒ Object (readonly)
Returns the value of attribute event_key_map.
51 52 53 |
# File 'lib/optimizely/project_config.rb', line 51 def event_key_map @event_key_map end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
43 44 45 |
# File 'lib/optimizely/project_config.rb', line 43 def events @events end |
#experiment_id_map ⇒ Object (readonly)
Returns the value of attribute experiment_id_map.
52 53 54 |
# File 'lib/optimizely/project_config.rb', line 52 def experiment_id_map @experiment_id_map end |
#experiment_key_map ⇒ Object (readonly)
Returns the value of attribute experiment_key_map.
53 54 55 |
# File 'lib/optimizely/project_config.rb', line 53 def experiment_key_map @experiment_key_map end |
#experiments ⇒ Object (readonly)
Returns the value of attribute experiments.
44 45 46 |
# File 'lib/optimizely/project_config.rb', line 44 def experiments @experiments end |
#group_key_map ⇒ Object (readonly)
Returns the value of attribute group_key_map.
54 55 56 |
# File 'lib/optimizely/project_config.rb', line 54 def group_key_map @group_key_map end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
45 46 47 |
# File 'lib/optimizely/project_config.rb', line 45 def groups @groups end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
36 37 38 |
# File 'lib/optimizely/project_config.rb', line 36 def logger @logger end |
#parsing_succeeded ⇒ Object (readonly)
Returns the value of attribute parsing_succeeded.
38 39 40 |
# File 'lib/optimizely/project_config.rb', line 38 def parsing_succeeded @parsing_succeeded end |
#project_id ⇒ Object (readonly)
Returns the value of attribute project_id.
41 42 43 |
# File 'lib/optimizely/project_config.rb', line 41 def project_id @project_id end |
#revision ⇒ Object (readonly)
Returns the value of attribute revision.
46 47 48 |
# File 'lib/optimizely/project_config.rb', line 46 def revision @revision end |
#variation_id_map ⇒ Object (readonly)
Returns the value of attribute variation_id_map.
56 57 58 |
# File 'lib/optimizely/project_config.rb', line 56 def variation_id_map @variation_id_map end |
#variation_key_map ⇒ Object (readonly)
Returns the value of attribute variation_key_map.
57 58 59 |
# File 'lib/optimizely/project_config.rb', line 57 def variation_key_map @variation_key_map end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
39 40 41 |
# File 'lib/optimizely/project_config.rb', line 39 def version @version end |
Instance Method Details
#experiment_running?(experiment_key) ⇒ Boolean
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/optimizely/project_config.rb', line 107 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
268 269 270 271 272 273 274 |
# File 'lib/optimizely/project_config.rb', line 268 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
190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/optimizely/project_config.rb', line 190 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
176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/optimizely/project_config.rb', line 176 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
261 262 263 264 265 266 |
# File 'lib/optimizely/project_config.rb', line 261 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
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/optimizely/project_config.rb', line 120 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_event(event_key) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/optimizely/project_config.rb', line 148 def get_experiment_ids_for_event(event_key) # Get experiment IDs for the provided event key. # # event_key - Event key for which experiment IDs are to be retrieved. # # Returns array of all experiment IDs for the event. event = @event_key_map[event_key] return event['experimentIds'] if event @logger.log Logger::ERROR, "Event '#{event_key}' is not in datafile." @error_handler.handle_error InvalidEventError [] end |
#get_experiment_key(experiment_id) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/optimizely/project_config.rb', line 134 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? @logger.log Logger::ERROR, "Experiment id '#{experiment_id}' is not in datafile." @error_handler.handle_error InvalidExperimentError nil end |
#get_forced_variations(experiment_key) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/optimizely/project_config.rb', line 248 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_traffic_allocation(experiment_key) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/optimizely/project_config.rb', line 162 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
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/optimizely/project_config.rb', line 226 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
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/optimizely/project_config.rb', line 204 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 |
#parsing_succeeded? ⇒ Boolean
289 290 291 292 293 294 295 |
# File 'lib/optimizely/project_config.rb', line 289 def parsing_succeeded? # Helper method to determine if parsing the datafile was successful. # # Returns Boolean depending on whether parsing the datafile succeeded or not. @parsing_succeeded end |
#user_in_forced_variation?(experiment_key, user_id) ⇒ Boolean
276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/optimizely/project_config.rb', line 276 def user_in_forced_variation?(experiment_key, user_id) # Determines if a given user is in a forced variation # # experiment_key - String experiment key # user_id - String user ID # # Returns true if user is in a forced variation forced_variations = get_forced_variations(experiment_key) return forced_variations.include?(user_id) if forced_variations false end |
#variation_id_exists?(experiment_id, variation_id) ⇒ Boolean
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/optimizely/project_config.rb', line 297 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 @logger.log Logger::ERROR, "Variation ID '#{variation_id}' is not in datafile." @error_handler.handle_error InvalidVariationError return false end @logger.log Logger::ERROR, "Experiment ID '#{experiment_id}' is not in datafile." @error_handler.handle_error InvalidExperimentError false end |