Skip to content

Commit

Permalink
Merge fbe224d into 061063f
Browse files Browse the repository at this point in the history
  • Loading branch information
gardleopard committed Oct 16, 2023
2 parents 061063f + fbe224d commit 2230b52
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Expand Up @@ -50,7 +50,7 @@ jobs:
- name: Install dependencies
run: bundle install
- name: Download test cases
run: git clone --depth 5 --branch v4.3.3 https://github.com/Unleash/client-specification.git client-specification
run: git clone --depth 5 --branch v4.5.1 https://github.com/Unleash/client-specification.git client-specification
- name: Run tests
run: bundle exec rake
env:
Expand Down
2 changes: 2 additions & 0 deletions .rubocop.yml
Expand Up @@ -13,6 +13,8 @@ Metrics/ClassLength:
Max: 135
CountAsOne:
- 'method_call'
Exclude:
- 'lib/unleash/feature_toggle.rb'
Layout/LineLength:
Max: 140
Metrics/MethodLength:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -528,7 +528,7 @@ You can also run `bin/console` for an interactive prompt that will allow you to
This SDK is also built against the Unleash Client Specification tests.
To run the Ruby SDK against this test suite, you'll need to have a copy on your machine, you can clone the repository directly using:

`git clone --depth 5 --branch v4.3.3 https://github.com/Unleash/client-specification.git client-specification`
`git clone --depth 5 --branch v4.5.1 https://github.com/Unleash/client-specification.git client-specification`

After doing this, `rake spec` will also run the client specification tests.

Expand Down
1 change: 0 additions & 1 deletion lib/unleash/client.rb
Expand Up @@ -41,7 +41,6 @@ def is_enabled?(feature, context = nil, default_value_param = false, &fallback_b
end

toggle_as_hash = Unleash&.toggles&.select{ |toggle| toggle['name'] == feature }&.first

if toggle_as_hash.nil?
Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
return default_value
Expand Down
2 changes: 1 addition & 1 deletion lib/unleash/configuration.rb
Expand Up @@ -53,7 +53,7 @@ def http_headers
{
'UNLEASH-INSTANCEID' => self.instance_id,
'UNLEASH-APPNAME' => self.app_name,
'Unleash-Client-Spec' => '4.2.2'
'Unleash-Client-Spec' => '4.5.1'
}.merge!(generate_custom_http_headers)
end

Expand Down
41 changes: 38 additions & 3 deletions lib/unleash/feature_toggle.rb
Expand Up @@ -7,7 +7,7 @@

module Unleash
class FeatureToggle
attr_accessor :name, :enabled, :strategies, :variant_definitions
attr_accessor :name, :enabled, :dependencies, :strategies, :variant_definitions

FeatureEvaluationResult = Struct.new(:enabled?, :strategy)

Expand All @@ -16,6 +16,7 @@ def initialize(params = {}, segment_map = {})

self.name = params.fetch('name', nil)
self.enabled = params.fetch('enabled', false)
self.dependencies = params.fetch('dependencies', [])

self.strategies = initialize_strategies(params, segment_map)
self.variant_definitions = initialize_variant_definitions(params)
Expand Down Expand Up @@ -75,9 +76,44 @@ def am_enabled?(context)
evaluate(context).enabled?
end

def parent_dependencies_satisfied?(context)
dependencies.empty? || dependencies.all?{ |parent| evaluate_parent(parent, context) }
end

def evaluate_parent(parent, context)
parent_toggle = get_parent(parent["feature"])

return false if parent_toggle.nil?

return false unless parent_toggle.dependencies.empty?

evaluation_result = parent_toggle.is_enabled?(context)
return !evaluation_result if parent["enabled"] == false

return false unless evaluation_result

unless parent["variants"].nil? || parent["variants"].empty?
return parent["variants"].include?(parent_toggle.get_variant(context).name)
end

evaluation_result
end

def get_parent(feature)
toggle_as_hash = Unleash&.toggles&.find{ |toggle| toggle['name'] == feature }
if toggle_as_hash.nil?
Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
return nil
end

Unleash::FeatureToggle.new(toggle_as_hash, Unleash&.segment_cache)
end

def evaluate(context)
evaluation_result =
if !self.enabled
if !parent_dependencies_satisfied?(context)
FeatureEvaluationResult.new(false, nil)
elsif !self.enabled
FeatureEvaluationResult.new(false, nil)
elsif self.strategies.empty?
FeatureEvaluationResult.new(true, nil)
Expand All @@ -88,7 +124,6 @@ def evaluate(context)

Unleash.logger.debug "Unleash::FeatureToggle (enabled:#{self.enabled}) " \
"and Strategies combined with constraints returned #{evaluation_result})"

evaluation_result
end

Expand Down
6 changes: 3 additions & 3 deletions spec/unleash/client_specification_spec.rb
Expand Up @@ -47,12 +47,12 @@
variant_tests = current_test_set.fetch('variantTests', [])
variant_tests.each do |test|
it "test that #{test['description']}" do
test_toggle = unleash_toggles.select{ |t| t.fetch('name', '') == test.fetch('toggleName') }.first
Unleash.toggles = unleash_toggles
Unleash.segment_cache = state_segments

toggle = Unleash::FeatureToggle.new(test_toggle, state_segments)
context = Unleash::Context.new(test['context'])

variant = toggle.get_variant(context, DEFAULT_VARIANT)
variant = unleash_client.get_variant(test.fetch('toggleName'), context)

expect(variant).to eq(Unleash::Variant.new(test['expectedResult']))
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unleash/configuration_spec.rb
Expand Up @@ -146,7 +146,7 @@
'X-API-KEY' => '123',
'UNLEASH-APPNAME' => 'test-app',
'UNLEASH-INSTANCEID' => config.instance_id,
'Unleash-Client-Spec' => '4.2.2'
'Unleash-Client-Spec' => '4.5.1'
}
)
expect(custom_headers_proc).to have_received(:call).exactly(1).times
Expand Down

0 comments on commit 2230b52

Please sign in to comment.