Class: PostHog::FeatureFlagResult

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, enabled:, variant: nil, payload: nil) ⇒ FeatureFlagResult

Returns a new instance of FeatureFlagResult.



11
12
13
14
15
16
# File 'lib/posthog/feature_flag_result.rb', line 11

def initialize(key:, enabled:, variant: nil, payload: nil)
  @key = key
  @enabled = enabled
  @variant = variant
  @payload = payload
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/posthog/feature_flag_result.rb', line 9

def key
  @key
end

#payloadObject (readonly)

Returns the value of attribute payload.



9
10
11
# File 'lib/posthog/feature_flag_result.rb', line 9

def payload
  @payload
end

#variantObject (readonly)

Returns the value of attribute variant.



9
10
11
# File 'lib/posthog/feature_flag_result.rb', line 9

def variant
  @variant
end

Class Method Details

.from_value_and_payload(key, value, payload) ⇒ Object

Factory method to create from flag value and payload



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/posthog/feature_flag_result.rb', line 30

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.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/posthog/feature_flag_result.rb', line 46

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

Returns:

  • (Boolean)


25
26
27
# File 'lib/posthog/feature_flag_result.rb', line 25

def enabled?
  @enabled
end

#valueObject

Returns the effective value of the feature flag variant if present, otherwise enabled status



20
21
22
# File 'lib/posthog/feature_flag_result.rb', line 20

def value
  @variant || @enabled
end