-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
206 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'configcat/overridedatasource' | ||
require 'configcat/constants' | ||
|
||
|
||
module ConfigCat | ||
class LocalDictionaryDataSource < OverrideDataSource | ||
def initialize(source, override_behaviour) | ||
super(override_behaviour) | ||
dictionary = {} | ||
source.each do |key, value| | ||
dictionary[key] = {VALUE => value} | ||
end | ||
@_settings = {FEATURE_FLAGS => dictionary} | ||
end | ||
|
||
def get_overrides() | ||
return @_settings | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module ConfigCat | ||
class OverrideBehaviour | ||
# When evaluating values, the SDK will not use feature flags & settings from the ConfigCat CDN, but it will use | ||
# all feature flags & settings that are loaded from local-override sources. | ||
LOCAL_ONLY = 0 | ||
|
||
# When evaluating values, the SDK will use all feature flags & settings that are downloaded from the ConfigCat CDN, | ||
# plus all feature flags & settings that are loaded from local-override sources. If a feature flag or a setting is | ||
# defined both in the fetched and the local-override source then the local-override version will take precedence. | ||
LOCAL_OVER_REMOTE = 1 | ||
|
||
# When evaluating values, the SDK will use all feature flags & settings that are downloaded from the ConfigCat CDN, | ||
# plus all feature flags & settings that are loaded from local-override sources. If a feature flag or a setting is | ||
# defined both in the fetched and the local-override source then the fetched version will take precedence. | ||
REMOTE_OVER_LOCAL = 2 | ||
end | ||
|
||
class OverrideDataSource | ||
def initialize(override_behaviour) | ||
@_override_behaviour = override_behaviour | ||
end | ||
|
||
def get_behaviour() | ||
return @_override_behaviour | ||
end | ||
|
||
def get_overrides() | ||
# :returns the override dictionary | ||
return {} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require 'spec_helper' | ||
require 'configcat/localdictionarydatasource' | ||
|
||
|
||
RSpec.describe 'Local test', type: :feature do | ||
|
||
def stub_request() | ||
uri_template = Addressable::Template.new "https://{base_url}/{base_path}/{api_key}/{base_ext}" | ||
json = '{"f": {"fakeKey": {"v": false} } }' | ||
WebMock.stub_request(:get, uri_template) | ||
.with( | ||
body: "", | ||
headers: { | ||
'Accept' => '*/*', | ||
'Content-Type' => 'application/json', | ||
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3' | ||
} | ||
) | ||
.to_return(status: 200, body: json, headers: {}) | ||
end | ||
|
||
it "test dictionary" do | ||
dictionary = { | ||
"enabledFeature" => true, | ||
"disabledFeature" => false, | ||
"intSetting" => 5, | ||
"doubleSetting" => 3.14, | ||
"stringSetting" => "test" | ||
} | ||
client = ConfigCat::ConfigCatClient.new("test", | ||
poll_interval_seconds: 0, | ||
max_init_wait_time_seconds: 0, | ||
flag_overrides: ConfigCat::LocalDictionaryDataSource.new(dictionary, ConfigCat::OverrideBehaviour::LOCAL_ONLY)) | ||
expect(client.get_value("enabledFeature", false)).to eq true | ||
expect(client.get_value("disabledFeature", true)).to eq false | ||
expect(client.get_value("intSetting", 0)).to eq 5 | ||
expect(client.get_value("doubleSetting", 0.0)).to eq 3.14 | ||
expect(client.get_value("stringSetting", "")).to eq "test" | ||
client.stop() | ||
end | ||
|
||
it "test local over remote" do | ||
stub_request() | ||
dictionary = { | ||
"fakeKey" => true, | ||
"nonexisting" => true | ||
} | ||
client = ConfigCat::ConfigCatClient.new("test", | ||
poll_interval_seconds: 0, | ||
max_init_wait_time_seconds: 0, | ||
flag_overrides: ConfigCat::LocalDictionaryDataSource.new(dictionary, ConfigCat::OverrideBehaviour::LOCAL_OVER_REMOTE)) | ||
expect(client.get_value("fakeKey", false)).to eq true | ||
expect(client.get_value("nonexisting", false)).to eq true | ||
client.force_refresh() | ||
client.stop() | ||
end | ||
|
||
it "test remote over local" do | ||
stub_request() | ||
dictionary = { | ||
"fakeKey" => true, | ||
"nonexisting" => true | ||
} | ||
client = ConfigCat::ConfigCatClient.new("test", | ||
poll_interval_seconds: 0, | ||
max_init_wait_time_seconds: 0, | ||
flag_overrides: ConfigCat::LocalDictionaryDataSource.new(dictionary, ConfigCat::OverrideBehaviour::REMOTE_OVER_LOCAL)) | ||
expect(client.get_value("fakeKey", true)).to eq false | ||
expect(client.get_value("nonexisting", false)).to eq true | ||
client.force_refresh() | ||
client.stop() | ||
end | ||
end |