diff --git a/README.md b/README.md index cbeb71ae..a9899291 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/optimizely.rb b/lib/optimizely.rb index 93f4fc3c..da6cbbf5 100644 --- a/lib/optimizely.rb +++ b/lib/optimizely.rb @@ -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 diff --git a/lib/optimizely/audience.rb b/lib/optimizely/audience.rb index 4c57261a..3e919ad8 100644 --- a/lib/optimizely/audience.rb +++ b/lib/optimizely/audience.rb @@ -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. @@ -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| diff --git a/lib/optimizely/event/event_factory.rb b/lib/optimizely/event/event_factory.rb index d8d5062e..9ac8a937 100644 --- a/lib/optimizely/event/event_factory.rb +++ b/lib/optimizely/event/event_factory.rb @@ -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. @@ -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) diff --git a/lib/optimizely/event_builder.rb b/lib/optimizely/event_builder.rb index 9b87413e..7b1ba51d 100644 --- a/lib/optimizely/event_builder.rb +++ b/lib/optimizely/event_builder.rb @@ -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. @@ -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) diff --git a/lib/optimizely/helpers/validator.rb b/lib/optimizely/helpers/validator.rb index 3ae2350a..d3baa447 100644 --- a/lib/optimizely/helpers/validator.rb +++ b/lib/optimizely/helpers/validator.rb @@ -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. @@ -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) @@ -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) diff --git a/lib/optimizely/optimizely_factory.rb b/lib/optimizely/optimizely_factory.rb index b6734872..04c7ecdd 100644 --- a/lib/optimizely/optimizely_factory.rb +++ b/lib/optimizely/optimizely_factory.rb @@ -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. @@ -103,7 +103,7 @@ 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 @@ -111,7 +111,7 @@ def self.default_instance(sdk_key, datafile = nil) # # @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. @@ -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 diff --git a/spec/audience_spec.rb b/spec/audience_spec.rb index 7777e804..eb997f0e 100644 --- a/spec/audience_spec.rb +++ b/spec/audience_spec.rb @@ -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. @@ -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 } diff --git a/spec/condition_tree_evaluator_spec.rb b/spec/condition_tree_evaluator_spec.rb index 68e99844..28dda143 100644 --- a/spec/condition_tree_evaluator_spec.rb +++ b/spec/condition_tree_evaluator_spec.rb @@ -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. @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/decision_service_spec.rb b/spec/decision_service_spec.rb index 7646c032..10f58792 100644 --- a/spec/decision_service_spec.rb +++ b/spec/decision_service_spec.rb @@ -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. @@ -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 } @@ -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 @@ -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' @@ -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 diff --git a/spec/notification_center_registry_spec.rb b/spec/notification_center_registry_spec.rb index ab783ef5..2a4521c7 100644 --- a/spec/notification_center_registry_spec.rb +++ b/spec/notification_center_registry_spec.rb @@ -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 @@ -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) @@ -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) diff --git a/spec/optimizely_config_spec.rb b/spec/optimizely_config_spec.rb index 8d364e1d..cfac6ba7 100644 --- a/spec/optimizely_config_spec.rb +++ b/spec/optimizely_config_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # -# Copyright 2019-2021, Optimizely and contributors +# Copyright 2019-2021, 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. @@ -26,16 +26,16 @@ let(:error_handler) { Optimizely::NoOpErrorHandler.new } let(:spy_logger) { spy('logger') } let(:project_config) { Optimizely::DatafileProjectConfig.new(config_body_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(:optimizely_config) { project_instance.get_optimizely_config } let(:project_config_sim_keys) { Optimizely::DatafileProjectConfig.new(similar_exp_keys_JSON, spy_logger, error_handler) } - let(:project_instance_sim_keys) { Optimizely::Project.new(similar_exp_keys_JSON, nil, spy_logger, error_handler) } + let(:project_instance_sim_keys) { Optimizely::Project.new(datafile: similar_exp_keys_JSON, logger: spy_logger, error_handler: error_handler) } let(:optimizely_config_sim_keys) { project_instance_sim_keys.get_optimizely_config } let(:project_config_typed_audiences) { Optimizely::DatafileProjectConfig.new(typed_audiences_JSON, spy_logger, error_handler) } - let(:project_instance_typed_audiences) { Optimizely::Project.new(typed_audiences_JSON, nil, spy_logger, error_handler) } + let(:project_instance_typed_audiences) { Optimizely::Project.new(datafile: typed_audiences_JSON, logger: spy_logger, error_handler: error_handler) } let(:optimizely_config_typed_audiences) { project_instance_typed_audiences.get_optimizely_config } let(:project_config_similar_rule_keys) { Optimizely::DatafileProjectConfig.new(similar_rule_key_JSON, spy_logger, error_handler) } - let(:project_instance_similar_rule_keys) { Optimizely::Project.new(similar_rule_key_JSON, nil, spy_logger, error_handler) } + let(:project_instance_similar_rule_keys) { Optimizely::Project.new(datafile: similar_rule_key_JSON, logger: spy_logger, error_handler: error_handler) } let(:optimizely_config_similar_rule_keys) { project_instance_similar_rule_keys.get_optimizely_config } after(:example) do project_instance.close diff --git a/spec/optimizely_user_context_spec.rb b/spec/optimizely_user_context_spec.rb index 6a99c57b..c968c336 100644 --- a/spec/optimizely_user_context_spec.rb +++ b/spec/optimizely_user_context_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # -# Copyright 2020, 2022, Optimizely and contributors +# Copyright 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. @@ -27,9 +27,9 @@ let(:integration_JSON) { OptimizelySpec::CONFIG_DICT_WITH_INTEGRATIONS_JSON } let(:error_handler) { Optimizely::RaiseErrorHandler.new } let(:spy_logger) { spy('logger') } - let(:project_instance) { Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler) } - let(:forced_decision_project_instance) { Optimizely::Project.new(forced_decision_JSON, nil, spy_logger, error_handler, false, nil, nil, nil, nil, nil, [], {batch_size: 1}) } - let(:integration_project_instance) { Optimizely::Project.new(integration_JSON, nil, spy_logger, error_handler) } + let(:project_instance) { Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler) } + let(:forced_decision_project_instance) { Optimizely::Project.new(datafile: forced_decision_JSON, logger: spy_logger, error_handler: error_handler, event_processor_options: {batch_size: 1}) } + let(:integration_project_instance) { Optimizely::Project.new(datafile: integration_JSON, logger: spy_logger, error_handler: error_handler) } let(:impression_log_url) { 'https://logx.optimizely.com/v1/events' } let(:good_response_data) do { diff --git a/spec/project_spec.rb b/spec/project_spec.rb index d00f93c1..2c1aeaca 100644 --- a/spec/project_spec.rb +++ b/spec/project_spec.rb @@ -48,7 +48,7 @@ let(:version) { Optimizely::VERSION } let(:impression_log_url) { 'https://logx.optimizely.com/v1/events' } let(:conversion_log_url) { 'https://logx.optimizely.com/v1/events' } - let(:project_instance) { Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler, false, nil, nil, nil, nil, nil, [], {batch_size: 1}) } + let(:project_instance) { Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler, event_processor_options: {batch_size: 1}) } let(:project_config) { project_instance.config_manager.config } let(:time_now) { Time.now } let(:post_headers) { {'Content-Type' => 'application/json'} } @@ -71,7 +71,7 @@ def log(_level, log_message) end logger = CustomLogger.new - instance_with_logger = Optimizely::Project.new(config_body_JSON, nil, logger) + instance_with_logger = Optimizely::Project.new(datafile: config_body_JSON, logger: logger) expect(instance_with_logger.logger.log(Logger::INFO, 'test_message')).to eq('test_message') instance_with_logger.close end @@ -84,19 +84,19 @@ def handle_error(error) end error_handler = CustomErrorHandler.new - instance_with_error_handler = Optimizely::Project.new(config_body_JSON, nil, nil, error_handler) + instance_with_error_handler = Optimizely::Project.new(datafile: config_body_JSON, error_handler: error_handler) expect(instance_with_error_handler.error_handler.handle_error('test_message')).to eq('test_message') instance_with_error_handler.close end it 'should log an error when datafile is null' do expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') - Optimizely::Project.new(nil, nil, spy_logger).close + Optimizely::Project.new(logger: spy_logger).close end it 'should log an error when datafile is empty' do expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') - Optimizely::Project.new('', nil, spy_logger).close + Optimizely::Project.new(datafile: '', logger: spy_logger).close end it 'should log an error when given a datafile that does not conform to the schema' do @@ -104,7 +104,7 @@ def handle_error(error) allow(spy_logger).to receive(:log).with(Logger::DEBUG, anything) expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'SDK key not provided/cannot be found in the datafile. ODP may not work properly without it.') - Optimizely::Project.new('{"foo": "bar"}', nil, spy_logger).close + Optimizely::Project.new(datafile: '{"foo": "bar"}', logger: spy_logger).close end it 'should log an error when given an invalid logger' do @@ -114,7 +114,7 @@ def handle_error(error) expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'Provided logger is in an invalid format.') class InvalidLogger; end # rubocop:disable Lint/ConstantDefinitionInBlock - Optimizely::Project.new(config_body_JSON, nil, InvalidLogger.new).close + Optimizely::Project.new(datafile: config_body_JSON, logger: InvalidLogger.new).close end it 'should log an error when given an invalid event_dispatcher' do @@ -123,7 +123,7 @@ class InvalidLogger; end # rubocop:disable Lint/ConstantDefinitionInBlock expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided event_dispatcher is in an invalid format.') class InvalidEventDispatcher; end # rubocop:disable Lint/ConstantDefinitionInBlock - Optimizely::Project.new(config_body_JSON, InvalidEventDispatcher.new).close + Optimizely::Project.new(datafile: config_body_JSON, event_dispatcher: InvalidEventDispatcher.new).close end it 'should log an error when given an invalid error_handler' do @@ -132,14 +132,14 @@ class InvalidEventDispatcher; end # rubocop:disable Lint/ConstantDefinitionInBlo expect_any_instance_of(Optimizely::SimpleLogger).to receive(:log).once.with(Logger::ERROR, 'Provided error_handler is in an invalid format.') class InvalidErrorHandler; end # rubocop:disable Lint/ConstantDefinitionInBlock - Optimizely::Project.new(config_body_JSON, nil, nil, InvalidErrorHandler.new).close + Optimizely::Project.new(datafile: config_body_JSON, error_handler: InvalidErrorHandler.new).close end it 'should not validate the JSON schema of the datafile when skip_json_validation is true' do project_instance.close expect(Optimizely::Helpers::Validator).not_to receive(:datafile_valid?) - Optimizely::Project.new(config_body_JSON, nil, nil, nil, true).close + Optimizely::Project.new(datafile: config_body_JSON, skip_json_validation: true).close end it 'should be invalid when datafile contains integrations missing key' do @@ -152,7 +152,7 @@ class InvalidErrorHandler; end # rubocop:disable Lint/ConstantDefinitionInBlock config['integrations'][0].delete('key') integrations_json = JSON.dump(config) - Optimizely::Project.new(integrations_json, nil, spy_logger) + Optimizely::Project.new(datafile: integrations_json, logger: spy_logger) end it 'should be valid when datafile contains integrations with only key' do @@ -161,7 +161,7 @@ class InvalidErrorHandler; end # rubocop:disable Lint/ConstantDefinitionInBlock config['integrations'].push('key' => '123') integrations_json = JSON.dump(config) - project_instance = Optimizely::Project.new(integrations_json) + project_instance = Optimizely::Project.new(datafile: integrations_json) expect(project_instance.is_valid).to be true end @@ -171,7 +171,7 @@ class InvalidErrorHandler; end # rubocop:disable Lint/ConstantDefinitionInBlock config['integrations'].push('key' => 'future', 'any-key-1' => 1, 'any-key-2' => 'any-value-2') integrations_json = JSON.dump(config) - project_instance = Optimizely::Project.new(integrations_json) + project_instance = Optimizely::Project.new(datafile: integrations_json) expect(project_instance.is_valid).to be true end @@ -179,20 +179,20 @@ class InvalidErrorHandler; end # rubocop:disable Lint/ConstantDefinitionInBlock expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') expect_any_instance_of(Optimizely::RaiseErrorHandler).to receive(:handle_error).once.with(Optimizely::InvalidInputError) - Optimizely::Project.new('this is not JSON', nil, spy_logger, Optimizely::RaiseErrorHandler.new, true) + Optimizely::Project.new(datafile: 'this is not JSON', logger: spy_logger, error_handler: Optimizely::RaiseErrorHandler.new, skip_json_validation: true) end it 'should log an error when provided an invalid JSON datafile and skip_json_validation is true' do expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') - Optimizely::Project.new('{"version": "2", "foo": "bar"}', nil, spy_logger, nil, true) + Optimizely::Project.new(datafile: '{"version": "2", "foo": "bar"}', logger: spy_logger, skip_json_validation: true) end it 'should log and raise an error when provided a datafile of unsupported version' do config_body_invalid_json = JSON.parse(config_body_invalid_JSON) expect(spy_logger).to receive(:log).once.with(Logger::ERROR, "This version of the Ruby SDK does not support the given datafile version: #{config_body_invalid_json['version']}.") - expect { Optimizely::Project.new(config_body_invalid_JSON, nil, spy_logger, Optimizely::RaiseErrorHandler.new, true) }.to raise_error(Optimizely::InvalidDatafileVersionError, 'This version of the Ruby SDK does not support the given datafile version: 5.') + expect { Optimizely::Project.new(datafile: config_body_invalid_JSON, logger: spy_logger, error_handler: Optimizely::RaiseErrorHandler.new, skip_json_validation: true) }.to raise_error(Optimizely::InvalidDatafileVersionError, 'This version of the Ruby SDK does not support the given datafile version: 5.') end end @@ -225,7 +225,7 @@ class InvalidErrorHandler; end # rubocop:disable Lint/ConstantDefinitionInBlock end it 'should send identify event when called with odp enabled' do - project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger) + project = Optimizely::Project.new(datafile: config_body_integrations_JSON, logger: spy_logger) expect(project.odp_manager).to receive(:identify_user).with({user_id: 'tester'}) project.create_user_context('tester') @@ -359,7 +359,7 @@ class InvalidErrorHandler; end # rubocop:disable Lint/ConstantDefinitionInBlock describe '.typed audiences' do before(:example) do - @project_typed_audience_instance = Optimizely::Project.new(JSON.dump(OptimizelySpec::CONFIG_DICT_WITH_TYPED_AUDIENCES), nil, spy_logger, error_handler, false, nil, nil, nil, nil, nil, [], {batch_size: 1}) + @project_typed_audience_instance = Optimizely::Project.new(datafile: JSON.dump(OptimizelySpec::CONFIG_DICT_WITH_TYPED_AUDIENCES), logger: spy_logger, error_handler: error_handler, event_processor_options: {batch_size: 1}) @project_config = @project_typed_audience_instance.config_manager.config @expected_activate_params = { account_id: '4879520872', @@ -900,7 +900,7 @@ def callback(_args); end end it 'should log an error when called with an invalid Project object' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) invalid_project.activate('test_exp', 'test_user') expect(spy_logger).to have_received(:log).with(Logger::ERROR, 'Provided datafile is in an invalid format.') expect(spy_logger).to have_received(:log).with(Logger::ERROR, "Optimizely instance is not valid. Failing 'activate'.") @@ -976,8 +976,8 @@ def callback(_args); end ) custom_project_instance = Optimizely::Project.new( - nil, nil, spy_logger, error_handler, - false, nil, nil, http_project_config_manager, notification_center + logger: spy_logger, error_handler: error_handler, + config_manager: http_project_config_manager, notification_center: notification_center ) sleep 0.1 until http_project_config_manager.ready? @@ -1003,8 +1003,8 @@ def callback(_args); end ) custom_project_instance = Optimizely::Project.new( - nil, nil, spy_logger, error_handler, - false, nil, nil, http_project_config_manager, notification_center + logger: spy_logger, error_handler: error_handler, + config_manager: http_project_config_manager, notification_center: notification_center ) sleep 0.1 until http_project_config_manager.ready? @@ -1037,8 +1037,8 @@ def callback(_args); end expect(notification_center).to receive(:send_notifications).ordered custom_project_instance = Optimizely::Project.new( - nil, nil, spy_logger, error_handler, - false, nil, sdk_key, nil, notification_center + logger: spy_logger, error_handler: error_handler, + sdk_key: sdk_key, notification_center: notification_center ) sleep 0.1 until custom_project_instance.config_manager.ready? @@ -1132,7 +1132,7 @@ def callback(_args); end end it 'should properly track an event with tags even when the project does not have a custom logger' do - custom_project_instance = Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler, false, nil, nil, nil, nil, nil, [], {batch_size: 1}) + custom_project_instance = Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler, event_processor_options: {batch_size: 1}) params = @expected_track_event_params params[:visitors][0][:snapshots][0][:events][0][:tags] = {revenue: 42} @@ -1217,7 +1217,7 @@ def callback(_args); end describe '.typed audiences' do before(:example) do - @project_typed_audience_instance = Optimizely::Project.new(JSON.dump(OptimizelySpec::CONFIG_DICT_WITH_TYPED_AUDIENCES), nil, spy_logger, error_handler, false, nil, nil, nil, nil, nil, [], {batch_size: 1}) + @project_typed_audience_instance = Optimizely::Project.new(datafile: JSON.dump(OptimizelySpec::CONFIG_DICT_WITH_TYPED_AUDIENCES), logger: spy_logger, error_handler: error_handler, event_processor_options: {batch_size: 1}) @expected_event_params = { account_id: '4879520872', project_id: '11624721371', @@ -1424,7 +1424,7 @@ def callback(_args); end end it 'should log an error when called with an invalid Project object' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) invalid_project.track('test_event', 'test_user') expect(spy_logger).to have_received(:log).with(Logger::ERROR, 'Provided datafile is in an invalid format.') expect(spy_logger).to have_received(:log).with(Logger::ERROR, "Optimizely instance is not valid. Failing 'track'.") @@ -1532,7 +1532,7 @@ def callback(_args); end end it 'should log an error when called with an invalid Project object' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) invalid_project.get_variation('test_exp', 'test_user') expect(spy_logger).to have_received(:log).with(Logger::ERROR, 'Provided datafile is in an invalid format.') expect(spy_logger).to have_received(:log).with(Logger::ERROR, "Optimizely instance is not valid. Failing 'get_variation'.") @@ -1620,7 +1620,7 @@ def callback(_args); end end it 'should return false when called with invalid project config' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) expect(invalid_project.is_feature_enabled('totally_invalid_feature_key', 'test_user')).to be false expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, "Optimizely instance is not valid. Failing 'is_feature_enabled'.") @@ -1758,7 +1758,7 @@ def callback(_args); end describe '.typed audiences' do before(:example) do - @project_typed_audience_instance = Optimizely::Project.new(JSON.dump(OptimizelySpec::CONFIG_DICT_WITH_TYPED_AUDIENCES), nil, spy_logger, error_handler) + @project_typed_audience_instance = Optimizely::Project.new(datafile: JSON.dump(OptimizelySpec::CONFIG_DICT_WITH_TYPED_AUDIENCES), logger: spy_logger, error_handler: error_handler) stub_request(:post, impression_log_url) end after(:example) do @@ -2023,7 +2023,7 @@ def callback(_args); end describe '#get_enabled_features' do it 'should return empty when called with invalid project config' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) expect(invalid_project.get_enabled_features('test_user')).to be_empty expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, "Optimizely instance is not valid. Failing 'get_enabled_features'.") @@ -2249,7 +2249,7 @@ def callback(_args); end user_attributes = {} it 'should return nil when called with invalid project config' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) expect(invalid_project.get_feature_variable_string('string_single_variable_feature', 'string_variable', user_id, user_attributes)) .to eq(nil) expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') @@ -2399,7 +2399,7 @@ def callback(_args); end user_attributes = {} it 'should return nil when called with invalid project config' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) expect(invalid_project.get_feature_variable_json('json_single_variable_feature', 'json_variable', user_id, user_attributes)) .to eq(nil) expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') @@ -2582,7 +2582,7 @@ def callback(_args); end user_attributes = {} it 'should return nil when called with invalid project config' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) expect(invalid_project.get_feature_variable_boolean('boolean_single_variable_feature', 'boolean_variable', user_id, user_attributes)) .to eq(nil) expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') @@ -2626,7 +2626,7 @@ def callback(_args); end user_attributes = {} it 'should return nil when called with invalid project config' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) expect(invalid_project.get_feature_variable_double('double_single_variable_feature', 'double_variable', user_id, user_attributes)) .to eq(nil) expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') @@ -2672,7 +2672,7 @@ def callback(_args); end user_attributes = {} it 'should return nil when called with invalid project config' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) expect(invalid_project.get_feature_variable_integer('integer_single_variable_feature', 'integer_variable', user_id, user_attributes)) .to eq(nil) expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') @@ -2718,7 +2718,7 @@ def callback(_args); end user_attributes = {} it 'should return nil when called with invalid project config' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) expect(invalid_project.get_all_feature_variables('all_variables_feature', user_id, user_attributes)) .to eq(nil) expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') @@ -2951,7 +2951,7 @@ def callback(_args); end user_attributes = {} it 'should return nil when called with invalid project config' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) expect(invalid_project.get_feature_variable('string_single_variable_feature', 'string_variable', user_id, user_attributes)) .to eq(nil) expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'Provided datafile is in an invalid format.') @@ -3161,7 +3161,7 @@ def callback(_args); end describe '.typed audiences' do before(:example) do - @project_typed_audience_instance = Optimizely::Project.new(JSON.dump(OptimizelySpec::CONFIG_DICT_WITH_TYPED_AUDIENCES), nil, spy_logger, error_handler) + @project_typed_audience_instance = Optimizely::Project.new(datafile: JSON.dump(OptimizelySpec::CONFIG_DICT_WITH_TYPED_AUDIENCES), logger: spy_logger, error_handler: error_handler) end after(:example) do @project_typed_audience_instance.close @@ -3466,7 +3466,7 @@ def callback(_args); end valid_variation = {id: '111128', key: 'control'} it 'should log an error when called with an invalid Project object' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) invalid_project.set_forced_variation(valid_experiment[:key], user_id, valid_variation[:key]) expect(spy_logger).to have_received(:log).with(Logger::ERROR, 'Provided datafile is in an invalid format.') expect(spy_logger).to have_received(:log).with(Logger::ERROR, "Optimizely instance is not valid. Failing 'set_forced_variation'.") @@ -3523,7 +3523,7 @@ def callback(_args); end valid_experiment = {id: '111127', key: 'test_experiment'} it 'should log an error when called with an invalid Project object' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) invalid_project.get_forced_variation(valid_experiment[:key], user_id) expect(spy_logger).to have_received(:log).with(Logger::ERROR, 'Provided datafile is in an invalid format.') expect(spy_logger).to have_received(:log).with(Logger::ERROR, "Optimizely instance is not valid. Failing 'get_forced_variation'.") @@ -3569,7 +3569,7 @@ def callback(_args); end describe '#is_valid' do it 'should return false when called with an invalid datafile' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) expect(invalid_project.is_valid).to be false invalid_project.close end @@ -3595,9 +3595,9 @@ def callback(_args); end event_processor = Optimizely::BatchEventProcessor.new(event_dispatcher: Optimizely::EventDispatcher.new) - Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler).close + Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler).close - project_instance = Optimizely::Project.new(nil, nil, nil, nil, true, nil, nil, config_manager, nil, event_processor) + project_instance = Optimizely::Project.new(skip_json_validation: true, config_manager: config_manager, event_processor: event_processor) expect(config_manager.stopped).to be false expect(event_processor.started).to be false @@ -3617,8 +3617,8 @@ def callback(_args); end ) project_instance = Optimizely::Project.new( - nil, nil, spy_logger, error_handler, - false, nil, nil, http_project_config_manager + logger: spy_logger, error_handler: error_handler, + config_manager: http_project_config_manager ) project_instance.close @@ -3631,8 +3631,8 @@ def callback(_args); end ) project_instance = Optimizely::Project.new( - config_body_JSON, nil, spy_logger, error_handler, - false, nil, nil, http_project_config_manager + datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler, + config_manager: http_project_config_manager ) sleep 0.1 until http_project_config_manager.ready? @@ -3652,8 +3652,8 @@ def callback(_args); end ) project_instance = Optimizely::Project.new( - nil, nil, spy_logger, error_handler, - false, nil, nil, static_project_config_manager + logger: spy_logger, error_handler: error_handler, + config_manager: static_project_config_manager ) project_instance.close @@ -3666,8 +3666,8 @@ def callback(_args); end ) project_instance = Optimizely::Project.new( - config_body_JSON, nil, spy_logger, error_handler, - false, nil, nil, static_project_config_manager + datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler, + config_manager: static_project_config_manager ) project_instance.close @@ -3696,7 +3696,7 @@ def callback(_args); end describe '#decide' do describe 'should return empty decision object with correct reason when sdk is not ready' do it 'when sdk is not ready' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) user_context = project_instance.create_user_context('user1') decision = invalid_project.decide(user_context, 'dummy_flag') expect(decision.as_json).to eq( @@ -4216,7 +4216,7 @@ def callback(_args); end describe '#decide_all' do it 'should get empty object when sdk is not ready' do - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) user_context = project_instance.create_user_context('user1') decisions = invalid_project.decide_all(user_context) expect(decisions).to eq({}) @@ -4283,7 +4283,7 @@ def callback(_args); end boolean_feature empty_feature ] - invalid_project = Optimizely::Project.new('invalid', nil, spy_logger) + invalid_project = Optimizely::Project.new(datafile: 'invalid', logger: spy_logger) user_context = project_instance.create_user_context('user1') decisions = invalid_project.decide_for_keys(user_context, keys) expect(decisions).to eq({}) @@ -4354,8 +4354,8 @@ def callback(_args); end it 'should get only enabled decisions for keys when ENABLED_FLAGS_ONLY is true in default_decide_options' do custom_project_instance = Optimizely::Project.new( - config_body_JSON, nil, spy_logger, error_handler, - false, nil, nil, nil, nil, nil, [Optimizely::Decide::OptimizelyDecideOption::ENABLED_FLAGS_ONLY] + datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler, + default_decide_options: [Optimizely::Decide::OptimizelyDecideOption::ENABLED_FLAGS_ONLY] ) keys = %w[ boolean_single_variable_feature @@ -4392,7 +4392,7 @@ def callback(_args); end describe 'default_decide_options' do describe 'EXCLUDE_VARIABLES' do it 'should include variables when the option is not set in default_decide_options' do - custom_project_instance = Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler) + custom_project_instance = Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler) experiment_to_return = config_body['experiments'][3] variation_to_return = experiment_to_return['variations'][0] decision_to_return = Optimizely::DecisionService::Decision.new( @@ -4418,8 +4418,8 @@ def callback(_args); end it 'should exclude variables when the option is set in default_decide_options' do custom_project_instance = Optimizely::Project.new( - config_body_JSON, nil, spy_logger, error_handler, - false, nil, nil, nil, nil, nil, [Optimizely::Decide::OptimizelyDecideOption::EXCLUDE_VARIABLES] + datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler, + default_decide_options: [Optimizely::Decide::OptimizelyDecideOption::EXCLUDE_VARIABLES] ) experiment_to_return = config_body['experiments'][3] variation_to_return = experiment_to_return['variations'][0] @@ -4448,8 +4448,8 @@ def callback(_args); end describe 'INCLUDE_REASONS' do it 'should include reasons when the option is set in default_decide_options' do custom_project_instance = Optimizely::Project.new( - config_body_JSON, nil, spy_logger, error_handler, - false, nil, nil, nil, nil, nil, [Optimizely::Decide::OptimizelyDecideOption::INCLUDE_REASONS] + datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler, + default_decide_options: [Optimizely::Decide::OptimizelyDecideOption::INCLUDE_REASONS] ) expect(custom_project_instance.notification_center).to receive(:send_notifications) .once.with(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:LOG_EVENT], any_args) @@ -4497,7 +4497,7 @@ def callback(_args); end end it 'should not include reasons when the option is not set in default_decide_options' do - custom_project_instance = Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler) + custom_project_instance = Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler) expect(custom_project_instance.notification_center).to receive(:send_notifications) .once.with(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:LOG_EVENT], any_args) expect(custom_project_instance.notification_center).to receive(:send_notifications) @@ -4532,7 +4532,7 @@ def callback(_args); end describe 'DISABLE_DECISION_EVENT' do it 'should send event when option is not set in default_decide_options' do - custom_project_instance = Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler) + custom_project_instance = Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler) experiment_to_return = config_body['experiments'][3] variation_to_return = experiment_to_return['variations'][0] expect(custom_project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) @@ -4549,8 +4549,8 @@ def callback(_args); end it 'should not send event when option is set in default_decide_options' do custom_project_instance = Optimizely::Project.new( - config_body_JSON, nil, spy_logger, error_handler, - false, nil, nil, nil, nil, nil, [Optimizely::Decide::OptimizelyDecideOption::DISABLE_DECISION_EVENT] + datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler, + default_decide_options: [Optimizely::Decide::OptimizelyDecideOption::DISABLE_DECISION_EVENT] ) experiment_to_return = config_body['experiments'][3] variation_to_return = experiment_to_return['variations'][0] @@ -4574,7 +4574,7 @@ def callback(_args); end stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(disable_odp: true) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) expect(project.odp_manager.instance_variable_get('@event_manager')).to be_nil expect(project.odp_manager.instance_variable_get('@segment_manager')).to be_nil project.close @@ -4587,7 +4587,7 @@ def callback(_args); end stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(odp_event_flush_interval: 0) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) event_manager = project.odp_manager.instance_variable_get('@event_manager') expect(event_manager.instance_variable_get('@flush_interval')).to eq 0 project.close @@ -4599,7 +4599,7 @@ def callback(_args); end stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(odp_event_flush_interval: nil) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) event_manager = project.odp_manager.instance_variable_get('@event_manager') expect(event_manager.instance_variable_get('@flush_interval')).to eq 1 project.close @@ -4612,7 +4612,7 @@ def callback(_args); end .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(segments_cache_size: 5) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) segment_manager = project.odp_manager.instance_variable_get('@segment_manager') expect(segment_manager.instance_variable_get('@segments_cache').capacity).to eq 5 project.close @@ -4624,7 +4624,7 @@ def callback(_args); end stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(segments_cache_timeout_in_secs: 5) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) segment_manager = project.odp_manager.instance_variable_get('@segment_manager') expect(segment_manager.instance_variable_get('@segments_cache').timeout).to eq 5 project.close @@ -4636,7 +4636,7 @@ def callback(_args); end stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(segments_cache_size: 10, segments_cache_timeout_in_secs: 5) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) segment_manager = project.odp_manager.instance_variable_get('@segment_manager') segments_cache = segment_manager.instance_variable_get('@segments_cache') expect(segments_cache.capacity).to eq 10 @@ -4650,7 +4650,7 @@ def callback(_args); end stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) segment_manager = project.odp_manager.instance_variable_get('@segment_manager') segments_cache = segment_manager.instance_variable_get('@segments_cache') expect(segments_cache.capacity).to eq 10_000 @@ -4664,7 +4664,7 @@ def callback(_args); end stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(segments_cache_size: 0, segments_cache_timeout_in_secs: 0) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) segment_manager = project.odp_manager.instance_variable_get('@segment_manager') segments_cache = segment_manager.instance_variable_get('@segments_cache') expect(segments_cache.capacity).to eq 0 @@ -4684,7 +4684,7 @@ def save(key, value); end stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(odp_segments_cache: CustomCache.new) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) segment_manager = project.odp_manager.instance_variable_get('@segment_manager') expect(segment_manager.instance_variable_get('@segments_cache')).to be_a CustomCache project.close @@ -4698,7 +4698,7 @@ class InvalidCustomCache; end # rubocop:disable Lint/ConstantDefinitionInBlock stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(odp_segments_cache: InvalidCustomCache.new) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) segment_manager = project.odp_manager.instance_variable_get('@segment_manager') expect(segment_manager.instance_variable_get('@segments_cache')).to be_a Optimizely::LRUCache @@ -4722,7 +4722,7 @@ def fetch_qualified_segments(user_key, user_value, options); end stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(odp_segment_manager: CustomSegmentManager.new) - project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger, error_handler, false, nil, nil, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(datafile: config_body_integrations_JSON, logger: spy_logger, error_handler: error_handler, settings: sdk_settings) segment_manager = project.odp_manager.instance_variable_get('@segment_manager') expect(segment_manager).to be_a CustomSegmentManager project.fetch_qualified_segments(user_id: 'test') @@ -4738,7 +4738,7 @@ class InvalidSegmentManager; end # rubocop:disable Lint/ConstantDefinitionInBloc stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(odp_segment_manager: InvalidSegmentManager.new) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) segment_manager = project.odp_manager.instance_variable_get('@segment_manager') expect(segment_manager).to be_a Optimizely::OdpSegmentManager @@ -4761,7 +4761,7 @@ def running?; end stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(odp_event_manager: CustomEventManager.new) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) event_manager = project.odp_manager.instance_variable_get('@event_manager') expect(event_manager).to be_a CustomEventManager project.send_odp_event(action: 'test', identifiers: {wow: 'great'}) @@ -4776,7 +4776,7 @@ class InvalidEventManager; end # rubocop:disable Lint/ConstantDefinitionInBlock stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: config_body_integrations_JSON) sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(odp_event_manager: InvalidEventManager.new) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) event_manager = project.odp_manager.instance_variable_get('@event_manager') expect(event_manager).to be_a Optimizely::OdpEventManager @@ -4791,7 +4791,7 @@ class InvalidEventManager; end # rubocop:disable Lint/ConstantDefinitionInBlock stub_request(:post, 'https://api.zaius.com/v3/events').to_return(status: 200) expect(spy_logger).to receive(:log).once.with(Logger::DEBUG, 'ODP event queue: flushing batch size 1.') expect(spy_logger).not_to receive(:log).with(Logger::ERROR, anything) - project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger) + project = Optimizely::Project.new(datafile: config_body_integrations_JSON, logger: spy_logger) project.send_odp_event(type: 'wow', action: 'great', identifiers: {amazing: 'fantastic'}, data: {}) project.close end @@ -4803,7 +4803,7 @@ class InvalidEventManager; end # rubocop:disable Lint/ConstantDefinitionInBlock stub_request(:post, 'https://api.zaius.com/v3/events').to_return(status: 200) expect(spy_logger).to receive(:log).once.with(Logger::DEBUG, 'ODP event queue: flushing batch size 1.') expect(spy_logger).not_to receive(:log).with(Logger::ERROR, anything) - project = Optimizely::Project.new(nil, nil, spy_logger, nil, false, nil, sdk_key) + project = Optimizely::Project.new(logger: spy_logger, sdk_key: sdk_key) sleep 0.1 until project.odp_manager.instance_variable_get('@event_manager').instance_variable_get('@event_queue').empty? @@ -4814,7 +4814,7 @@ class InvalidEventManager; end # rubocop:disable Lint/ConstantDefinitionInBlock it 'should log error when odp disabled' do expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'ODP is not enabled.') sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(disable_odp: true) - custom_project_instance = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger, error_handler, false, nil, nil, nil, nil, nil, [], {}, sdk_settings) + custom_project_instance = Optimizely::Project.new(datafile: config_body_integrations_JSON, logger: spy_logger, error_handler: error_handler, settings: sdk_settings) custom_project_instance.send_odp_event(type: 'wow', action: 'great', identifiers: {amazing: 'fantastic'}, data: {}) custom_project_instance.close end @@ -4823,7 +4823,7 @@ class InvalidEventManager; end # rubocop:disable Lint/ConstantDefinitionInBlock stub_request(:get, "https://cdn.optimizely.com/datafiles/#{sdk_key}.json") .to_return(status: 200, body: nil) expect(spy_logger).to receive(:log).once.with(Logger::ERROR, "Optimizely instance is not valid. Failing 'send_odp_event'.") - 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.send_odp_event(type: 'wow', action: 'great', identifiers: {amazing: 'fantastic'}, data: {}) project.close end @@ -4833,28 +4833,28 @@ class InvalidEventManager; end # rubocop:disable Lint/ConstantDefinitionInBlock .to_return(status: 200, body: config_body_integrations_JSON) expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'ODP is not enabled.') sdk_settings = Optimizely::Helpers::OptimizelySdkSettings.new(disable_odp: true) - project = Optimizely::Project.new(nil, nil, spy_logger, error_handler, false, nil, sdk_key, nil, nil, nil, [], {}, sdk_settings) + project = Optimizely::Project.new(logger: spy_logger, error_handler: error_handler, sdk_key: sdk_key, settings: sdk_settings) project.send_odp_event(type: 'wow', action: 'great', identifiers: {amazing: 'fantastic'}, data: {}) project.close end it 'should log error with invalid data' do expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'ODP data is not valid.') - project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger) + project = Optimizely::Project.new(datafile: config_body_integrations_JSON, logger: spy_logger) project.send_odp_event(type: 'wow', action: 'great', identifiers: {amazing: 'fantastic'}, data: {'wow': {}}) project.close end it 'should log error with empty identifiers' do expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'ODP events must have at least one key-value pair in identifiers.') - project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger) + project = Optimizely::Project.new(datafile: config_body_integrations_JSON, logger: spy_logger) project.send_odp_event(type: 'wow', action: 'great', identifiers: {}, data: {'wow': {}}) project.close end it 'should log error with nil identifiers' do expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'ODP events must have at least one key-value pair in identifiers.') - project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger) + project = Optimizely::Project.new(datafile: config_body_integrations_JSON, logger: spy_logger) project.send_odp_event(type: 'wow', action: 'great', identifiers: nil, data: {'wow': {}}) project.close end @@ -4864,7 +4864,7 @@ class InvalidEventManager; end # rubocop:disable Lint/ConstantDefinitionInBlock feature_key = 'flag-segment' user_id = 'test_user' - project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger) + project = Optimizely::Project.new(datafile: config_body_integrations_JSON, logger: spy_logger) allow(project.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event)) expect(project.odp_manager).not_to receive(:send_event) @@ -4881,20 +4881,20 @@ class InvalidEventManager; end # rubocop:disable Lint/ConstantDefinitionInBlock it 'should log error with nil action' do expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'ODP action is not valid (cannot be empty).') - project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger) + project = Optimizely::Project.new(datafile: config_body_integrations_JSON, logger: spy_logger) project.send_odp_event(type: 'wow', action: nil, identifiers: {amazing: 'fantastic'}, data: {}) project.close end it 'should log error with empty string action' do expect(spy_logger).to receive(:log).once.with(Logger::ERROR, 'ODP action is not valid (cannot be empty).') - project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger) + project = Optimizely::Project.new(datafile: config_body_integrations_JSON, logger: spy_logger) project.send_odp_event(type: 'wow', action: '', identifiers: {amazing: 'fantastic'}, data: {}) project.close end it 'should use default with nil type' do - project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger) + project = Optimizely::Project.new(datafile: config_body_integrations_JSON, logger: spy_logger) expect(project.odp_manager).to receive('send_event').with(type: 'fullstack', action: 'great', identifiers: {amazing: 'fantastic'}, data: {}) project.send_odp_event(type: nil, action: 'great', identifiers: {amazing: 'fantastic'}, data: {}) @@ -4904,7 +4904,7 @@ class InvalidEventManager; end # rubocop:disable Lint/ConstantDefinitionInBlock end it 'should use default with empty string type' do - project = Optimizely::Project.new(config_body_integrations_JSON, nil, spy_logger) + project = Optimizely::Project.new(datafile: config_body_integrations_JSON, logger: spy_logger) expect(project.odp_manager).to receive('send_event').with(type: 'fullstack', action: 'great', identifiers: {amazing: 'fantastic'}, data: {}) project.send_odp_event(type: '', action: 'great', identifiers: {amazing: 'fantastic'}, data: {}) diff --git a/spec/user_condition_evaluator_spec.rb b/spec/user_condition_evaluator_spec.rb index 7aef929e..0d74e514 100644 --- a/spec/user_condition_evaluator_spec.rb +++ b/spec/user_condition_evaluator_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # -# Copyright 2019-2020, Optimizely and contributors +# Copyright 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. @@ -28,7 +28,7 @@ let(:error_handler) { Optimizely::NoOpErrorHandler.new } let(:spy_logger) { spy('logger') } let(:event_processor) { Optimizely::ForwardingEventProcessor.new(Optimizely::EventDispatcher.new) } - let(:project_instance) { Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler, false, nil, nil, nil, nil, event_processor) } + let(:project_instance) { Optimizely::Project.new(datafile: config_body_JSON, logger: spy_logger, error_handler: error_handler, event_processor: event_processor) } let(:user_context) { project_instance.create_user_context('some-user', {}) } after(:example) { project_instance.close }