Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/optimizely.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

#
# Copyright 2016-2019, Optimizely and contributors
# Copyright 2016-2020, 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 @@ -566,8 +566,13 @@ def get_optimizely_config
return nil
end

optimizely_config = OptimizelyConfig.new(project_config)
optimizely_config.config
# config_manager might not contain optimizely_config if its supplied by the consumer
# Generating a new OptimizelyConfig object in this case as a fallback
if @config_manager.respond_to?(:optimizely_config)
@config_manager.optimizely_config
else
OptimizelyConfig.new(project_config).config
end
end

private
Expand Down
7 changes: 5 additions & 2 deletions lib/optimizely/config_manager/http_project_config_manager.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-2020, 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 @@ -22,6 +22,7 @@
require_relative '../logger'
require_relative '../notification_center'
require_relative '../project_config'
require_relative '../optimizely_config'
require_relative 'project_config_manager'
require_relative 'async_scheduler'
require 'httparty'
Expand All @@ -30,7 +31,7 @@ module Optimizely
class HTTPProjectConfigManager < ProjectConfigManager
# Config manager that polls for the datafile and updated ProjectConfig based on an update interval.

attr_reader :stopped
attr_reader :stopped, :optimizely_config

# Initialize config manager. One of sdk_key or url has to be set to be able to use.
#
Expand Down Expand Up @@ -73,6 +74,7 @@ def initialize(
@skip_json_validation = skip_json_validation
@notification_center = notification_center.is_a?(Optimizely::NotificationCenter) ? notification_center : NotificationCenter.new(@logger, @error_handler)
@config = datafile.nil? ? nil : DatafileProjectConfig.create(datafile, @logger, @error_handler, @skip_json_validation)
@optimizely_config = @config.nil? ? nil : OptimizelyConfig.new(@config).config
@mutex = Mutex.new
@resource = ConditionVariable.new
@async_scheduler = AsyncScheduler.new(method(:fetch_datafile_config), @polling_interval, auto_update, @logger)
Expand Down Expand Up @@ -192,6 +194,7 @@ def set_config(config)
end

@config = config
@optimizely_config = OptimizelyConfig.new(config).config

@notification_center.send_notifications(NotificationCenter::NOTIFICATION_TYPES[:OPTIMIZELY_CONFIG_UPDATE])

Expand Down
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-2020, 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 @@ -17,11 +17,13 @@
#

require_relative '../config/datafile_project_config'
require_relative '../optimizely_config'
require_relative 'project_config_manager'

module Optimizely
class StaticProjectConfigManager < ProjectConfigManager
# Implementation of ProjectConfigManager interface.
attr_reader :config
attr_reader :config, :optimizely_config

def initialize(datafile, logger, error_handler, skip_json_validation)
# Looks up and sets datafile and config based on response body.
Expand All @@ -38,6 +40,8 @@ def initialize(datafile, logger, error_handler, skip_json_validation)
error_handler,
skip_json_validation
)

@optimizely_config = @config.nil? ? nil : OptimizelyConfig.new(@config).config
end
end
end
15 changes: 14 additions & 1 deletion spec/config_manager/http_project_config_manager_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-2020, 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 @@ -423,4 +423,17 @@
expect(spy_logger).to have_received(:log).once.with(Logger::DEBUG, "Blocking timeout '999999999999999999' has invalid range. Defaulting to 15 seconds.")
end
end

describe 'optimizely_config' do
it 'optimizely_config object updates correctly when new config is recieved' do
@http_project_config_manager = Optimizely::HTTPProjectConfigManager.new(
sdk_key: 'valid_sdk_key',
datafile: config_body_JSON,
polling_interval: 0.1
)
expect(@http_project_config_manager.optimizely_config['revision']).to eq('42')
sleep 0.3
expect(@http_project_config_manager.optimizely_config['revision']).to eq('81')
end
end
end