posthog-rails 3.5.0 → 3.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,66 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Represents a feature flag returned by /flags v2
4
- module PostHog
5
- class FeatureFlag
6
- attr_reader :key, :enabled, :variant, :reason, :metadata
7
-
8
- def initialize(json)
9
- json.transform_keys!(&:to_s)
10
- @key = json['key']
11
- @enabled = json['enabled']
12
- @variant = json['variant']
13
- @reason = json['reason'] ? EvaluationReason.new(json['reason']) : nil
14
- @metadata = json['metadata'] ? FeatureFlagMetadata.new(json['metadata'].transform_keys(&:to_s)) : nil
15
- end
16
-
17
- # TODO: Rename to `value` in future version
18
- def get_value # rubocop:disable Naming/AccessorMethodName
19
- @variant || @enabled
20
- end
21
-
22
- def payload
23
- @metadata&.payload
24
- end
25
-
26
- def self.from_value_and_payload(key, value, payload)
27
- new({
28
- 'key' => key,
29
- 'enabled' => value.is_a?(String) || value,
30
- 'variant' => value.is_a?(String) ? value : nil,
31
- 'reason' => nil,
32
- 'metadata' => {
33
- 'id' => nil,
34
- 'version' => nil,
35
- 'payload' => payload,
36
- 'description' => nil
37
- }
38
- })
39
- end
40
- end
41
-
42
- # Represents the reason why a flag was enabled/disabled
43
- class EvaluationReason
44
- attr_reader :code, :description, :condition_index
45
-
46
- def initialize(json)
47
- json.transform_keys!(&:to_s)
48
- @code = json['code']
49
- @description = json['description']
50
- @condition_index = json['condition_index'].to_i if json['condition_index']
51
- end
52
- end
53
-
54
- # Represents metadata about a feature flag
55
- class FeatureFlagMetadata
56
- attr_reader :id, :version, :payload, :description
57
-
58
- def initialize(json)
59
- json.transform_keys!(&:to_s)
60
- @id = json['id']
61
- @version = json['version']
62
- @payload = json['payload']
63
- @description = json['description']
64
- end
65
- end
66
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module PostHog
4
- # Error type constants for the $feature_flag_error property.
5
- #
6
- # These values are sent in analytics events to track flag evaluation failures.
7
- # They should not be changed without considering impact on existing dashboards
8
- # and queries that filter on these values.
9
- #
10
- # Error values:
11
- # ERRORS_WHILE_COMPUTING: Server returned errorsWhileComputingFlags=true
12
- # FLAG_MISSING: Requested flag not in API response
13
- # QUOTA_LIMITED: Rate/quota limit exceeded
14
- # TIMEOUT: Request timed out
15
- # CONNECTION_ERROR: Network connectivity issue
16
- # UNKNOWN_ERROR: Unexpected exceptions
17
- #
18
- # For API errors with status codes, use the api_error() method which returns
19
- # a string like "api_error_500".
20
- class FeatureFlagError
21
- ERRORS_WHILE_COMPUTING = 'errors_while_computing_flags'
22
- FLAG_MISSING = 'flag_missing'
23
- QUOTA_LIMITED = 'quota_limited'
24
- TIMEOUT = 'timeout'
25
- CONNECTION_ERROR = 'connection_error'
26
- UNKNOWN_ERROR = 'unknown_error'
27
-
28
- # Generate API error string with status code.
29
- #
30
- # @param status [Integer, String] The HTTP status code
31
- # @return [String] Error string in format "api_error_STATUS"
32
- def self.api_error(status)
33
- "api_error_#{status.to_s.downcase}"
34
- end
35
- end
36
- end
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
-
5
- module PostHog
6
- # Represents the result of a feature flag evaluation
7
- # containing both the flag value and payload
8
- class FeatureFlagResult
9
- attr_reader :key, :variant, :payload
10
-
11
- def initialize(key:, enabled:, variant: nil, payload: nil)
12
- @key = key
13
- @enabled = enabled
14
- @variant = variant
15
- @payload = payload
16
- end
17
-
18
- # Returns the effective value of the feature flag
19
- # variant if present, otherwise enabled status
20
- def value
21
- @variant || @enabled
22
- end
23
-
24
- # Returns whether or not the feature flag evaluated as enabled
25
- def enabled?
26
- @enabled
27
- end
28
-
29
- # Factory method to create from flag value and payload
30
- def self.from_value_and_payload(key, value, payload)
31
- return nil if value.nil?
32
-
33
- parsed_payload = parse_payload(payload)
34
-
35
- if value.is_a?(String)
36
- new(key: key, enabled: true, variant: value, payload: parsed_payload)
37
- else
38
- new(key: key, enabled: value, payload: parsed_payload)
39
- end
40
- end
41
-
42
- def self.parse_payload(payload)
43
- return nil if payload.nil?
44
- return payload unless payload.is_a?(String)
45
- return nil if payload.empty?
46
-
47
- begin
48
- JSON.parse(payload)
49
- rescue JSON::ParserError
50
- payload
51
- end
52
- end
53
-
54
- private_class_method :parse_payload
55
- end
56
- end