-
Notifications
You must be signed in to change notification settings - Fork 27
feat: Add get_feature_flags_result method to client
#89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+520
−18
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,56 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
|
|
||
| module PostHog | ||
| # Represents the result of a feature flag evaluation | ||
| # containing both the flag value and payload | ||
| class FeatureFlagResult | ||
| attr_reader :key, :variant, :payload | ||
|
|
||
| def initialize(key:, enabled:, variant: nil, payload: nil) | ||
| @key = key | ||
| @enabled = enabled | ||
| @variant = variant | ||
| @payload = payload | ||
| end | ||
|
|
||
| # Returns the effective value of the feature flag | ||
| # variant if present, otherwise enabled status | ||
| def value | ||
| @variant || @enabled | ||
| end | ||
|
|
||
| # Returns whether or not the feature flag evaluated as enabled | ||
| def enabled? | ||
| @enabled | ||
| end | ||
|
|
||
| # Factory method to create from flag value and payload | ||
| def self.from_value_and_payload(key, value, payload) | ||
| return nil if value.nil? | ||
|
|
||
| parsed_payload = parse_payload(payload) | ||
|
|
||
| if value.is_a?(String) | ||
| new(key: key, enabled: true, variant: value, payload: parsed_payload) | ||
| else | ||
| new(key: key, enabled: value, payload: parsed_payload) | ||
| end | ||
| end | ||
|
|
||
| def self.parse_payload(payload) | ||
| return nil if payload.nil? | ||
| return payload unless payload.is_a?(String) | ||
| return nil if payload.empty? | ||
|
|
||
| begin | ||
| JSON.parse(payload) | ||
| rescue JSON::ParserError | ||
| payload | ||
| end | ||
| end | ||
|
|
||
| private_class_method :parse_payload | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,159 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
| require 'posthog/feature_flag_result' | ||
|
|
||
| module PostHog | ||
| describe FeatureFlagResult do | ||
| describe '#initialize' do | ||
| it 'initializes with all attributes' do | ||
| result = FeatureFlagResult.new( | ||
| key: 'test-flag', | ||
| enabled: true, | ||
| variant: 'control', | ||
| payload: { 'foo' => 'bar' } | ||
| ) | ||
|
|
||
| expect(result.key).to eq('test-flag') | ||
| expect(result.enabled?).to be true | ||
| expect(result.variant).to eq('control') | ||
| expect(result.payload).to eq({ 'foo' => 'bar' }) | ||
| end | ||
|
|
||
| it 'initializes with minimal attributes' do | ||
| result = FeatureFlagResult.new(key: 'test-flag', enabled: false) | ||
|
|
||
| expect(result.key).to eq('test-flag') | ||
| expect(result.enabled?).to be false | ||
| expect(result.variant).to be_nil | ||
| expect(result.payload).to be_nil | ||
| end | ||
| end | ||
|
|
||
| describe '#value' do | ||
| it 'returns variant when present' do | ||
| result = FeatureFlagResult.new(key: 'test-flag', enabled: true, variant: 'control') | ||
|
|
||
| expect(result.value).to eq('control') | ||
| end | ||
|
|
||
| it 'returns enabled when variant is not present' do | ||
| result = FeatureFlagResult.new(key: 'test-flag', enabled: true) | ||
|
|
||
| expect(result.value).to be true | ||
| end | ||
|
|
||
| it 'returns false when flag is disabled and no variant' do | ||
| result = FeatureFlagResult.new(key: 'test-flag', enabled: false) | ||
|
|
||
| expect(result.value).to be false | ||
| end | ||
| end | ||
|
|
||
| describe '.from_value_and_payload' do | ||
| context 'with nil value' do | ||
| it 'returns nil' do | ||
| result = FeatureFlagResult.from_value_and_payload('test-flag', nil, nil) | ||
|
|
||
| expect(result).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context 'with boolean true value' do | ||
| it 'creates result with enabled true and no variant' do | ||
| result = FeatureFlagResult.from_value_and_payload('test-flag', true, nil) | ||
|
|
||
| expect(result.key).to eq('test-flag') | ||
| expect(result.enabled?).to be true | ||
| expect(result.variant).to be_nil | ||
| expect(result.payload).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context 'with boolean false value' do | ||
| it 'creates result with enabled false and no variant' do | ||
| result = FeatureFlagResult.from_value_and_payload('test-flag', false, nil) | ||
|
|
||
| expect(result.key).to eq('test-flag') | ||
| expect(result.enabled?).to be false | ||
| expect(result.variant).to be_nil | ||
| expect(result.payload).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context 'with string variant' do | ||
| it 'creates result with enabled true and variant set' do | ||
| result = FeatureFlagResult.from_value_and_payload('test-flag', 'control', nil) | ||
|
|
||
| expect(result.key).to eq('test-flag') | ||
| expect(result.enabled?).to be true | ||
| expect(result.variant).to eq('control') | ||
| expect(result.payload).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context 'with JSON string payload' do | ||
| it 'parses the JSON payload' do | ||
| result = FeatureFlagResult.from_value_and_payload('test-flag', true, '{"foo": "bar"}') | ||
|
|
||
| expect(result.payload).to eq({ 'foo' => 'bar' }) | ||
| end | ||
|
|
||
| it 'parses complex nested JSON payload' do | ||
| json_payload = '{"settings": {"theme": "dark", "notifications": true}, "features": ["a", "b"]}' | ||
| result = FeatureFlagResult.from_value_and_payload('test-flag', true, json_payload) | ||
|
|
||
| expect(result.payload).to eq({ | ||
| 'settings' => { 'theme' => 'dark', 'notifications' => true }, | ||
| 'features' => %w[a b] | ||
| }) | ||
| end | ||
| end | ||
|
|
||
| context 'with non-JSON string payload' do | ||
| it 'returns the string as-is' do | ||
| result = FeatureFlagResult.from_value_and_payload('test-flag', true, 'just a plain string') | ||
|
|
||
| expect(result.payload).to eq('just a plain string') | ||
| end | ||
| end | ||
|
|
||
| context 'with empty string payload' do | ||
| it 'returns nil' do | ||
| result = FeatureFlagResult.from_value_and_payload('test-flag', true, '') | ||
|
|
||
| expect(result.payload).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context 'with hash payload' do | ||
| it 'passes through hash payloads unchanged' do | ||
| hash_payload = { 'foo' => 'bar' } | ||
| result = FeatureFlagResult.from_value_and_payload('test-flag', true, hash_payload) | ||
|
|
||
| expect(result.payload).to eq({ 'foo' => 'bar' }) | ||
| end | ||
| end | ||
|
|
||
| context 'with nil payload' do | ||
| it 'sets payload to nil' do | ||
| result = FeatureFlagResult.from_value_and_payload('test-flag', true, nil) | ||
|
|
||
| expect(result.payload).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context 'with variant and payload' do | ||
| it 'creates result with variant and parsed payload' do | ||
| result = FeatureFlagResult.from_value_and_payload('test-flag', 'control', '{"discount": 10}') | ||
|
|
||
| expect(result.key).to eq('test-flag') | ||
| expect(result.enabled?).to be true | ||
| expect(result.variant).to eq('control') | ||
| expect(result.payload).to eq({ 'discount' => 10 }) | ||
| expect(result.value).to eq('control') | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.