From 56ccf17ec340df0ea14a72ea7379c51dbb9d7b13 Mon Sep 17 00:00:00 2001 From: Will Kim Date: Fri, 29 Mar 2024 12:48:46 -0400 Subject: [PATCH] fix: Add domain to build_client (#109) Signed-off-by: William Kim --- README.md | 16 +++++----- lib/open_feature/sdk/api.rb | 10 +++--- lib/open_feature/sdk/metadata.rb | 9 ++++-- spec/open_feature/sdk/api_spec.rb | 47 +++++++++++++++++++++++++++- spec/open_feature/sdk/client_spec.rb | 4 ++- 5 files changed, 69 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 218cf6b..24d10e4 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,11 @@ We support multiple data types for flags (numbers, strings, booleans, objects) a ## Support Matrix -| Ruby Version | OS | -| ----------- | ----------- | -| Ruby 3.1.4 | Windows, MacOS, Linux | -| Ruby 3.2.3 | Windows, MacOS, Linux | -| Ruby 3.3.0 | Windows, MacOS, Linux | - +| Ruby Version | OS | +| ------------ | --------------------- | +| Ruby 3.1.4 | Windows, MacOS, Linux | +| Ruby 3.2.3 | Windows, MacOS, Linux | +| Ruby 3.3.0 | Windows, MacOS, Linux | ## Installation @@ -54,7 +53,9 @@ OpenFeature::SDK.configure do |config| end # Create a client -client = OpenFeature::SDK.build_client(name: "my-app") +client = OpenFeature::SDK.build_client +# Create a client for a different domain, this will use the provider assigned to that domain +legacy_flag_client = OpenFeature::SDK.build_client(domain: "legacy_flags") # fetching boolean value feature flag bool_value = client.fetch_boolean_value(flag_key: 'boolean_flag', default_value: false) @@ -102,7 +103,6 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to the O Our community meetings are held regularly and open to everyone. Check the [OpenFeature community calendar](https://calendar.google.com/calendar/u/0?cid=MHVhN2kxaGl2NWRoMThiMjd0b2FoNjM2NDRAZ3JvdXAuY2FsZW5kYXIuZ29vZ2xlLmNvbQ) for specific dates and for the Zoom meeting links. - ## License [Apache License 2.0](LICENSE) diff --git a/lib/open_feature/sdk/api.rb b/lib/open_feature/sdk/api.rb index 7ad7e2f..1ecb030 100644 --- a/lib/open_feature/sdk/api.rb +++ b/lib/open_feature/sdk/api.rb @@ -42,10 +42,12 @@ def configure(&block) block.call(configuration) end - def build_client(name: nil, version: nil) - client_options = Metadata.new(name: name, version: version).freeze - provider = Provider::NoOpProvider.new if provider.nil? - Client.new(provider: provider, client_options: client_options, context: context) + def build_client(name: nil, version: nil, domain: nil) + client_options = Metadata.new(name: name, version: version, domain: domain).freeze + + active_provider = provider(domain:).nil? ? Provider::NoOpProvider.new : provider(domain:) + + Client.new(provider: active_provider, client_options:, context:) end end end diff --git a/lib/open_feature/sdk/metadata.rb b/lib/open_feature/sdk/metadata.rb index 411ae1d..f1af85a 100644 --- a/lib/open_feature/sdk/metadata.rb +++ b/lib/open_feature/sdk/metadata.rb @@ -10,19 +10,22 @@ module SDK # # * version - Allows you to specify version of the Metadata structure # + # * domain - Allows you to specify the domain of the Metadata structure + # # Usage: # - # metadata = Metadata.new(name: 'name-for-metadata', version: 'v1.1.3') + # metadata = Metadata.new(name: 'name-for-metadata', version: 'v1.1.3', domain: 'test') # metadata.name # 'name-for-metadata' # metadata.version # version # metadata_two = Metadata.new(name: 'name-for-metadata') # metadata_two == metadata # true - equality based on values class Metadata - attr_reader :name, :version + attr_reader :name, :version, :domain - def initialize(name:, version: nil) + def initialize(name:, version: nil, domain: nil) @name = name @version = version + @domain = domain end def ==(other) diff --git a/spec/open_feature/sdk/api_spec.rb b/spec/open_feature/sdk/api_spec.rb index 1b3be34..d6488ef 100644 --- a/spec/open_feature/sdk/api_spec.rb +++ b/spec/open_feature/sdk/api_spec.rb @@ -55,7 +55,7 @@ config.set_provider(OpenFeature::SDK::Provider::NoOpProvider.new) end - api.build_client(name: "requirement-1.1.5") + api.build_client end it "provide a function for creating a client which accepts the following options: * name (optional): A logical string identifier for the client." do @@ -70,4 +70,49 @@ context "with Requirement 1.1.6" do pending end + + # TODO: These tests should be re-enabled when the client's provider is exposed via the metadata + # See this PR for context: https://github.com/open-feature/ruby-sdk/pull/109 + + # context "when domain is given" do + # it "can generate a client both with and without that domain" do + # provider = OpenFeature::SDK::Provider::InMemoryProvider.new + + # api.configure do |config| + # config.set_provider(provider, domain: "testing1") + # end + + # client = api.build_client(domain: "testing1") + # no_domain_client = api.build_client + + # expect(client.provider).to be(provider) + # expect(no_domain_client.provider).to be_an_instance_of(OpenFeature::SDK::Provider::NoOpProvider) + # end + # end + + # context "when domain is not provided" do + # it "can generate a client without a domain properly" do + # provider = OpenFeature::SDK::Provider::InMemoryProvider.new + + # api.configure do |config| + # config.set_provider(provider) + # end + + # no_domain_client = api.build_client + + # expect(no_domain_client.provider).to be(provider) + # end + + # it "can generate a client with a domain properly" do + # api.configure do |config| + # config.set_provider(OpenFeature::SDK::Provider::InMemoryProvider.new) + # end + + # domain_client = api.build_client(domain: "testing2") + # # This domain was never given a provider, so it should default to the NoOpProvider + # expect(domain_client.provider).to be_an_instance_of(OpenFeature::SDK::Provider::NoOpProvider) + # end + # end + + # End Client provider metadata tests end diff --git a/spec/open_feature/sdk/client_spec.rb b/spec/open_feature/sdk/client_spec.rb index 38b6d8e..6f08c74 100644 --- a/spec/open_feature/sdk/client_spec.rb +++ b/spec/open_feature/sdk/client_spec.rb @@ -7,7 +7,8 @@ RSpec.describe OpenFeature::SDK::Client do subject(:client) { described_class.new(provider: provider, client_options: client_metadata) } let(:provider) { OpenFeature::SDK::Provider::NoOpProvider.new } - let(:client_metadata) { OpenFeature::SDK::Metadata.new(name: name) } + let(:client_metadata) { OpenFeature::SDK::Metadata.new(name: name, domain: domain) } + let(:domain) { "testing" } let(:name) { "my-openfeature-client" } context "Requirement 1.2.1" do @@ -28,6 +29,7 @@ expect(client).to respond_to(:metadata) expect(client.metadata).to respond_to(:name) expect(client.metadata.name).to eq(name) + expect(client.metadata.domain).to eq(domain) end end