Skip to content

Commit ca43f1e

Browse files
[FSSDK-9382] switch client init args from positional to keyword (#342)
1 parent 9487c0a commit ca43f1e

15 files changed

+170
-174
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ You can initialize the Optimizely instance in two ways: directly with a datafile
4141
Initialize Optimizely with a datafile. This datafile will be used as ProjectConfig throughout the life of the Optimizely instance.
4242

4343
```ruby
44-
optimizely_instance = Optimizely::Project.new(datafile)
44+
optimizely_instance = Optimizely::Project.new(datafile: datafile)
4545
```
4646

4747
#### Initialization by OptimizelyFactory

lib/optimizely.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,20 @@ class Project
7070
# @param event_processor_options: Optional hash of options to be passed to the default batch event processor.
7171
# @param settings: Optional instance of OptimizelySdkSettings for sdk configuration.
7272

73-
def initialize( # rubocop:disable Metrics/ParameterLists
74-
datafile = nil,
75-
event_dispatcher = nil,
76-
logger = nil,
77-
error_handler = nil,
78-
skip_json_validation = false, # rubocop:disable Style/OptionalBooleanParameter
79-
user_profile_service = nil,
80-
sdk_key = nil,
81-
config_manager = nil,
82-
notification_center = nil,
83-
event_processor = nil,
84-
default_decide_options = [],
85-
event_processor_options = {},
86-
settings = nil
73+
def initialize(
74+
datafile: nil,
75+
event_dispatcher: nil,
76+
logger: nil,
77+
error_handler: nil,
78+
skip_json_validation: false,
79+
user_profile_service: nil,
80+
sdk_key: nil,
81+
config_manager: nil,
82+
notification_center: nil,
83+
event_processor: nil,
84+
default_decide_options: [],
85+
event_processor_options: {},
86+
settings: nil
8787
)
8888
@logger = logger || NoOpLogger.new
8989
@error_handler = error_handler || NoOpErrorHandler.new

lib/optimizely/audience.rb

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

33
#
4-
# Copyright 2016-2017, 2019-2020, Optimizely and contributors
4+
# Copyright 2016-2017, 2019-2020, 2023, 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.
@@ -59,7 +59,7 @@ def user_meets_audience_conditions?(config, experiment, user_context, logger, lo
5959
user_condition_evaluator = UserConditionEvaluator.new(user_context, logger)
6060

6161
evaluate_user_conditions = lambda do |condition|
62-
return user_condition_evaluator.evaluate(condition)
62+
user_condition_evaluator.evaluate(condition)
6363
end
6464

6565
evaluate_audience = lambda do |audience_id|

lib/optimizely/event/event_factory.rb

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

33
#
4-
# Copyright 2019-2020, 2022, Optimizely and contributors
4+
# Copyright 2019-2020, 2022-2023, 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.
@@ -72,7 +72,7 @@ def create_log_event(user_events, logger)
7272

7373
def build_attribute_list(user_attributes, project_config)
7474
visitor_attributes = []
75-
user_attributes&.keys&.each do |attribute_key|
75+
user_attributes&.each_key do |attribute_key|
7676
# Omit attribute values that are not supported by the log endpoint.
7777
attribute_value = user_attributes[attribute_key]
7878
next unless Helpers::Validator.attribute_valid?(attribute_key, attribute_value)

lib/optimizely/event_builder.rb

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

33
#
4-
# Copyright 2016-2019, 2022, Optimizely and contributors
4+
# Copyright 2016-2019, 2022-2023, 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.
@@ -62,7 +62,7 @@ def get_common_params(project_config, user_id, attributes)
6262

6363
visitor_attributes = []
6464

65-
attributes&.keys&.each do |attribute_key|
65+
attributes&.each_key do |attribute_key|
6666
# Omit attribute values that are not supported by the log endpoint.
6767
attribute_value = attributes[attribute_key]
6868
if Helpers::Validator.attribute_valid?(attribute_key, attribute_value)

lib/optimizely/helpers/validator.rb

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

33
#
4-
# Copyright 2016-2019, 2022, Optimizely and contributors
4+
# Copyright 2016-2019, 2022-2023, 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.
@@ -190,14 +190,13 @@ def segments_cache_valid?(segments_cache)
190190
# segments_cache - custom cache to be validated.
191191
#
192192
# Returns boolean depending on whether cache has required methods.
193-
(
194-
segments_cache.respond_to?(:reset) &&
193+
194+
segments_cache.respond_to?(:reset) &&
195195
segments_cache.method(:reset)&.parameters&.empty? &&
196196
segments_cache.respond_to?(:lookup) &&
197197
segments_cache.method(:lookup)&.parameters&.length&.positive? &&
198198
segments_cache.respond_to?(:save) &&
199199
segments_cache.method(:save)&.parameters&.length&.positive?
200-
)
201200
end
202201

203202
def segment_manager_valid?(segment_manager)
@@ -206,13 +205,12 @@ def segment_manager_valid?(segment_manager)
206205
# segment_manager - custom manager to be validated.
207206
#
208207
# Returns boolean depending on whether manager has required methods.
209-
(
210-
segment_manager.respond_to?(:odp_config) &&
208+
209+
segment_manager.respond_to?(:odp_config) &&
211210
segment_manager.respond_to?(:reset) &&
212211
segment_manager.method(:reset)&.parameters&.empty? &&
213212
segment_manager.respond_to?(:fetch_qualified_segments) &&
214213
(segment_manager.method(:fetch_qualified_segments)&.parameters&.length || 0) >= 3
215-
)
216214
end
217215

218216
def event_manager_valid?(event_manager)

lib/optimizely/optimizely_factory.rb

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

33
#
4-
# Copyright 2019, 2022, Optimizely and contributors
4+
# Copyright 2019, 2022-2023, 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.
@@ -103,15 +103,15 @@ def self.default_instance(sdk_key, datafile = nil)
103103
)
104104

105105
Optimizely::Project.new(
106-
datafile, nil, logger, error_handler, nil, nil, sdk_key, config_manager, notification_center
106+
datafile: datafile, logger: logger, error_handler: error_handler, sdk_key: sdk_key, config_manager: config_manager, notification_center: notification_center
107107
)
108108
end
109109

110110
# Returns a new optimizely instance.
111111
#
112112
# @param config_manager - Required ConfigManagerInterface Responds to 'config' method.
113113
def self.default_instance_with_config_manager(config_manager)
114-
Optimizely::Project.new(nil, nil, nil, nil, nil, nil, nil, config_manager)
114+
Optimizely::Project.new(config_manager: config_manager)
115115
end
116116

117117
# Returns a new optimizely instance.
@@ -167,19 +167,17 @@ def self.custom_instance( # rubocop:disable Metrics/ParameterLists
167167
)
168168

169169
Optimizely::Project.new(
170-
datafile,
171-
event_dispatcher,
172-
logger,
173-
error_handler,
174-
skip_json_validation,
175-
user_profile_service,
176-
sdk_key,
177-
config_manager,
178-
notification_center,
179-
event_processor,
180-
[],
181-
{},
182-
settings
170+
datafile: datafile,
171+
event_dispatcher: event_dispatcher,
172+
logger: logger,
173+
error_handler: error_handler,
174+
skip_json_validation: skip_json_validation,
175+
user_profile_service: user_profile_service,
176+
sdk_key: sdk_key,
177+
config_manager: config_manager,
178+
notification_center: notification_center,
179+
event_processor: event_processor,
180+
settings: settings
183181
)
184182
end
185183
end

spec/audience_spec.rb

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

33
#
4-
# Copyright 2016-2017, 2019-2020, 2022, Optimizely and contributors
4+
# Copyright 2016-2017, 2019-2020, 2022-2023, 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.
@@ -25,7 +25,7 @@
2525
let(:config) { Optimizely::DatafileProjectConfig.new(config_body_JSON, spy_logger, error_handler) }
2626
let(:typed_audience_config) { Optimizely::DatafileProjectConfig.new(config_typed_audience_JSON, spy_logger, error_handler) }
2727
let(:integration_config) { Optimizely::DatafileProjectConfig.new(config_integration_JSON, spy_logger, error_handler) }
28-
let(:project_instance) { Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler) }
28+
let(:project_instance) { Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler) }
2929
let(:user_context) { project_instance.create_user_context('some-user', {}) }
3030
after(:example) { project_instance.close }
3131

