Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-cat committed Apr 11, 2022
1 parent 98bd1b2 commit 34b786f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 44 deletions.
72 changes: 32 additions & 40 deletions lib/configcat/configcatclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,24 @@ def initialize(sdk_key,
if !@_override_data_source.equal?(nil) && @_override_data_source.get_behaviour() == OverrideBehaviour::LOCAL_ONLY
@_config_fetcher = nil
@_cache_policy = nil
elsif poll_interval_seconds > 0
@_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "p", base_url: base_url,
proxy_address: proxy_address, proxy_port: proxy_port, proxy_user: proxy_user, proxy_pass: proxy_pass,
open_timeout: open_timeout, read_timeout: read_timeout,
data_governance: data_governance)
@_cache_policy = AutoPollingCachePolicy.new(@_config_fetcher, @_config_cache, _get_cache_key(), poll_interval_seconds, max_init_wait_time_seconds, on_configuration_changed_callback)
elsif cache_time_to_live_seconds > 0
@_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "l", base_url: base_url,
proxy_address: proxy_address, proxy_port: proxy_port, proxy_user: proxy_user, proxy_pass: proxy_pass,
open_timeout: open_timeout, read_timeout: read_timeout,
data_governance: data_governance)
@_cache_policy = LazyLoadingCachePolicy.new(@_config_fetcher, @_config_cache, _get_cache_key(), cache_time_to_live_seconds)
else
if poll_interval_seconds > 0
@_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "p", base_url: base_url,
proxy_address: proxy_address, proxy_port: proxy_port, proxy_user: proxy_user, proxy_pass: proxy_pass,
open_timeout: open_timeout, read_timeout: read_timeout,
data_governance: data_governance)
@_cache_policy = AutoPollingCachePolicy.new(@_config_fetcher, @_config_cache, _get_cache_key(), poll_interval_seconds, max_init_wait_time_seconds, on_configuration_changed_callback)
else
if cache_time_to_live_seconds > 0
@_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "l", base_url: base_url,
proxy_address: proxy_address, proxy_port: proxy_port, proxy_user: proxy_user, proxy_pass: proxy_pass,
open_timeout: open_timeout, read_timeout: read_timeout,
data_governance: data_governance)
@_cache_policy = LazyLoadingCachePolicy.new(@_config_fetcher, @_config_cache, _get_cache_key(), cache_time_to_live_seconds)
else
@_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "m", base_url: base_url,
proxy_address: proxy_address, proxy_port: proxy_port, proxy_user: proxy_user, proxy_pass: proxy_pass,
open_timeout: open_timeout, read_timeout: read_timeout,
data_governance: data_governance)
@_cache_policy = ManualPollingCachePolicy.new(@_config_fetcher, @_config_cache, _get_cache_key())
end
end
@_config_fetcher = CacheControlConfigFetcher.new(sdk_key, "m", base_url: base_url,
proxy_address: proxy_address, proxy_port: proxy_port, proxy_user: proxy_user, proxy_pass: proxy_pass,
open_timeout: open_timeout, read_timeout: read_timeout,
data_governance: data_governance)
@_cache_policy = ManualPollingCachePolicy.new(@_config_fetcher, @_config_cache, _get_cache_key())
end
end

