Skip to content

Commit

Permalink
[FSSDK-9382] switch client init args from positional to keyword (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewleap-optimizely authored Dec 8, 2023
1 parent 9487c0a commit ca43f1e
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 174 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ You can initialize the Optimizely instance in two ways: directly with a datafile
Initialize Optimizely with a datafile. This datafile will be used as ProjectConfig throughout the life of the Optimizely instance.

```ruby
optimizely_instance = Optimizely::Project.new(datafile)
optimizely_instance = Optimizely::Project.new(datafile: datafile)
```

#### Initialization by OptimizelyFactory
Expand Down
28 changes: 14 additions & 14 deletions lib/optimizely.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,20 @@ class Project
# @param event_processor_options: Optional hash of options to be passed to the default batch event processor.
# @param settings: Optional instance of OptimizelySdkSettings for sdk configuration.

def initialize( # rubocop:disable Metrics/ParameterLists
datafile = nil,
event_dispatcher = nil,
logger = nil,
error_handler = nil,
skip_json_validation = false, # rubocop:disable Style/OptionalBooleanParameter
user_profile_service = nil,
sdk_key = nil,
config_manager = nil,
notification_center = nil,
event_processor = nil,
default_decide_options = [],
event_processor_options = {},
settings = nil
def initialize(
datafile: nil,
event_dispatcher: nil,
logger: nil,
error_handler: nil,
skip_json_validation: false,
user_profile_service: nil,
sdk_key: nil,
config_manager: nil,
notification_center: nil,
event_processor: nil,
default_decide_options: [],
event_processor_options: {},
settings: nil
)
@logger = logger || NoOpLogger.new
@error_handler = error_handler || NoOpErrorHandler.new
Expand Down
4 changes: 2 additions & 2 deletions lib/optimizely/audience.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2016-2017, 2019-2020, Optimizely and contributors
# Copyright 2016-2017, 2019-2020, 2023, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,7 +59,7 @@ def user_meets_audience_conditions?(config, experiment, user_context, logger, lo
user_condition_evaluator = UserConditionEvaluator.new(user_context, logger)

evaluate_user_conditions = lambda do |condition|
return user_condition_evaluator.evaluate(condition)
user_condition_evaluator.evaluate(condition)
end

evaluate_audience = lambda do |audience_id|
Expand Down
4 changes: 2 additions & 2 deletions lib/optimizely/event/event_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2019-2020, 2022, Optimizely and contributors
# Copyright 2019-2020, 2022-2023, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -72,7 +72,7 @@ def create_log_event(user_events, logger)

def build_attribute_list(user_attributes, project_config)
visitor_attributes = []
user_attributes&.keys&.each do |attribute_key|
user_attributes&.each_key do |attribute_key|
# Omit attribute values that are not supported by the log endpoint.
attribute_value = user_attributes[attribute_key]
next unless Helpers::Validator.attribute_valid?(attribute_key, attribute_value)
Expand Down
4 changes: 2 additions & 2 deletions lib/optimizely/event_builder.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2016-2019, 2022, Optimizely and contributors
# Copyright 2016-2019, 2022-2023, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -62,7 +62,7 @@ def get_common_params(project_config, user_id, attributes)

visitor_attributes = []

attributes&.keys&.each do |attribute_key|
attributes&.each_key do |attribute_key|
# Omit attribute values that are not supported by the log endpoint.
attribute_value = attributes[attribute_key]
if Helpers::Validator.attribute_valid?(attribute_key, attribute_value)
Expand Down
12 changes: 5 additions & 7 deletions lib/optimizely/helpers/validator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2016-2019, 2022, Optimizely and contributors
# Copyright 2016-2019, 2022-2023, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -190,14 +190,13 @@ def segments_cache_valid?(segments_cache)
# segments_cache - custom cache to be validated.
#
# Returns boolean depending on whether cache has required methods.
(
segments_cache.respond_to?(:reset) &&

segments_cache.respond_to?(:reset) &&
segments_cache.method(:reset)&.parameters&.empty? &&
segments_cache.respond_to?(:lookup) &&
segments_cache.method(:lookup)&.parameters&.length&.positive? &&
segments_cache.respond_to?(:save) &&
segments_cache.method(:save)&.parameters&.length&.positive?
)
end

def segment_manager_valid?(segment_manager)
Expand All @@ -206,13 +205,12 @@ def segment_manager_valid?(segment_manager)
# segment_manager - custom manager to be validated.
#
# Returns boolean depending on whether manager has required methods.
(
segment_manager.respond_to?(:odp_config) &&

segment_manager.respond_to?(:odp_config) &&
segment_manager.respond_to?(:reset) &&
segment_manager.method(:reset)&.parameters&.empty? &&
segment_manager.respond_to?(:fetch_qualified_segments) &&
(segment_manager.method(:fetch_qualified_segments)&.parameters&.length || 0) >= 3
)
end

def event_manager_valid?(event_manager)
Expand Down
30 changes: 14 additions & 16 deletions lib/optimizely/optimizely_factory.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2019, 2022, Optimizely and contributors
# Copyright 2019, 2022-2023, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -103,15 +103,15 @@ def self.default_instance(sdk_key, datafile = nil)
)

Optimizely::Project.new(
datafile, nil, logger, error_handler, nil, nil, sdk_key, config_manager, notification_center
datafile: datafile, logger: logger, error_handler: error_handler, sdk_key: sdk_key, config_manager: config_manager, notification_center: notification_center
)
end

# Returns a new optimizely instance.
#
# @param config_manager - Required ConfigManagerInterface Responds to 'config' method.
def self.default_instance_with_config_manager(config_manager)
Optimizely::Project.new(nil, nil, nil, nil, nil, nil, nil, config_manager)
Optimizely::Project.new(config_manager: config_manager)
end

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

Optimizely::Project.new(
datafile,
event_dispatcher,
logger,
error_handler,
skip_json_validation,
user_profile_service,
sdk_key,
config_manager,
notification_center,
event_processor,
[],
{},
settings
datafile: datafile,
event_dispatcher: event_dispatcher,
logger: logger,
error_handler: error_handler,
skip_json_validation: skip_json_validation,
user_profile_service: user_profile_service,
sdk_key: sdk_key,
config_manager: config_manager,
notification_center: notification_center,
event_processor: event_processor,
settings: settings
)
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/audience_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2016-2017, 2019-2020, 2022, Optimizely and contributors
# Copyright 2016-2017, 2019-2020, 2022-2023, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,7 @@
let(:config) { Optimizely::DatafileProjectConfig.new(config_body_JSON, spy_logger, error_handler) }
let(:typed_audience_config) { Optimizely::DatafileProjectConfig.new(config_typed_audience_JSON, spy_logger, error_handler) }
let(:integration_config) { Optimizely::DatafileProjectConfig.new(config_integration_JSON, spy_logger, error_handler) }
let(:project_instance) { Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler) }
let(:project_instance) { Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler) }
let(:user_context) { project_instance.create_user_context('some-user', {}) }
after(:example) { project_instance.close }