spec/condition_tree_evaluator_spec.rb

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

33
#
4-
# Copyright 2019, Optimizely and contributors
4+
# Copyright 2019, 2023, 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.
@@ -27,19 +27,19 @@
2727

2828
describe 'evaluate' do
2929
it 'should return true for a leaf condition when the leaf condition evaluator returns true' do
30-
leaf_callback = ->(_condition) { return true }
30+
leaf_callback = ->(_condition) { true }
3131
expect(Optimizely::ConditionTreeEvaluator.evaluate(@browser_condition, leaf_callback)).to be true
3232
end
3333

3434
it 'should return false for a leaf condition when the leaf condition evaluator returns false' do
35-
leaf_callback = ->(_condition) { return false }
35+
leaf_callback = ->(_condition) { false }
3636
expect(Optimizely::ConditionTreeEvaluator.evaluate(@browser_condition, leaf_callback)).to be false
3737
end
3838
end
3939

4040
describe 'and evaluation' do
4141
it 'should return true when ALL conditions evaluate to true' do
42-
leaf_callback = ->(_condition) { return true }
42+
leaf_callback = ->(_condition) { true }
4343
expect(Optimizely::ConditionTreeEvaluator.evaluate(['and', @browser_condition, @device_condition], leaf_callback)).to be true
4444
end
4545

