Class: FeatureFlag

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

Overview

Represents a feature flag returned by /flags v2

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



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

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

Instance Method Details

#get_valueObject

TODO: Rename to value in future version



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

def get_value # rubocop:disable Naming/AccessorMethodName
  @variant || @enabled
end

#payloadObject



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

def payload
  @metadata.payload if @metadata
end