Skip to content

Commit 0094ed7

Browse files
rashidspmikeproeng37
authored andcommitted
refac(config): Refactoring and passing ProjectConfig from Optimizely. (#173)
1 parent a082d59 commit 0094ed7

File tree

8 files changed

+205
-208
lines changed

8 files changed

+205
-208
lines changed

lib/optimizely.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ def initialize(datafile, event_dispatcher = nil, logger = nil, error_handler = n
7474
return
7575
end
7676

77-
@decision_service = DecisionService.new(@config, @user_profile_service)
78-
@event_builder = EventBuilder.new(@config, @logger)
77+
@decision_service = DecisionService.new(@logger, @user_profile_service)
78+
@event_builder = EventBuilder.new(@logger)
7979
@notification_center = NotificationCenter.new(@logger, @error_handler)
8080
end
8181

@@ -145,7 +145,7 @@ def get_variation(experiment_key, user_id, attributes = nil)
145145
return nil
146146
end
147147

148-
variation_id = @decision_service.get_variation(experiment_key, user_id, attributes)
148+
variation_id = @decision_service.get_variation(@config, experiment_key, user_id, attributes)
149149
variation = @config.get_variation_from_id(experiment_key, variation_id) unless variation_id.nil?
150150
variation_key = variation['key'] if variation
151151
decision_notification_type = if @config.feature_experiment?(experiment['id'])
@@ -219,7 +219,7 @@ def track(event_key, user_id, attributes = nil, event_tags = nil)
219219
return nil
220220
end
221221

222-
conversion_event = @event_builder.create_conversion_event(event, user_id, attributes, event_tags)
222+
conversion_event = @event_builder.create_conversion_event(@config, event, user_id, attributes, event_tags)
223223
@config.logger.log(Logger::INFO, "Tracking event '#{event_key}' for user '#{user_id}'.")
224224
@logger.log(Logger::INFO,
225225
"Dispatching conversion event to URL #{conversion_event.url} with params #{conversion_event.params}.")
@@ -268,7 +268,7 @@ def is_feature_enabled(feature_flag_key, user_id, attributes = nil)
268268
return false
269269
end
270270

271-
decision = @decision_service.get_variation_for_feature(feature_flag, user_id, attributes)
271+
decision = @decision_service.get_variation_for_feature(@config, feature_flag, user_id, attributes)
272272

273273
feature_enabled = false
274274
source_string = Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT']
@@ -496,7 +496,7 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
496496
return nil
497497
else
498498
source_string = Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT']
499-
decision = @decision_service.get_variation_for_feature(feature_flag, user_id, attributes)
499+
decision = @decision_service.get_variation_for_feature(@config, feature_flag, user_id, attributes)
500500
variable_value = variable['defaultValue']
501501
if decision
502502
variation = decision['variation']
@@ -592,7 +592,7 @@ def validate_instantiation_options(datafile, skip_json_validation)
592592
def send_impression(experiment, variation_key, user_id, attributes = nil)
593593
experiment_key = experiment['key']
594594
variation_id = @config.get_variation_id_from_key(experiment_key, variation_key)
595-
impression_event = @event_builder.create_impression_event(experiment, variation_id, user_id, attributes)
595+
impression_event = @event_builder.create_impression_event(@config, experiment, variation_id, user_id, attributes)
596596
@logger.log(Logger::INFO,
597597
"Dispatching impression event to URL #{impression_event.url} with params #{impression_event.params}.")
598598
begin

lib/optimizely/bucketer.rb

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2016-2017, Optimizely and contributors
4+
# Copyright 2016-2017, 2019 Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -28,18 +28,17 @@ class Bucketer
2828
MAX_TRAFFIC_VALUE = 10_000
2929
UNSIGNED_MAX_32_BIT_VALUE = 0xFFFFFFFF
3030

31-
def initialize(config)
32-
# Bucketer init method to set bucketing seed and project config data.
33-
#
34-
# config - ProjectConfig data to be used in making bucketing decisions.
35-
31+
def initialize(logger)
32+
# Bucketer init method to set bucketing seed and logger.
33+
# logger - Optional component which provides a log method to log messages.
34+
@logger = logger
3635
@bucket_seed = HASH_SEED
37-
@config = config
3836
end
3937

40-
def bucket(experiment, bucketing_id, user_id)
38+
def bucket(project_config, experiment, bucketing_id, user_id)
4139
# Determines ID of variation to be shown for a given experiment key and user ID.
4240
#
41+
# project_config - Instance of ProjectConfig
4342
# experiment - Experiment for which visitor is to be bucketed.
4443
# bucketing_id - String A customer-assigned value used to generate the bucketing key
4544
# user_id - String ID for user.
@@ -52,27 +51,27 @@ def bucket(experiment, bucketing_id, user_id)
5251
experiment_key = experiment['key']
5352
group_id = experiment['groupId']
5453
if group_id
55-
group = @config.group_key_map.fetch(group_id)
54+
group = project_config.group_key_map.fetch(group_id)
5655
if Helpers::Group.random_policy?(group)
5756
traffic_allocations = group.fetch('trafficAllocation')
5857
bucketed_experiment_id = find_bucket(bucketing_id, user_id, group_id, traffic_allocations)
5958
# return if the user is not bucketed into any experiment
6059
unless bucketed_experiment_id
61-
@config.logger.log(Logger::INFO, "User '#{user_id}' is in no experiment.")
60+
@logger.log(Logger::INFO, "User '#{user_id}' is in no experiment.")
6261
return nil
6362
end
6463

6564
# return if the user is bucketed into a different experiment than the one specified
6665
if bucketed_experiment_id != experiment_id
67-
@config.logger.log(
66+
@logger.log(
6867
Logger::INFO,
6968
"User '#{user_id}' is not in experiment '#{experiment_key}' of group #{group_id}."
7069
)
7170
return nil
7271
end
7372

7473
# continue bucketing if the user is bucketed into the experiment specified
75-
@config.logger.log(
74+
@logger.log(
7675
Logger::INFO,
7776
"User '#{user_id}' is in experiment '#{experiment_key}' of group #{group_id}."
7877
)
@@ -82,9 +81,9 @@ def bucket(experiment, bucketing_id, user_id)
8281
traffic_allocations = experiment['trafficAllocation']
8382
variation_id = find_bucket(bucketing_id, user_id, experiment_id, traffic_allocations)
8483
if variation_id && variation_id != ''
85-
variation = @config.get_variation_from_id(experiment_key, variation_id)
84+
variation = project_config.get_variation_from_id(experiment_key, variation_id)
8685
variation_key = variation ? variation['key'] : nil
87-
@config.logger.log(
86+
@logger.log(
8887
Logger::INFO,
8988
"User '#{user_id}' is in variation '#{variation_key}' of experiment '#{experiment_key}'."
9089
)
@@ -93,13 +92,13 @@ def bucket(experiment, bucketing_id, user_id)
9392

9493
# Handle the case when the traffic range is empty due to sticky bucketing
9594
if variation_id == ''
96-
@config.logger.log(
95+
@logger.log(
9796
Logger::DEBUG,
9897
'Bucketed into an empty traffic range. Returning nil.'
9998
)
10099
end
101100

102-
@config.logger.log(Logger::INFO, "User '#{user_id}' is in no variation.")
101+
@logger.log(Logger::INFO, "User '#{user_id}' is in no variation.")
103102
nil
104103
end
105104

@@ -114,7 +113,7 @@ def find_bucket(bucketing_id, user_id, parent_id, traffic_allocations)
114113
# Returns entity ID corresponding to the provided bucket value or nil if no match is found.
115114
bucketing_key = format(BUCKETING_ID_TEMPLATE, bucketing_id: bucketing_id, entity_id: parent_id)
116115
bucket_value = generate_bucket_value(bucketing_key)
117-
@config.logger.log(Logger::DEBUG, "Assigned bucket #{bucket_value} to user '#{user_id}' "\
116+
@logger.log(Logger::DEBUG, "Assigned bucket #{bucket_value} to user '#{user_id}' "\
118117
"with bucketing ID: '#{bucketing_id}'.")
119118

120119
traffic_allocations.each do |traffic_allocation|

0 commit comments

Comments
 (0)