@@ -51,7 +51,7 @@
5151

5252
describe 'nil handling' do
5353
it 'should return nil when all operands evaluate to nil' do
54-
leaf_callback = ->(_condition) { return nil }
54+
leaf_callback = ->(_condition) { nil }
5555
expect(Optimizely::ConditionTreeEvaluator.evaluate(['and', @browser_condition, @device_condition], leaf_callback)).to eq(nil)
5656
end
5757

@@ -83,7 +83,7 @@
8383

8484
describe 'or evaluation' do
8585
it 'should return false if all conditions evaluate to false' do
86-
leaf_callback = ->(_condition) { return false }
86+
leaf_callback = ->(_condition) { false }
8787
expect(Optimizely::ConditionTreeEvaluator.evaluate(['or', @browser_condition, @device_condition], leaf_callback)).to be false
8888
end
8989

@@ -95,7 +95,7 @@
9595

9696
describe 'nil handling' do
9797
it 'should return nil when all operands evaluate to nil' do
98-
leaf_callback = ->(_condition) { return nil }
98+
leaf_callback = ->(_condition) { nil }
9999
expect(Optimizely::ConditionTreeEvaluator.evaluate(['or', @browser_condition, @device_condition], leaf_callback)).to eq(nil)
100100
end
101101

@@ -127,34 +127,34 @@
127127

128128
describe 'not evaluation' do
129129
it 'should return true if the condition evaluates to false' do
130-
leaf_callback = ->(_condition) { return false }
130+
leaf_callback = ->(_condition) { false }
131131
expect(Optimizely::ConditionTreeEvaluator.evaluate(['not', @browser_condition], leaf_callback)).to be true
132132
end
133133

134134
it 'should return false if the condition evaluates to true' do
135-
leaf_callback = ->(_condition) { return true }
135+
leaf_callback = ->(_condition) { true }
136136
expect(Optimizely::ConditionTreeEvaluator.evaluate(['not', @browser_condition], leaf_callback)).to be false
137137
end
138138

139139
it 'should return the result of negating the first condition, and ignore any additional conditions' do
140-
leaf_callback = ->(id) { return id == '1' }
140+
leaf_callback = ->(id) { id == '1' }
141141
expect(Optimizely::ConditionTreeEvaluator.evaluate(%w[not 1 2 1], leaf_callback)).to be false
142142

143-
leaf_callback2 = ->(id) { return id == '2' }
143+
leaf_callback2 = ->(id) { id == '2' }
144144
expect(Optimizely::ConditionTreeEvaluator.evaluate(%w[not 1 2 1], leaf_callback2)).to be true
145145

146-
leaf_callback3 = ->(id) { return id == '1' ? nil : id == '3' }
146+
leaf_callback3 = ->(id) { id == '1' ? nil : id == '3' }
147147
expect(Optimizely::ConditionTreeEvaluator.evaluate(%w[not 1 2 3], leaf_callback3)).to eq(nil)
148148
end
149149

150150
describe 'nil handling' do
151151
it 'should return nil when operand evaluates to nil' do
152-
leaf_callback = ->(_condition) { return nil }
152+
leaf_callback = ->(_condition) { nil }
153153
expect(Optimizely::ConditionTreeEvaluator.evaluate(['not', @browser_condition, @device_condition], leaf_callback)).to eq(nil)
154154
end
155155

156156
it 'should return nil when there are no operands' do
157-
leaf_callback = ->(_condition) { return nil }
157+
leaf_callback = ->(_condition) { nil }
158158
expect(Optimizely::ConditionTreeEvaluator.evaluate(['not'], leaf_callback)).to eq(nil)
159159
end
160160
end
@@ -166,7 +166,7 @@
166166
allow(leaf_callback).to receive(:call).and_return(true, false)
167167
expect(Optimizely::ConditionTreeEvaluator.evaluate([@browser_condition, @device_condition], leaf_callback)).to be true
168168