Expand Down Expand Up @@ -187,26 +183,22 @@ def _get_settings()
behaviour = @_override_data_source.get_behaviour()
if behaviour == OverrideBehaviour::LOCAL_ONLY
return @_override_data_source.get_overrides()
else
if behaviour == OverrideBehaviour::REMOTE_OVER_LOCAL
remote_settings = @_cache_policy.get()
local_settings = @_override_data_source.get_overrides()
result = local_settings.clone()
if remote_settings.key?(FEATURE_FLAGS) && local_settings.key?(FEATURE_FLAGS)
result[FEATURE_FLAGS] = result[FEATURE_FLAGS].merge(remote_settings[FEATURE_FLAGS])
end
return result
else
if behaviour == OverrideBehaviour::LOCAL_OVER_REMOTE
remote_settings = @_cache_policy.get()
local_settings = @_override_data_source.get_overrides()
result = remote_settings.clone()
if remote_settings.key?(FEATURE_FLAGS) && local_settings.key?(FEATURE_FLAGS)
result[FEATURE_FLAGS] = result[FEATURE_FLAGS].merge(local_settings[FEATURE_FLAGS])
end
return result
end
elsif behaviour == OverrideBehaviour::REMOTE_OVER_LOCAL
remote_settings = @_cache_policy.get()
local_settings = @_override_data_source.get_overrides()
result = local_settings.clone()
if remote_settings.key?(FEATURE_FLAGS) && local_settings.key?(FEATURE_FLAGS)
result[FEATURE_FLAGS] = result[FEATURE_FLAGS].merge(remote_settings[FEATURE_FLAGS])
end
return result
elsif behaviour == OverrideBehaviour::LOCAL_OVER_REMOTE
remote_settings = @_cache_policy.get()
local_settings = @_override_data_source.get_overrides()
result = remote_settings.clone()
if remote_settings.key?(FEATURE_FLAGS) && local_settings.key?(FEATURE_FLAGS)
result[FEATURE_FLAGS] = result[FEATURE_FLAGS].merge(local_settings[FEATURE_FLAGS])
end
return result
end
end
return @_cache_policy.get()
Expand Down
9 changes: 5 additions & 4 deletions spec/datagovernance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

RSpec.describe 'Data governance tests', type: :feature do
BASE_URL_FORCED = "https://forced.configcat.com"
BASE_URL_CUSTOM = "https://custom.configcat.com"
URI_GLOBAL = ConfigCat::BASE_URL_GLOBAL + "/" + ConfigCat::BASE_PATH + ConfigCat::BASE_EXTENSION
URI_EU_ONLY = ConfigCat::BASE_URL_EU_ONLY + "/" + ConfigCat::BASE_PATH + ConfigCat::BASE_EXTENSION
URI_CUSTOM = "https://custom.configcat.com/" + ConfigCat::BASE_PATH + ConfigCat::BASE_EXTENSION
URI_CUSTOM = BASE_URL_CUSTOM + "/" + ConfigCat::BASE_PATH + ConfigCat::BASE_EXTENSION
URI_FORCED = BASE_URL_FORCED + "/" + ConfigCat::BASE_PATH + ConfigCat::BASE_EXTENSION
TEST_JSON = '{"test": "json"}'

Expand Down Expand Up @@ -142,7 +143,7 @@ def stub_request(request_uri, response_uri, redirect)
custom_stub = stub_request(URI_CUSTOM, ConfigCat::BASE_URL_GLOBAL, 0)

fetcher = ConfigCat::CacheControlConfigFetcher.new("", "m",
base_url: "https://custom.configcat.com",
base_url: BASE_URL_CUSTOM,
data_governance: ConfigCat::DataGovernance::GLOBAL)

# First fetch
Expand Down Expand Up @@ -173,7 +174,7 @@ def stub_request(request_uri, response_uri, redirect)
custom_stub = stub_request(URI_CUSTOM, ConfigCat::BASE_URL_GLOBAL, 0)

fetcher = ConfigCat::CacheControlConfigFetcher.new("", "m",
base_url: "https://custom.configcat.com",
base_url: BASE_URL_CUSTOM,
data_governance: ConfigCat::DataGovernance::EU_ONLY)

# First fetch
Expand Down Expand Up @@ -265,7 +266,7 @@ def stub_request(request_uri, response_uri, redirect)
custom_to_forced_stub = stub_request(URI_CUSTOM, BASE_URL_FORCED, 2)

fetcher = ConfigCat::CacheControlConfigFetcher.new("", "m",
base_url: "https://custom.configcat.com",
base_url: BASE_URL_CUSTOM,
data_governance: ConfigCat::DataGovernance::GLOBAL)

# First fetch
Expand Down

0 comments on commit 34b786f

Please sign in to comment.