Class: PostHog::FeatureFlagResult
- Inherits:
-
Object
- Object
- PostHog::FeatureFlagResult
- Defined in:
- lib/posthog/feature_flag_result.rb
Overview
Represents the result of a feature flag evaluation containing both the flag value and payload
Instance Attribute Summary collapse
-
#key ⇒ String, Symbol
readonly
The feature flag key.
-
#payload ⇒ Object?
readonly
The parsed feature flag payload.
-
#variant ⇒ String?
readonly
The variant key for multivariate flags.
Class Method Summary collapse
-
.from_value_and_payload(key, value, payload) ⇒ PostHog::FeatureFlagResult?
Factory method to create from flag value and payload.
-
.parse_payload(payload) ⇒ Object?
Deserialize a flag payload.
Instance Method Summary collapse
-
#enabled? ⇒ Boolean
Returns whether or not the feature flag evaluated as enabled.
-
#initialize(key:, enabled:, variant: nil, payload: nil) ⇒ FeatureFlagResult
constructor
A new instance of FeatureFlagResult.
-
#value ⇒ String, Boolean
Returns the effective value of the feature flag: variant if present, otherwise enabled status.
Constructor Details
#initialize(key:, enabled:, variant: nil, payload: nil) ⇒ FeatureFlagResult
Returns a new instance of FeatureFlagResult.
22 23 24 25 26 27 |
# File 'lib/posthog/feature_flag_result.rb', line 22 def initialize(key:, enabled:, variant: nil, payload: nil) @key = key @enabled = enabled @variant = variant @payload = payload end |
Instance Attribute Details
#key ⇒ String, Symbol (readonly)
Returns The feature flag key.
10 11 12 |
# File 'lib/posthog/feature_flag_result.rb', line 10 def key @key end |
#payload ⇒ Object? (readonly)
Returns The parsed feature flag payload.
16 17 18 |
# File 'lib/posthog/feature_flag_result.rb', line 16 def payload @payload end |
#variant ⇒ String? (readonly)
Returns The variant key for multivariate flags.
13 14 15 |
# File 'lib/posthog/feature_flag_result.rb', line 13 def variant @variant end |
Class Method Details
.from_value_and_payload(key, value, payload) ⇒ PostHog::FeatureFlagResult?
Factory method to create from flag value and payload.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/posthog/feature_flag_result.rb', line 50 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 |
.parse_payload(payload) ⇒ Object?
Deserialize a flag payload. Strings are JSON-parsed (with the raw string returned when the body is not valid JSON); already-deserialized values pass through. Public so PostHog::FeatureFlagEvaluations can normalize payloads the same way PostHog::FeatureFlagResult does.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/posthog/feature_flag_result.rb', line 69 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 |
Instance Method Details
#enabled? ⇒ Boolean
Returns whether or not the feature flag evaluated as enabled.
40 41 42 |
# File 'lib/posthog/feature_flag_result.rb', line 40 def enabled? @enabled end |
#value ⇒ String, Boolean
Returns the effective value of the feature flag: variant if present, otherwise enabled status.
33 34 35 |
# File 'lib/posthog/feature_flag_result.rb', line 33 def value @variant || @enabled end |