169-
leaf_callback = ->(_condition) { return false }
169+
leaf_callback = ->(_condition) { false }
170170
allow(leaf_callback).to receive(:call).and_return(false, true)
171171
expect(Optimizely::ConditionTreeEvaluator.evaluate([@browser_condition, @device_condition], leaf_callback)).to be true
172172
end

spec/decision_service_spec.rb

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

33
#
4-
# Copyright 2017-2020, Optimizely and contributors
4+
# Copyright 2017-2020, 2023, 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,7 +28,7 @@
2828
let(:spy_user_profile_service) { spy('user_profile_service') }
2929
let(:config) { Optimizely::DatafileProjectConfig.new(config_body_JSON, spy_logger, error_handler) }
3030
let(:decision_service) { Optimizely::DecisionService.new(spy_logger, spy_user_profile_service) }
31-
let(:project_instance) { Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler) }
31+
let(:project_instance) { Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler) }
3232
let(:user_context) { project_instance.create_user_context('some-user', {}) }
3333
after(:example) { project_instance.close }
3434

@@ -497,7 +497,7 @@
497497

498498
describe '#get_variation_for_feature_experiment' do
499499
config_body_json = OptimizelySpec::VALID_CONFIG_BODY_JSON
500-
project_instance = Optimizely::Project.new(config_body_json, nil, nil, nil)
500+
project_instance = Optimizely::Project.new(datafile: config_body_json)
501501
user_context = project_instance.create_user_context('user_1', {})
502502

503503
describe 'when the feature flag\'s experiment ids array is empty' do
@@ -619,7 +619,7 @@
619619

620620
describe '#get_variation_for_feature_rollout' do
621621
config_body_json = OptimizelySpec::VALID_CONFIG_BODY_JSON
622-
project_instance = Optimizely::Project.new(config_body_json, nil, nil, nil)
622+
project_instance = Optimizely::Project.new(datafile: config_body_json)
623623
user_context = project_instance.create_user_context('user_1', {})
624624
user_id = 'user_1'
625625

@@ -816,7 +816,7 @@
816816

817817
describe '#get_variation_for_feature' do
818818
config_body_json = OptimizelySpec::VALID_CONFIG_BODY_JSON
819-
project_instance = Optimizely::Project.new(config_body_json, nil, nil, nil)
819+
project_instance = Optimizely::Project.new(datafile: config_body_json)
820820
user_context = project_instance.create_user_context('user_1', {})
821821

822822
describe 'when the user is bucketed into the feature experiment' do

spec/notification_center_registry_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json")
4343
.to_return(status: 200, body: config_body_JSON)
4444

45-
project = Optimizely::Project.new(nil, nil, spy_logger, nil, false, nil, sdk_key)
45+
project = Optimizely::Project.new(logger: spy_logger, sdk_key: sdk_key)
4646

4747
notification_center = Optimizely::NotificationCenterRegistry.get_notification_center(sdk_key, spy_logger)
4848
expect(notification_center).to be_a Optimizely::NotificationCenter
@@ -60,7 +60,7 @@
6060
.to_return(status: 200, body: config_body_JSON)
6161

6262
notification_center = Optimizely::NotificationCenterRegistry.get_notification_center(sdk_key, spy_logger)
63-
project = Optimizely::Project.new(nil, nil, spy_logger, nil, false, nil, sdk_key)
63+
project = Optimizely::Project.new(logger: spy_logger, sdk_key: sdk_key)
6464

6565
expect(notification_center).to eq(Optimizely::NotificationCenterRegistry.get_notification_center(sdk_key, spy_logger))
6666
expect(spy_logger).not_to have_received(:log).with(Logger::ERROR, anything)
@@ -78,7 +78,7 @@
7878
notification_center = Optimizely::NotificationCenterRegistry.get_notification_center(sdk_key, spy_logger)
7979
expect(notification_center).to receive(:send_notifications).once
8080

81-
project = Optimizely::Project.new(nil, nil, spy_logger, nil, false, nil, sdk_key)
81+
project = Optimizely::Project.new(logger: spy_logger, sdk_key: sdk_key)
8282
project.config_manager.config
8383

8484
Optimizely::NotificationCenterRegistry.remove_notification_center(sdk_key)

0 commit comments

Comments
 (0)