Expand Down
30 changes: 15 additions & 15 deletions spec/condition_tree_evaluator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2019, Optimizely and contributors
# Copyright 2019, 2023, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -27,19 +27,19 @@

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

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

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

Expand All @@ -51,7 +51,7 @@

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

Expand Down Expand Up @@ -83,7 +83,7 @@

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

Expand All @@ -95,7 +95,7 @@

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

Expand Down Expand Up @@ -127,34 +127,34 @@

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

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

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

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

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

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

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

leaf_callback = ->(_condition) { return false }
leaf_callback = ->(_condition) { false }
allow(leaf_callback).to receive(:call).and_return(false, true)
expect(Optimizely::ConditionTreeEvaluator.evaluate([@browser_condition, @device_condition], leaf_callback)).to be true
end
Expand Down
10 changes: 5 additions & 5 deletions spec/decision_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2017-2020, Optimizely and contributors
# Copyright 2017-2020, 2023, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -28,7 +28,7 @@
let(:spy_user_profile_service) { spy('user_profile_service') }
let(:config) { Optimizely::DatafileProjectConfig.new(config_body_JSON, spy_logger, error_handler) }
let(:decision_service) { Optimizely::DecisionService.new(spy_logger, spy_user_profile_service) }
let(:project_instance) { Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler) }
let(:project_instance) { Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler) }
let(:user_context) { project_instance.create_user_context('some-user', {}) }
after(:example) { project_instance.close }

Expand Down Expand Up @@ -497,7 +497,7 @@

describe '#get_variation_for_feature_experiment' do
config_body_json = OptimizelySpec::VALID_CONFIG_BODY_JSON
project_instance = Optimizely::Project.new(config_body_json, nil, nil, nil)
project_instance = Optimizely::Project.new(datafile: config_body_json)
user_context = project_instance.create_user_context('user_1', {})

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

describe '#get_variation_for_feature_rollout' do
config_body_json = OptimizelySpec::VALID_CONFIG_BODY_JSON
project_instance = Optimizely::Project.new(config_body_json, nil, nil, nil)
project_instance = Optimizely::Project.new(datafile: config_body_json)
user_context = project_instance.create_user_context('user_1', {})
user_id = 'user_1'

Expand Down Expand Up @@ -816,7 +816,7 @@

describe '#get_variation_for_feature' do
config_body_json = OptimizelySpec::VALID_CONFIG_BODY_JSON
project_instance = Optimizely::Project.new(config_body_json, nil, nil, nil)
project_instance = Optimizely::Project.new(datafile: config_body_json)
user_context = project_instance.create_user_context('user_1', {})

describe 'when the user is bucketed into the feature experiment' do
Expand Down
6 changes: 3 additions & 3 deletions spec/notification_center_registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json")
.to_return(status: 200, body: config_body_JSON)

project = Optimizely::Project.new(nil, nil, spy_logger, nil, false, nil, sdk_key)
project = Optimizely::Project.new(logger: spy_logger, sdk_key: sdk_key)

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

notification_center = Optimizely::NotificationCenterRegistry.get_notification_center(sdk_key, spy_logger)
project = Optimizely::Project.new(nil, nil, spy_logger, nil, false, nil, sdk_key)
project = Optimizely::Project.new(logger: spy_logger, sdk_key: sdk_key)

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

project = Optimizely::Project.new(nil, nil, spy_logger, nil, false, nil, sdk_key)
project = Optimizely::Project.new(logger: spy_logger, sdk_key: sdk_key)
project.config_manager.config

Optimizely::NotificationCenterRegistry.remove_notification_center(sdk_key)
Expand Down
Loading

0 comments on commit ca43f1e

Please sign in to comment.