Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 53 additions & 51 deletions lib/posthog/feature_flag.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,64 @@
# Represents a feature flag returned by /flags v2
class FeatureFlag
attr_reader :key, :enabled, :variant, :reason, :metadata

def initialize(json)
json.transform_keys!(&:to_s)
@key = json['key']
@enabled = json['enabled']
@variant = json['variant']
@reason = json['reason'] ? EvaluationReason.new(json['reason']) : nil
@metadata = json['metadata'] ? FeatureFlagMetadata.new(json['metadata'].transform_keys(&:to_s)) : nil
end
class PostHog
# Represents a feature flag returned by /flags v2
class FeatureFlag
attr_reader :key, :enabled, :variant, :reason, :metadata

# TODO: Rename to `value` in future version
def get_value # rubocop:disable Naming/AccessorMethodName
@variant || @enabled
end
def initialize(json)
json.transform_keys!(&:to_s)
@key = json['key']
@enabled = json['enabled']
@variant = json['variant']
@reason = json['reason'] ? PostHog::EvaluationReason.new(json['reason']) : nil
@metadata = json['metadata'] ? PostHog::FeatureFlagMetadata.new(json['metadata'].transform_keys(&:to_s)) : nil
end

def payload
@metadata.payload if @metadata
end
# TODO: Rename to `value` in future version
def get_value # rubocop:disable Naming/AccessorMethodName
@variant || @enabled
end

def self.from_value_and_payload(key, value, payload)
new({
'key' => key,
'enabled' => value.is_a?(String) || value,
'variant' => value.is_a?(String) ? value : nil,
'reason' => nil,
'metadata' => {
'id' => nil,
'version' => nil,
'payload' => payload,
'description' => nil
}
})
def payload
@metadata.payload if @metadata
end

def self.from_value_and_payload(key, value, payload)
new({
'key' => key,
'enabled' => value.is_a?(String) || value,
'variant' => value.is_a?(String) ? value : nil,
'reason' => nil,
'metadata' => {
'id' => nil,
'version' => nil,
'payload' => payload,
'description' => nil
}
})
end
end
end

# Represents the reason why a flag was enabled/disabled
class EvaluationReason
attr_reader :code, :description, :condition_index
# Represents the reason why a flag was enabled/disabled
class EvaluationReason
attr_reader :code, :description, :condition_index

def initialize(json)
json.transform_keys!(&:to_s)
@code = json['code']
@description = json['description']
@condition_index = json['condition_index'].to_i if json['condition_index']
def initialize(json)
json.transform_keys!(&:to_s)
@code = json['code']
@description = json['description']
@condition_index = json['condition_index'].to_i if json['condition_index']
end
end
end

# Represents metadata about a feature flag
class FeatureFlagMetadata
attr_reader :id, :version, :payload, :description
# Represents metadata about a feature flag
class FeatureFlagMetadata
attr_reader :id, :version, :payload, :description

