31
31
32
32
module Optimizely
33
33
class Project
34
- # Boolean representing if the instance represents a usable Optimizely Project
35
- attr_reader :is_valid
36
-
37
- attr_reader :config
38
- attr_reader :decision_service
39
- attr_reader :error_handler
40
- attr_reader :event_builder
41
- attr_reader :event_dispatcher
42
- attr_reader :logger
43
34
attr_reader :notification_center
35
+ # @api no-doc
36
+ attr_reader :is_valid , :config , :decision_service , :error_handler ,
37
+ :event_builder , :event_dispatcher , :logger
38
+
39
+ # Constructor for Projects.
40
+ #
41
+ # @param datafile - JSON string representing the project.
42
+ # @param event_dispatcher - Provides a dispatch_event method which if given a URL and params sends a request to it.
43
+ # @param logger - Optional component which provides a log method to log messages. By default nothing would be logged.
44
+ # @param error_handler - Optional component which provides a handle_error method to handle exceptions.
45
+ # By default all exceptions will be suppressed.
46
+ # @param user_profile_service - Optional component which provides methods to store and retreive user profiles.
47
+ # @param skip_json_validation - Optional boolean param to skip JSON schema validation of the provided datafile.
44
48
45
49
def initialize ( datafile , event_dispatcher = nil , logger = nil , error_handler = nil , skip_json_validation = false , user_profile_service = nil )
46
- # Constructor for Projects.
47
- #
48
- # datafile - JSON string representing the project.
49
- # event_dispatcher - Provides a dispatch_event method which if given a URL and params sends a request to it.
50
- # logger - Optional component which provides a log method to log messages. By default nothing would be logged.
51
- # error_handler - Optional component which provides a handle_error method to handle exceptions.
52
- # By default all exceptions will be suppressed.
53
- # user_profile_service - Optional component which provides methods to store and retreive user profiles.
54
- # skip_json_validation - Optional boolean param to skip JSON schema validation of the provided datafile.
55
-
56
50
@is_valid = true
57
51
@logger = logger || NoOpLogger . new
58
52
@error_handler = error_handler || NoOpErrorHandler . new
@@ -89,16 +83,16 @@ def initialize(datafile, event_dispatcher = nil, logger = nil, error_handler = n
89
83
@notification_center = NotificationCenter . new ( @logger , @error_handler )
90
84
end
91
85
92
- def activate ( experiment_key , user_id , attributes = nil )
93
- # Buckets visitor and sends impression event to Optimizely.
94
- #
95
- # experiment_key - Experiment which needs to be activated.
96
- # user_id - String ID for user.
97
- # attributes - Hash representing user attributes and values to be recorded.
98
- #
99
- # Returns variation key representing the variation the user will be bucketed in.
100
- # Returns nil if experiment is not Running, if user is not in experiment, or if datafile is invalid.
86
+ # Buckets visitor and sends impression event to Optimizely.
87
+ #
88
+ # @param experiment_key - Experiment which needs to be activated.
89
+ # @param user_id - String ID for user.
90
+ # @param attributes - Hash representing user attributes and values to be recorded.
91
+ #
92
+ # @return [Variation Key] representing the variation the user will be bucketed in.
93
+ # @return [nil] if experiment is not Running, if user is not in experiment, or if datafile is invalid.
101
94
95
+ def activate ( experiment_key , user_id , attributes = nil )
102
96
unless @is_valid
103
97
@logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'activate' ) . message )
104
98
return nil
@@ -125,16 +119,16 @@ def activate(experiment_key, user_id, attributes = nil)
125
119
variation_key
126
120
end
127
121
128
- def get_variation ( experiment_key , user_id , attributes = nil )
129
- # Gets variation where visitor will be bucketed.
130
- #
131
- # experiment_key - Experiment for which visitor variation needs to be determined.
132
- # user_id - String ID for user.
133
- # attributes - Hash representing user attributes.
134
- #
135
- # Returns variation key where visitor will be bucketed.
136
- # Returns nil if experiment is not Running, if user is not in experiment, or if datafile is invalid.
122
+ # Gets variation where visitor will be bucketed.
123
+ #
124
+ # @param experiment_key - Experiment for which visitor variation needs to be determined.
125
+ # @param user_id - String ID for user.
126
+ # @param attributes - Hash representing user attributes.
127
+ #
128
+ # @return [variation key] where visitor will be bucketed.
129
+ # @return [nil] if experiment is not Running, if user is not in experiment, or if datafile is invalid.
137
130
131
+ def get_variation ( experiment_key , user_id , attributes = nil )
138
132
unless @is_valid
139
133
@logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'get_variation' ) . message )
140
134
return nil
@@ -161,42 +155,42 @@ def get_variation(experiment_key, user_id, attributes = nil)
161
155
nil
162
156
end
163
157
164
- def set_forced_variation ( experiment_key , user_id , variation_key )
165
- # Force a user into a variation for a given experiment.
166
- #
167
- # experiment_key - String - key identifying the experiment.
168
- # user_id - String - The user ID to be used for bucketing.
169
- # variation_key - The variation key specifies the variation which the user will
170
- # be forced into. If nil, then clear the existing experiment-to-variation mapping.
171
- #
172
- # Returns - Boolean - indicates if the set completed successfully.
158
+ # Force a user into a variation for a given experiment.
159
+ #
160
+ # @param experiment_key - String - key identifying the experiment.
161
+ # @param user_id - String - The user ID to be used for bucketing.
162
+ # @param variation_key - The variation key specifies the variation which the user will
163
+ # be forced into. If nil, then clear the existing experiment-to-variation mapping.
164
+ #
165
+ # @return [Boolean] indicates if the set completed successfully.
173
166
167
+ def set_forced_variation ( experiment_key , user_id , variation_key )
174
168
@config . set_forced_variation ( experiment_key , user_id , variation_key )
175
169
end
176
170
177
- def get_forced_variation ( experiment_key , user_id )
178
- # Gets the forced variation for a given user and experiment.
179
- #
180
- # experiment_key - String - Key identifying the experiment.
181
- # user_id - String - The user ID to be used for bucketing.
182
- #
183
- # Returns String|nil The forced variation key.
171
+ # Gets the forced variation for a given user and experiment.
172
+ #
173
+ # @param experiment_key - String - Key identifying the experiment.
174
+ # @param user_id - String - The user ID to be used for bucketing.
175
+ #
176
+ # @return [String] The forced variation key.
184
177
178
+ def get_forced_variation ( experiment_key , user_id )
185
179
forced_variation_key = nil
186
180
forced_variation = @config . get_forced_variation ( experiment_key , user_id )
187
181
forced_variation_key = forced_variation [ 'key' ] if forced_variation
188
182
189
183
forced_variation_key
190
184
end
191
185
192
- def track ( event_key , user_id , attributes = nil , event_tags = nil )
193
- # Send conversion event to Optimizely.
194
- #
195
- # event_key - Event key representing the event which needs to be recorded.
196
- # user_id - String ID for user.
197
- # attributes - Hash representing visitor attributes and values which need to be recorded.
198
- # event_tags - Hash representing metadata associated with the event.
186
+ # Send conversion event to Optimizely.
187
+ #
188
+ # @param event_key - Event key representing the event which needs to be recorded.
189
+ # @param user_id - String ID for user.
190
+ # @param attributes - Hash representing visitor attributes and values which need to be recorded.
191
+ # @param event_tags - Hash representing metadata associated with the event.
199
192
193
+ def track ( event_key , user_id , attributes = nil , event_tags = nil )
200
194
unless @is_valid
201
195
@logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'track' ) . message )
202
196
return nil
@@ -244,17 +238,18 @@ def track(event_key, user_id, attributes = nil, event_tags = nil)
244
238
nil
245
239
end
246
240
241
+ # Determine whether a feature is enabled.
242
+ # Sends an impression event if the user is bucketed into an experiment using the feature.
243
+ #
244
+ # @param feature_flag_key - String unique key of the feature.
245
+ # @param user_id - String ID of the user.
246
+ # @param attributes - Hash representing visitor attributes and values which need to be recorded.
247
+ #
248
+ # @return [True] if the feature is enabled.
249
+ # @return [False] if the feature is disabled.
250
+ # @return [False] if the feature is not found.
251
+
247
252
def is_feature_enabled ( feature_flag_key , user_id , attributes = nil )
248
- # Determine whether a feature is enabled.
249
- # Sends an impression event if the user is bucketed into an experiment using the feature.
250
- #
251
- # feature_flag_key - String unique key of the feature.
252
- # userId - String ID of the user.
253
- # attributes - Hash representing visitor attributes and values which need to be recorded.
254
- #
255
- # Returns True if the feature is enabled.
256
- # False if the feature is disabled.
257
- # False if the feature is not found.
258
253
unless @is_valid
259
254
@logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'is_feature_enabled' ) . message )
260
255
return false
@@ -300,14 +295,13 @@ def is_feature_enabled(feature_flag_key, user_id, attributes = nil)
300
295
end
301
296
end
302
297
298
+ # Gets keys of all feature flags which are enabled for the user.
299
+ #
300
+ # @param user_id - ID for user.
301
+ # @param attributes - Dict representing user attributes.
302
+ # @return [feature flag keys] A List of feature flag keys that are enabled for the user.
303
+
303
304
def get_enabled_features ( user_id , attributes = nil )
304
- # Gets keys of all feature flags which are enabled for the user.
305
- # Args:
306
- # user_id: ID for user.
307
- # attributes: Dict representing user attributes.
308
- # Returns:
309
- # A List of feature flag keys that are enabled for the user.
310
- #
311
305
enabled_features = [ ]
312
306
313
307
unless @is_valid
@@ -327,17 +321,17 @@ def get_enabled_features(user_id, attributes = nil)
327
321
enabled_features
328
322
end
329
323
330
- def get_feature_variable_string ( feature_flag_key , variable_key , user_id , attributes = nil )
331
- # Get the String value of the specified variable in the feature flag.
332
- #
333
- # feature_flag_key - String key of feature flag the variable belongs to
334
- # variable_key - String key of variable for which we are getting the string value
335
- # user_id - String user ID
336
- # attributes - Hash representing visitor attributes and values which need to be recorded.
337
- #
338
- # Returns the string variable value.
339
- # Returns nil if the feature flag or variable are not found.
324
+ # Get the String value of the specified variable in the feature flag.
325
+ #
326
+ # @param feature_flag_key - String key of feature flag the variable belongs to
327
+ # @param variable_key - String key of variable for which we are getting the string value
328
+ # @param user_id - String user ID
329
+ # @param attributes - Hash representing visitor attributes and values which need to be recorded.
330
+ #
331
+ # @return [String] the string variable value.
332
+ # @return [nil] if the feature flag or variable are not found.
340
333
334
+ def get_feature_variable_string ( feature_flag_key , variable_key , user_id , attributes = nil )
341
335
unless @is_valid
342
336
@logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'get_feature_variable_string' ) . message )
343
337
return nil
@@ -354,17 +348,17 @@ def get_feature_variable_string(feature_flag_key, variable_key, user_id, attribu
354
348
variable_value
355
349
end
356
350
357
- def get_feature_variable_boolean ( feature_flag_key , variable_key , user_id , attributes = nil )
358
- # Get the Boolean value of the specified variable in the feature flag.
359
- #
360
- # feature_flag_key - String key of feature flag the variable belongs to
361
- # variable_key - String key of variable for which we are getting the string value
362
- # user_id - String user ID
363
- # attributes - Hash representing visitor attributes and values which need to be recorded.
364
- #
365
- # Returns the boolean variable value.
366
- # Returns nil if the feature flag or variable are not found.
351
+ # Get the Boolean value of the specified variable in the feature flag.
352
+ #
353
+ # @param feature_flag_key - String key of feature flag the variable belongs to
354
+ # @param variable_key - String key of variable for which we are getting the string value
355
+ # @param user_id - String user ID
356
+ # @param attributes - Hash representing visitor attributes and values which need to be recorded.
357
+ #
358
+ # @return [Boolean] the boolean variable value.
359
+ # @return [nil] if the feature flag or variable are not found.
367
360
361
+ def get_feature_variable_boolean ( feature_flag_key , variable_key , user_id , attributes = nil )
368
362
unless @is_valid
369
363
@logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'get_feature_variable_boolean' ) . message )
370
364
return nil
@@ -381,17 +375,17 @@ def get_feature_variable_boolean(feature_flag_key, variable_key, user_id, attrib
381
375
variable_value
382
376
end
383
377
384
- def get_feature_variable_double ( feature_flag_key , variable_key , user_id , attributes = nil )
385
- # Get the Double value of the specified variable in the feature flag.
386
- #
387
- # feature_flag_key - String key of feature flag the variable belongs to
388
- # variable_key - String key of variable for which we are getting the string value
389
- # user_id - String user ID
390
- # attributes - Hash representing visitor attributes and values which need to be recorded.
391
- #
392
- # Returns the double variable value.
393
- # Returns nil if the feature flag or variable are not found.
378
+ # Get the Double value of the specified variable in the feature flag.
379
+ #
380
+ # @param feature_flag_key - String key of feature flag the variable belongs to
381
+ # @param variable_key - String key of variable for which we are getting the string value
382
+ # @param user_id - String user ID
383
+ # @param attributes - Hash representing visitor attributes and values which need to be recorded.
384
+ #
385
+ # @return [Boolean] the double variable value.
386
+ # @return [nil] if the feature flag or variable are not found.
394
387
388
+ def get_feature_variable_double ( feature_flag_key , variable_key , user_id , attributes = nil )
395
389
unless @is_valid
396
390
@logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'get_feature_variable_double' ) . message )
397
391
return nil
@@ -408,22 +402,21 @@ def get_feature_variable_double(feature_flag_key, variable_key, user_id, attribu
408
402
variable_value
409
403
end
410
404
411
- def get_feature_variable_integer ( feature_flag_key , variable_key , user_id , attributes = nil )
412
- # Get the Integer value of the specified variable in the feature flag.
413
- #
414
- # feature_flag_key - String key of feature flag the variable belongs to
415
- # variable_key - String key of variable for which we are getting the string value
416
- # user_id - String user ID
417
- # attributes - Hash representing visitor attributes and values which need to be recorded.
418
- #
419
- # Returns the integer variable value.
420
- # Returns nil if the feature flag or variable are not found.
405
+ # Get the Integer value of the specified variable in the feature flag.
406
+ #
407
+ # @param feature_flag_key - String key of feature flag the variable belongs to
408
+ # @param variable_key - String key of variable for which we are getting the string value
409
+ # @param user_id - String user ID
410
+ # @param attributes - Hash representing visitor attributes and values which need to be recorded.
411
+ #
412
+ # @return [Integer] variable value.
413
+ # @return [nil] if the feature flag or variable are not found.
421
414
415
+ def get_feature_variable_integer ( feature_flag_key , variable_key , user_id , attributes = nil )
422
416
unless @is_valid
423
417
@logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'get_feature_variable_integer' ) . message )
424
418
return nil
425
419
end
426
-
427
420
variable_value = get_feature_variable_for_type (
428
421
feature_flag_key ,
429
422
variable_key ,
0 commit comments