Skip to content

Commit

Permalink
fix: Add domain to build_client (#109)
Browse files Browse the repository at this point in the history
Signed-off-by: William Kim <wkim@justworks.com>
  • Loading branch information
grepfruit19 authored Mar 29, 2024
1 parent e8fb8cc commit 56ccf17
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
10 changes: 6 additions & 4 deletions lib/open_feature/sdk/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions lib/open_feature/sdk/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@ module SDK
#
# * <tt>version</tt> - Allows you to specify version of the Metadata structure
#
# * <tt>domain</tt> - 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)
Expand Down
47 changes: 46 additions & 1 deletion spec/open_feature/sdk/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 3 additions & 1 deletion spec/open_feature/sdk/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 56ccf17

Please sign in to comment.