Class: FeatureFlag

Inherits:
Object
  • Object
show all
Defined in:
lib/posthog/feature_flag.rb

Overview

Represents a feature flag returned by /decide v4

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ FeatureFlag

Returns a new instance of FeatureFlag.



5
6
7
8
9
10
11
12
# File 'lib/posthog/feature_flag.rb', line 5

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

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



3
4
5
# File 'lib/posthog/feature_flag.rb', line 3

def enabled
  @enabled
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/posthog/feature_flag.rb', line 3

def key
  @key
end

#metadataObject (readonly)

Returns the value of attribute metadata.



3
4
5
# File 'lib/posthog/feature_flag.rb', line 3

def 
  @metadata
end

#reasonObject (readonly)

Returns the value of attribute reason.



3
4
5
# File 'lib/posthog/feature_flag.rb', line 3

def reason
  @reason
end

#variantObject (readonly)

Returns the value of attribute variant.



3
4
5
# File 'lib/posthog/feature_flag.rb', line 3

def variant
  @variant
end

Class Method Details

.from_value_and_payload(key, value, payload) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/posthog/feature_flag.rb', line 22

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

Instance Method Details

#get_valueObject



14
15
16
# File 'lib/posthog/feature_flag.rb', line 14

def get_value
  @variant || @enabled
end

#payloadObject



18
19
20
# File 'lib/posthog/feature_flag.rb', line 18

def payload
  @metadata&.payload
end