3131
3232module Optimizely
3333 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
4334 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.
4448
4549 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-
5650 @is_valid = true
5751 @logger = logger || NoOpLogger . new
5852 @error_handler = error_handler || NoOpErrorHandler . new
@@ -89,16 +83,16 @@ def initialize(datafile, event_dispatcher = nil, logger = nil, error_handler = n
8983 @notification_center = NotificationCenter . new ( @logger , @error_handler )
9084 end
9185
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.
10194
95+ def activate ( experiment_key , user_id , attributes = nil )
10296 unless @is_valid
10397 @logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'activate' ) . message )
10498 return nil
@@ -125,16 +119,16 @@ def activate(experiment_key, user_id, attributes = nil)
125119 variation_key
126120 end
127121
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.
137130
131+ def get_variation ( experiment_key , user_id , attributes = nil )
138132 unless @is_valid
139133 @logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'get_variation' ) . message )
140134 return nil
@@ -161,42 +155,42 @@ def get_variation(experiment_key, user_id, attributes = nil)
161155 nil
162156 end
163157
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.
173166
167+ def set_forced_variation ( experiment_key , user_id , variation_key )
174168 @config . set_forced_variation ( experiment_key , user_id , variation_key )
175169 end
176170
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.
184177
178+ def get_forced_variation ( experiment_key , user_id )
185179 forced_variation_key = nil
186180 forced_variation = @config . get_forced_variation ( experiment_key , user_id )
187181 forced_variation_key = forced_variation [ 'key' ] if forced_variation
188182
189183 forced_variation_key
190184 end
191185
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.
199192
193+ def track ( event_key , user_id , attributes = nil , event_tags = nil )
200194 unless @is_valid
201195 @logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'track' ) . message )
202196 return nil
@@ -244,17 +238,18 @@ def track(event_key, user_id, attributes = nil, event_tags = nil)
244238 nil
245239 end
246240
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+
247252 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.
258253 unless @is_valid
259254 @logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'is_feature_enabled' ) . message )
260255 return false
@@ -300,14 +295,13 @@ def is_feature_enabled(feature_flag_key, user_id, attributes = nil)
300295 end
301296 end
302297
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+
303304 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- #
311305 enabled_features = [ ]
312306
313307 unless @is_valid
@@ -327,17 +321,17 @@ def get_enabled_features(user_id, attributes = nil)
327321 enabled_features
328322 end
329323
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.
340333
334+ def get_feature_variable_string ( feature_flag_key , variable_key , user_id , attributes = nil )
341335 unless @is_valid
342336 @logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'get_feature_variable_string' ) . message )
343337 return nil
@@ -354,17 +348,17 @@ def get_feature_variable_string(feature_flag_key, variable_key, user_id, attribu
354348 variable_value
355349 end
356350
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.
367360
361+ def get_feature_variable_boolean ( feature_flag_key , variable_key , user_id , attributes = nil )
368362 unless @is_valid
369363 @logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'get_feature_variable_boolean' ) . message )
370364 return nil
@@ -381,17 +375,17 @@ def get_feature_variable_boolean(feature_flag_key, variable_key, user_id, attrib
381375 variable_value
382376 end
383377
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.
394387
388+ def get_feature_variable_double ( feature_flag_key , variable_key , user_id , attributes = nil )
395389 unless @is_valid
396390 @logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'get_feature_variable_double' ) . message )
397391 return nil
@@ -408,22 +402,21 @@ def get_feature_variable_double(feature_flag_key, variable_key, user_id, attribu
408402 variable_value
409403 end
410404
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.
421414
415+ def get_feature_variable_integer ( feature_flag_key , variable_key , user_id , attributes = nil )
422416 unless @is_valid
423417 @logger . log ( Logger ::ERROR , InvalidDatafileError . new ( 'get_feature_variable_integer' ) . message )
424418 return nil
425419 end
426-
427420 variable_value = get_feature_variable_for_type (
428421 feature_flag_key ,
429422 variable_key ,
0 commit comments