def initialize(json)
json.transform_keys!(&:to_s)
@id = json['id']
@version = json['version']
@payload = json['payload']
@description = json['description']
def initialize(json)
json.transform_keys!(&:to_s)
@id = json['id']
@version = json['version']
@payload = json['payload']
@description = json['description']
end
end
end
4 changes: 2 additions & 2 deletions lib/posthog/feature_flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def get_flags(distinct_id, groups = {}, person_properties = {}, group_properties
if flags_response[:flags]
# v4 format
flags_hash = flags_response[:flags].transform_values do |flag|
FeatureFlag.new(flag)
PostHog::FeatureFlag.new(flag)
end
flags_response[:flags] = flags_hash
flags_response[:featureFlags] = flags_hash.transform_values(&:get_value).transform_keys(&:to_sym)
Expand All @@ -126,7 +126,7 @@ def get_flags(distinct_id, groups = {}, person_properties = {}, group_properties
flags_response[:featureFlags] = flags_response[:featureFlags] || {}
flags_response[:featureFlagPayloads] = flags_response[:featureFlagPayloads] || {}
flags_response[:flags] = flags_response[:featureFlags].map do |key, value|
[key, FeatureFlag.from_value_and_payload(key, value, flags_response[:featureFlagPayloads][key])]
[key, PostHog::FeatureFlag.from_value_and_payload(key, value, flags_response[:featureFlagPayloads][key])]
end.to_h
end
flags_response
Expand Down
34 changes: 17 additions & 17 deletions spec/posthog/flags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class PostHog

enabled_flag = result[:flags][:'enabled-flag']
expect(enabled_flag).to have_attributes(
class: FeatureFlag,
class: PostHog::FeatureFlag,
key: :'enabled-flag',
enabled: true,
variant: nil,
Expand All @@ -108,7 +108,7 @@ class PostHog

multi_variate_flag = result[:flags][:'multi-variate-flag']
expect(multi_variate_flag).to have_attributes(
class: FeatureFlag,
class: PostHog::FeatureFlag,
key: :'multi-variate-flag',
enabled: true,
variant: 'hello',
Expand All @@ -120,7 +120,7 @@ class PostHog

disabled_flag = result[:flags][:'disabled-flag']
expect(disabled_flag).to have_attributes(
class: FeatureFlag,
class: PostHog::FeatureFlag,
key: :'disabled-flag',
enabled: false,
variant: nil,
Expand Down Expand Up @@ -155,12 +155,12 @@ class PostHog
enabled_flag = result[:flags][:'enabled-flag']

expect(enabled_flag).to have_attributes(
class: FeatureFlag,
class: PostHog::FeatureFlag,
key: 'enabled-flag',
enabled: true,
variant: nil,
reason: have_attributes(
class: EvaluationReason,
class: PostHog::EvaluationReason,
code: 'condition_match',
description: 'Matched conditions set 3',
condition_index: 2
Expand All @@ -175,12 +175,12 @@ class PostHog

multi_variate_flag = result[:flags][:'multi-variate-flag']
expect(multi_variate_flag).to have_attributes(
class: FeatureFlag,
class: PostHog::FeatureFlag,
key: 'multi-variate-flag',
enabled: true,
variant: 'hello',
reason: have_attributes(
class: EvaluationReason,
class: PostHog::EvaluationReason,
code: 'condition_match',
description: 'Matched conditions set 2',
condition_index: 1
Expand All @@ -194,12 +194,12 @@ class PostHog

disabled_flag = result[:flags][:'disabled-flag']
expect(disabled_flag).to have_attributes(
class: FeatureFlag,
class: PostHog::FeatureFlag,
key: 'disabled-flag',
enabled: false,
variant: nil,
reason: have_attributes(
class: EvaluationReason,
class: PostHog::EvaluationReason,
code: 'no_condition_match',
description: 'No matching condition set',
condition_index: nil
Expand Down Expand Up @@ -267,31 +267,31 @@ class PostHog
end
end

describe FeatureFlag do
describe PostHog::FeatureFlag do
let(:decide_v4_response) do
JSON.parse(File.read(File.join(__dir__, 'fixtures', 'test-decide-v4.json')), symbolize_names: true)
end

it 'transforms v4 response flags into hash of FeatureFlag instances' do
json = decide_v4_response[:flags][:'enabled-flag']

result = FeatureFlag.new(json)
result = PostHog::FeatureFlag.new(json)

expect(result).to have_attributes(
class: FeatureFlag,
class: PostHog::FeatureFlag,
key: 'enabled-flag',
enabled: true,
variant: nil,
reason: have_attributes(
class: EvaluationReason,
class: PostHog::EvaluationReason,
code: 'condition_match',
description: 'Matched conditions set 3'
)
)
end

it 'transforms a hash into a FeatureFlag instance' do
result = FeatureFlag.new({
result = PostHog::FeatureFlag.new({
'key' => 'enabled-flag',
'enabled' => true,
'variant' => nil,
Expand All @@ -309,18 +309,18 @@ class PostHog
})

expect(result).to have_attributes(
class: FeatureFlag,
class: PostHog::FeatureFlag,
key: 'enabled-flag',
enabled: true,
variant: nil,
reason: have_attributes(
class: EvaluationReason,
class: PostHog::EvaluationReason,
code: 'condition_match',
description: 'Matched conditions set 3',
condition_index: 2
),
metadata: have_attributes(
class: FeatureFlagMetadata,
class: PostHog::FeatureFlagMetadata,
id: 1,
version: 23,
payload: '{"foo": 1}',
Expand Down