flagsmith 4.3.0 → 5.0.1
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.
- checksums.yaml +4 -4
- data/.release-please-manifest.json +1 -0
- data/CHANGELOG.md +258 -0
- data/CODEOWNERS +1 -0
- data/Gemfile.lock +17 -10
- data/README.md +0 -2
- data/lib/flagsmith/engine/core.rb +107 -48
- data/lib/flagsmith/engine/environments/models.rb +6 -3
- data/lib/flagsmith/engine/evaluation/core.rb +142 -0
- data/lib/flagsmith/engine/features/constants.rb +14 -0
- data/lib/flagsmith/engine/features/models.rb +1 -1
- data/lib/flagsmith/engine/mappers/environment.rb +59 -0
- data/lib/flagsmith/engine/mappers/identity.rb +97 -0
- data/lib/flagsmith/engine/mappers/segments.rb +65 -0
- data/lib/flagsmith/engine/mappers.rb +66 -0
- data/lib/flagsmith/engine/segments/evaluator.rb +193 -18
- data/lib/flagsmith/engine/segments/models.rb +50 -16
- data/lib/flagsmith/sdk/api_client.rb +1 -0
- data/lib/flagsmith/sdk/models/flags.rb +24 -10
- data/lib/flagsmith/sdk/utils.rb +29 -0
- data/lib/flagsmith/version.rb +1 -1
- data/lib/flagsmith.rb +30 -19
- data/release-please-config.json +62 -0
- data/renovate.json +28 -0
- metadata +32 -7
- data/flagsmith.gemspec +0 -39
|
@@ -1,22 +1,65 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'jsonpath'
|
|
3
5
|
require_relative 'constants'
|
|
6
|
+
require_relative 'models'
|
|
4
7
|
require_relative '../utils/hash_func'
|
|
5
8
|
|
|
6
9
|
module Flagsmith
|
|
7
10
|
module Engine
|
|
8
11
|
module Segments
|
|
9
12
|
# Evaluator methods
|
|
10
|
-
module Evaluator
|
|
13
|
+
module Evaluator # rubocop:disable Metrics/ModuleLength
|
|
11
14
|
include Flagsmith::Engine::Segments::Constants
|
|
12
15
|
include Flagsmith::Engine::Utils::HashFunc
|
|
13
16
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def get_enriched_context(context)
|
|
20
|
+
identity_context = context[:identity]
|
|
21
|
+
return context unless identity_context
|
|
22
|
+
return context if identity_context[:key]
|
|
23
|
+
|
|
24
|
+
enriched_context = context.dup
|
|
25
|
+
enriched_context[:identity] = identity_context.merge(
|
|
26
|
+
key: "#{context[:environment][:key]}_#{identity_context[:identifier]}"
|
|
27
|
+
)
|
|
28
|
+
enriched_context
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Context-based segment evaluation
|
|
32
|
+
# Returns all segments that the identity belongs to based on segment rules evaluation
|
|
33
|
+
#
|
|
34
|
+
# @param context [Hash] Evaluation context containing identity and segment definitions
|
|
35
|
+
# @return [Array<Hash>] Array of segments that the identity matches
|
|
36
|
+
def get_segments_from_context(context)
|
|
37
|
+
return [] unless context[:identity] && context[:segments]
|
|
38
|
+
|
|
39
|
+
context[:segments].values.select do |segment|
|
|
40
|
+
next false if segment[:rules].nil? || segment[:rules].empty?
|
|
41
|
+
|
|
42
|
+
matches = segment[:rules].all? { |rule| traits_match_segment_rule_from_context(rule, segment[:key], context) }
|
|
43
|
+
matches
|
|
17
44
|
end
|
|
18
45
|
end
|
|
19
46
|
|
|
47
|
+
def traits_match_segment_condition(identity_traits, condition, segment_id, identity_id)
|
|
48
|
+
if condition.operator == PERCENTAGE_SPLIT
|
|
49
|
+
return hashed_percentage_for_object_ids([segment_id,
|
|
50
|
+
identity_id]) <= condition.value.to_f
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
trait = identity_traits.find { |t| t.key.to_s == condition.property }
|
|
54
|
+
|
|
55
|
+
return handle_trait_existence_conditions(trait, condition.operator) if [IS_SET,
|
|
56
|
+
IS_NOT_SET].include?(condition.operator)
|
|
57
|
+
|
|
58
|
+
return condition.match_trait_value?(trait.trait_value) if trait
|
|
59
|
+
|
|
60
|
+
false
|
|
61
|
+
end
|
|
62
|
+
|
|
20
63
|
# Evaluates whether a given identity is in the provided segment.
|
|
21
64
|
#
|
|
22
65
|
# :param identity: identity model object to evaluate
|
|
@@ -50,32 +93,164 @@ module Flagsmith
|
|
|
50
93
|
end
|
|
51
94
|
|
|
52
95
|
matches_conditions &&
|
|
53
|
-
rule.rules.
|
|
96
|
+
rule.rules.send(rule.matching_function) { |r| traits_match_segment_rule(identity_traits, r, segment_id, identity_id) }
|
|
54
97
|
end
|
|
55
98
|
# rubocop:enable Metrics/MethodLength
|
|
56
99
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
100
|
+
# Context-based helper functions
|
|
101
|
+
|
|
102
|
+
# Evaluates whether a segment rule matches using context
|
|
103
|
+
#
|
|
104
|
+
# @param rule [Hash] The rule to evaluate
|
|
105
|
+
# @param segment_key [String] The segment key (used for percentage split)
|
|
106
|
+
# @param context [Hash] The evaluation context
|
|
107
|
+
# @return [Boolean] True if the rule matches
|
|
108
|
+
def traits_match_segment_rule_from_context(rule, segment_key, context)
|
|
109
|
+
matches_conditions = evaluate_conditions_from_context(rule, segment_key, context)
|
|
110
|
+
matches_sub_rules = evaluate_sub_rules_from_context(rule, segment_key, context)
|
|
111
|
+
|
|
112
|
+
matches_conditions && matches_sub_rules
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Evaluates rule conditions based on rule type (ALL/ANY/NONE)
|
|
116
|
+
#
|
|
117
|
+
# @param rule [Hash] The rule containing conditions and type
|
|
118
|
+
# @param segment_key [String] The segment key
|
|
119
|
+
# @param context [Hash] The evaluation context
|
|
120
|
+
# @return [Boolean] True if conditions match according to rule type
|
|
121
|
+
def evaluate_conditions_from_context(rule, segment_key, context)
|
|
122
|
+
return true unless rule[:conditions]&.any?
|
|
123
|
+
|
|
124
|
+
condition_results = rule[:conditions].map do |condition|
|
|
125
|
+
traits_match_segment_condition_from_context(condition, segment_key, context)
|
|
61
126
|
end
|
|
62
127
|
|
|
63
|
-
|
|
128
|
+
evaluate_rule_conditions(rule[:type], condition_results)
|
|
129
|
+
end
|
|
64
130
|
|
|
65
|
-
|
|
66
|
-
|
|
131
|
+
# Evaluates nested sub-rules
|
|
132
|
+
#
|
|
133
|
+
# @param rule [Hash] The rule containing nested rules
|
|
134
|
+
# @param segment_key [String] The segment key
|
|
135
|
+
# @param context [Hash] The evaluation context
|
|
136
|
+
# @return [Boolean] True if all sub-rules match
|
|
137
|
+
def evaluate_sub_rules_from_context(rule, segment_key, context)
|
|
138
|
+
return true if rule[:rules].nil? || rule[:rules].empty?
|
|
67
139
|
|
|
68
|
-
|
|
140
|
+
sub_rule_results = rule[:rules].map do |sub_rule|
|
|
141
|
+
traits_match_segment_rule_from_context(sub_rule, segment_key, context)
|
|
142
|
+
end
|
|
143
|
+
evaluate_rule_conditions(rule[:type], sub_rule_results)
|
|
144
|
+
end
|
|
69
145
|
|
|
70
|
-
|
|
146
|
+
# Evaluates a single segment condition using context
|
|
147
|
+
#
|
|
148
|
+
# @param condition [Hash] The condition to evaluate
|
|
149
|
+
# @param segment_key [String] The segment key (used for percentage split hashing)
|
|
150
|
+
# @param context [Hash] The evaluation context
|
|
151
|
+
# @return [Boolean] True if the condition matches
|
|
152
|
+
def traits_match_segment_condition_from_context(condition, segment_key, context)
|
|
153
|
+
return handle_percentage_split(condition, segment_key, context) if condition[:operator] == PERCENTAGE_SPLIT
|
|
154
|
+
return false if condition[:property].nil?
|
|
155
|
+
|
|
156
|
+
trait_value = get_trait_value(condition[:property], context)
|
|
157
|
+
evaluate_trait_condition(condition, trait_value)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def handle_percentage_split(condition, segment_key, context)
|
|
161
|
+
split_key = if condition[:property].nil? || condition[:property].empty?
|
|
162
|
+
# No property specified - use identity key
|
|
163
|
+
get_identity_key_from_context(context)
|
|
164
|
+
else
|
|
165
|
+
# Property specified - must exist in context
|
|
166
|
+
get_context_value(condition[:property], context)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# If split_key is nil, condition doesn't match
|
|
170
|
+
return false if split_key.nil?
|
|
171
|
+
|
|
172
|
+
hashed_percentage = hashed_percentage_for_object_ids([segment_key, split_key])
|
|
173
|
+
hashed_percentage <= condition[:value].to_f
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def evaluate_trait_condition(condition, trait_value)
|
|
177
|
+
return !trait_value.nil? if condition[:operator] == IS_SET
|
|
178
|
+
return trait_value.nil? if condition[:operator] == IS_NOT_SET
|
|
179
|
+
return false if trait_value.nil?
|
|
180
|
+
|
|
181
|
+
condition_obj = Flagsmith::Engine::Segments::Condition.new(
|
|
182
|
+
operator: condition[:operator],
|
|
183
|
+
value: condition[:value],
|
|
184
|
+
property: condition[:property]
|
|
185
|
+
)
|
|
186
|
+
condition_obj.match_trait_value?(trait_value)
|
|
71
187
|
end
|
|
72
188
|
|
|
73
|
-
|
|
189
|
+
# Evaluate rule conditions based on type (ALL/ANY/NONE)
|
|
190
|
+
#
|
|
191
|
+
# @param rule_type [String] The rule type
|
|
192
|
+
# @param condition_results [Array<Boolean>] Array of condition evaluation results
|
|
193
|
+
# @return [Boolean] True if conditions match according to rule type
|
|
194
|
+
def evaluate_rule_conditions(rule_type, condition_results)
|
|
195
|
+
case rule_type
|
|
196
|
+
when 'ALL'
|
|
197
|
+
condition_results.empty? || condition_results.all?
|
|
198
|
+
when 'ANY'
|
|
199
|
+
!condition_results.empty? && condition_results.any?
|
|
200
|
+
when 'NONE'
|
|
201
|
+
condition_results.empty? || condition_results.none?
|
|
202
|
+
else
|
|
203
|
+
false
|
|
204
|
+
end
|
|
205
|
+
end
|
|
74
206
|
|
|
75
|
-
|
|
76
|
-
|
|
207
|
+
# Get trait value from context, supporting JSONPath expressions
|
|
208
|
+
#
|
|
209
|
+
# @param property [String] The property name or JSONPath
|
|
210
|
+
# @param context [Hash] The evaluation context
|
|
211
|
+
# @return [Object, nil] The trait value or nil
|
|
212
|
+
def get_trait_value(property, context)
|
|
213
|
+
if property.start_with?('$.')
|
|
214
|
+
context_value = get_context_value(property, context)
|
|
215
|
+
return context_value if !context_value.nil? && primitive?(context_value)
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
traits = context.dig(:identity, :traits) || {}
|
|
219
|
+
traits[property] || traits[property.to_sym]
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# Get value from context using JSONPath syntax
|
|
223
|
+
#
|
|
224
|
+
# @param json_path [String] JSONPath expression (e.g., '$.identity.identifier')
|
|
225
|
+
# @param context [Hash] The evaluation context
|
|
226
|
+
# @return [Object, nil] The value at the path or nil
|
|
227
|
+
def get_context_value(json_path, context)
|
|
228
|
+
return nil unless context && json_path&.start_with?('$.')
|
|
229
|
+
|
|
230
|
+
results = JsonPath.new(json_path, use_symbols: true).on(context)
|
|
231
|
+
results.first
|
|
232
|
+
rescue StandardError
|
|
233
|
+
nil
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# Get identity key from context
|
|
237
|
+
#
|
|
238
|
+
# @param context [Hash] The evaluation context
|
|
239
|
+
# @return [String, nil] The identity key or generated composite key
|
|
240
|
+
def get_identity_key_from_context(context)
|
|
241
|
+
return nil unless context[:identity]
|
|
242
|
+
|
|
243
|
+
context[:identity][:key]
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# Check if value is primitive (not an object or array)
|
|
247
|
+
#
|
|
248
|
+
# @param value [Object] The value to check
|
|
249
|
+
# @return [Boolean] True if value is primitive (not an object or array)
|
|
250
|
+
def primitive?(value)
|
|
251
|
+
return true if value.nil?
|
|
77
252
|
|
|
78
|
-
|
|
253
|
+
!(value.is_a?(Hash) || value.is_a?(Array))
|
|
79
254
|
end
|
|
80
255
|
end
|
|
81
256
|
end
|
|
@@ -45,7 +45,7 @@ module Flagsmith
|
|
|
45
45
|
CONTAINS => ->(other_value, self_value) { (other_value || false) && other_value.include?(self_value) },
|
|
46
46
|
|
|
47
47
|
NOT_CONTAINS => ->(other_value, self_value) { (other_value || false) && !other_value.include?(self_value) },
|
|
48
|
-
REGEX => ->(other_value, self_value) { (other_value || false) && other_value.match?(self_value) }
|
|
48
|
+
REGEX => ->(other_value, self_value) { (other_value || false) && other_value.to_s.match?(self_value) }
|
|
49
49
|
}.freeze
|
|
50
50
|
|
|
51
51
|
def initialize(operator:, value:, property: nil)
|
|
@@ -55,30 +55,48 @@ module Flagsmith
|
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
def match_trait_value?(trait_value)
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
trait_value = parse_semver_trait_value(trait_value)
|
|
59
|
+
return false if trait_value.nil?
|
|
60
60
|
|
|
61
61
|
return match_in_value(trait_value) if @operator == IN
|
|
62
62
|
return match_modulo_value(trait_value) if @operator == MODULO
|
|
63
|
+
return MATCHING_FUNCTIONS[REGEX]&.call(trait_value, @value) if @operator == REGEX
|
|
63
64
|
|
|
65
|
+
match_with_type_conversion(trait_value)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def parse_semver_trait_value(trait_value)
|
|
69
|
+
return trait_value unless @value.is_a?(String) && @value.match?(/:semver$/)
|
|
70
|
+
|
|
71
|
+
Semantic::Version.new(trait_value.to_s.gsub(/:semver$/, ''))
|
|
72
|
+
rescue ArgumentError, Semantic::Version::ValidationFailed
|
|
73
|
+
nil
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def match_with_type_conversion(trait_value)
|
|
64
77
|
type_as_trait_value = format_to_type_of(trait_value)
|
|
65
78
|
formatted_value = type_as_trait_value ? type_as_trait_value.call(@value) : @value
|
|
66
|
-
|
|
67
79
|
MATCHING_FUNCTIONS[operator]&.call(trait_value, formatted_value)
|
|
68
80
|
end
|
|
69
81
|
|
|
70
|
-
|
|
82
|
+
TYPE_CONVERTERS = {
|
|
83
|
+
'String' => ->(v) { v.to_s },
|
|
84
|
+
'Semantic::Version' => ->(v) { Semantic::Version.new(v.to_s.gsub(/:semver$/, '')) },
|
|
85
|
+
'TrueClass' => ->(v) { ['True', 'true', 'TRUE', true, 1, '1'].include?(v) },
|
|
86
|
+
'FalseClass' => ->(v) { !['False', 'false', 'FALSE', false].include?(v) },
|
|
87
|
+
'Integer' => lambda { |v|
|
|
88
|
+
i = v.to_i
|
|
89
|
+
i.to_s == v.to_s ? i : v
|
|
90
|
+
},
|
|
91
|
+
'Float' => lambda { |v|
|
|
92
|
+
f = v.to_f
|
|
93
|
+
f.to_s == v.to_s ? f : v
|
|
94
|
+
}
|
|
95
|
+
}.freeze
|
|
96
|
+
|
|
71
97
|
def format_to_type_of(input)
|
|
72
|
-
|
|
73
|
-
'String' => ->(v) { v.to_s },
|
|
74
|
-
'Semantic::Version' => ->(v) { Semantic::Version.new(v.to_s.gsub(/:semver$/, '')) },
|
|
75
|
-
'TrueClass' => ->(v) { ['True', 'true', 'TRUE', true, 1, '1'].include?(v) },
|
|
76
|
-
'FalseClass' => ->(v) { !['False', 'false', 'FALSE', false, 0, '0'].include?(v) },
|
|
77
|
-
'Integer' => ->(v) { v.to_i },
|
|
78
|
-
'Float' => ->(v) { v.to_f }
|
|
79
|
-
}[input.class.to_s]
|
|
98
|
+
TYPE_CONVERTERS[input.class.to_s]
|
|
80
99
|
end
|
|
81
|
-
# rubocop:enable Metrics/AbcSize
|
|
82
100
|
|
|
83
101
|
def match_modulo_value(trait_value)
|
|
84
102
|
divisor, remainder = @value.split('|')
|
|
@@ -88,9 +106,25 @@ module Flagsmith
|
|
|
88
106
|
end
|
|
89
107
|
|
|
90
108
|
def match_in_value(trait_value)
|
|
91
|
-
return
|
|
109
|
+
return false if invalid_in_value?(trait_value)
|
|
110
|
+
return @value.include?(trait_value.to_s) if @value.is_a?(Array)
|
|
92
111
|
|
|
93
|
-
|
|
112
|
+
parse_and_match_string_value(trait_value)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def invalid_in_value?(trait_value)
|
|
116
|
+
trait_value.nil? || [TrueClass, FalseClass].any? { |klass| trait_value.is_a?(klass) }
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def parse_and_match_string_value(trait_value) # rubocop:disable Metrics/AbcSize
|
|
120
|
+
return @value.to_s.split(',').include?(trait_value.to_s) unless @value.is_a?(String)
|
|
121
|
+
|
|
122
|
+
parsed = JSON.parse(@value)
|
|
123
|
+
return parsed.include?(trait_value.to_s) if parsed.is_a?(Array)
|
|
124
|
+
|
|
125
|
+
@value.to_s.split(',').include?(trait_value.to_s)
|
|
126
|
+
rescue JSON::ParserError
|
|
127
|
+
@value.to_s.split(',').include?(trait_value.to_s)
|
|
94
128
|
end
|
|
95
129
|
|
|
96
130
|
class << self
|
|
@@ -31,6 +31,7 @@ module Flagsmith
|
|
|
31
31
|
faraday.headers['Accept'] = 'application/json'
|
|
32
32
|
faraday.headers['Content-Type'] = 'application/json'
|
|
33
33
|
faraday.headers['X-Environment-Key'] = config.environment_key
|
|
34
|
+
faraday.headers['User-Agent'] = Flagsmith::SDK::Utils.user_agent
|
|
34
35
|
faraday.headers.merge(config.custom_headers)
|
|
35
36
|
end
|
|
36
37
|
|
|
@@ -32,12 +32,13 @@ module Flagsmith
|
|
|
32
32
|
|
|
33
33
|
# 'live' Flag class as returned by API or local evaluation
|
|
34
34
|
class Flag < BaseFlag
|
|
35
|
-
attr_reader :feature_name, :feature_id
|
|
35
|
+
attr_reader :feature_name, :feature_id, :reason
|
|
36
36
|
|
|
37
|
-
def initialize(feature_name:, enabled:, value:, feature_id:)
|
|
37
|
+
def initialize(feature_name:, enabled:, value:, feature_id:, reason: nil)
|
|
38
38
|
super(enabled: enabled, value: value, default: false)
|
|
39
39
|
@feature_name = feature_name
|
|
40
40
|
@feature_id = feature_id
|
|
41
|
+
@reason = reason
|
|
41
42
|
end
|
|
42
43
|
|
|
43
44
|
def <=>(other)
|
|
@@ -54,7 +55,8 @@ module Flagsmith
|
|
|
54
55
|
feature_name: feature_name,
|
|
55
56
|
value: value,
|
|
56
57
|
enabled: enabled,
|
|
57
|
-
default: default
|
|
58
|
+
default: default,
|
|
59
|
+
reason: reason
|
|
58
60
|
}
|
|
59
61
|
end
|
|
60
62
|
|
|
@@ -172,15 +174,27 @@ module Flagsmith
|
|
|
172
174
|
)
|
|
173
175
|
end
|
|
174
176
|
|
|
175
|
-
def
|
|
176
|
-
to_flag_object = lambda { |
|
|
177
|
-
|
|
178
|
-
|
|
177
|
+
def from_evaluation_result(evaluation_result, **args)
|
|
178
|
+
to_flag_object = lambda { |flag_result, acc|
|
|
179
|
+
id = flag_result.dig(:metadata, :id)
|
|
180
|
+
if id.nil?
|
|
181
|
+
raise Flagsmith::ClientError,
|
|
182
|
+
"FlagResult metadata.id is missing for feature \"#{flag_result[:name]}\". This indicates a bug in the SDK, please report it."
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
acc[flag_result[:name]] = Collection.map_evaluated_flag_to_flag_result(flag_result, id)
|
|
179
186
|
}
|
|
180
187
|
|
|
181
|
-
new(
|
|
182
|
-
|
|
183
|
-
|
|
188
|
+
new(evaluation_result[:flags].each_value.each_with_object({}, &to_flag_object), **args)
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def map_evaluated_flag_to_flag_result(flag_result, id)
|
|
192
|
+
Flagsmith::Flags::Flag.new(
|
|
193
|
+
feature_name: flag_result[:name],
|
|
194
|
+
enabled: flag_result[:enabled],
|
|
195
|
+
value: flag_result[:value],
|
|
196
|
+
feature_id: id,
|
|
197
|
+
reason: flag_result[:reason]
|
|
184
198
|
)
|
|
185
199
|
end
|
|
186
200
|
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Flagsmith
|
|
4
|
+
module SDK
|
|
5
|
+
# Utility functions
|
|
6
|
+
module Utils
|
|
7
|
+
FLAGSMITH_USER_AGENT = 'flagsmith-ruby-sdk'
|
|
8
|
+
FLAGSMITH_UNKNOWN_VERSION = 'unknown'
|
|
9
|
+
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
# Returns the user agent string for HTTP requests
|
|
13
|
+
# @return [String] user agent in format "flagsmith-ruby-sdk/version"
|
|
14
|
+
def user_agent
|
|
15
|
+
"#{FLAGSMITH_USER_AGENT}/#{version}"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Returns the SDK version
|
|
19
|
+
# @return [String] version string or 'unknown' if not available
|
|
20
|
+
def version
|
|
21
|
+
return Flagsmith::VERSION if defined?(Flagsmith::VERSION)
|
|
22
|
+
|
|
23
|
+
FLAGSMITH_UNKNOWN_VERSION
|
|
24
|
+
rescue StandardError
|
|
25
|
+
FLAGSMITH_UNKNOWN_VERSION
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/flagsmith/version.rb
CHANGED
data/lib/flagsmith.rb
CHANGED
|
@@ -7,11 +7,14 @@ require 'faraday/retry'
|
|
|
7
7
|
# This is the patch to use slice in earler versions
|
|
8
8
|
require 'flagsmith/hash_slice'
|
|
9
9
|
|
|
10
|
+
require 'flagsmith/version'
|
|
11
|
+
|
|
10
12
|
require 'flagsmith/sdk/analytics_processor'
|
|
11
13
|
require 'flagsmith/sdk/api_client'
|
|
12
14
|
require 'flagsmith/sdk/config'
|
|
13
15
|
require 'flagsmith/sdk/errors'
|
|
14
16
|
require 'flagsmith/sdk/intervals'
|
|
17
|
+
require 'flagsmith/sdk/utils'
|
|
15
18
|
require 'flagsmith/sdk/pooling_manager'
|
|
16
19
|
require 'flagsmith/sdk/models/flags'
|
|
17
20
|
require 'flagsmith/sdk/models/segments'
|
|
@@ -66,7 +69,6 @@ module Flagsmith
|
|
|
66
69
|
api_client
|
|
67
70
|
analytics_processor
|
|
68
71
|
environment_data_polling_manager
|
|
69
|
-
engine
|
|
70
72
|
load_offline_handler
|
|
71
73
|
end
|
|
72
74
|
|
|
@@ -96,10 +98,6 @@ module Flagsmith
|
|
|
96
98
|
@realtime_client ||= Flagsmith::RealtimeClient.new(@config)
|
|
97
99
|
end
|
|
98
100
|
|
|
99
|
-
def engine
|
|
100
|
-
@engine ||= Flagsmith::Engine::Engine.new
|
|
101
|
-
end
|
|
102
|
-
|
|
103
101
|
def analytics_processor
|
|
104
102
|
return nil unless @config.enable_analytics?
|
|
105
103
|
|
|
@@ -208,21 +206,33 @@ module Flagsmith
|
|
|
208
206
|
end
|
|
209
207
|
|
|
210
208
|
def get_identity_segments(identifier, traits = {})
|
|
211
|
-
unless environment
|
|
212
|
-
raise Flagsmith::ClientError,
|
|
213
|
-
'Local evaluation or offline handler is required to obtain identity segments.'
|
|
214
|
-
end
|
|
209
|
+
raise Flagsmith::ClientError, 'Local evaluation or offline handler is required to obtain identity segments.' unless environment
|
|
215
210
|
|
|
216
211
|
identity_model = get_identity_model(identifier, traits)
|
|
217
|
-
|
|
218
|
-
|
|
212
|
+
context = Flagsmith::Engine::Mappers.get_evaluation_context(environment, identity_model)
|
|
213
|
+
raise Flagsmith::ClientError, 'Local evaluation required to obtain identity segments' unless context
|
|
214
|
+
|
|
215
|
+
evaluation_result = Flagsmith::Engine.get_evaluation_result(context)
|
|
216
|
+
evaluation_result[:segments].filter_map do |segment_result|
|
|
217
|
+
id = segment_result.dig(:metadata, :id)
|
|
218
|
+
Flagsmith::Segments::Segment.new(id: id, name: segment_result[:name]) if id
|
|
219
|
+
end
|
|
219
220
|
end
|
|
220
221
|
|
|
221
222
|
private
|
|
222
223
|
|
|
223
|
-
def environment_flags_from_document
|
|
224
|
-
Flagsmith::
|
|
225
|
-
|
|
224
|
+
def environment_flags_from_document # rubocop:disable Metrics/MethodLength
|
|
225
|
+
context = Flagsmith::Engine::Mappers.get_evaluation_context(environment)
|
|
226
|
+
|
|
227
|
+
unless context
|
|
228
|
+
raise Flagsmith::ClientError,
|
|
229
|
+
'Unable to get flags. No environment present.'
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
evaluation_result = Flagsmith::Engine.get_evaluation_result(context)
|
|
233
|
+
|
|
234
|
+
Flagsmith::Flags::Collection.from_evaluation_result(
|
|
235
|
+
evaluation_result,
|
|
226
236
|
analytics_processor: analytics_processor,
|
|
227
237
|
default_flag_handler: default_flag_handler,
|
|
228
238
|
offline_handler: offline_handler
|
|
@@ -231,12 +241,13 @@ module Flagsmith
|
|
|
231
241
|
|
|
232
242
|
def get_identity_flags_from_document(identifier, traits = {})
|
|
233
243
|
identity_model = get_identity_model(identifier, traits)
|
|
244
|
+
context = Flagsmith::Engine::Mappers.get_evaluation_context(environment, identity_model)
|
|
245
|
+
raise Flagsmith::ClientError, 'Unable to get flags. No environment present.' unless context
|
|
234
246
|
|
|
235
|
-
Flagsmith::
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
analytics_processor: analytics_processor,
|
|
239
|
-
default_flag_handler: default_flag_handler,
|
|
247
|
+
evaluation_result = Flagsmith::Engine.get_evaluation_result(context)
|
|
248
|
+
Flagsmith::Flags::Collection.from_evaluation_result(
|
|
249
|
+
evaluation_result,
|
|
250
|
+
analytics_processor: analytics_processor, default_flag_handler: default_flag_handler,
|
|
240
251
|
offline_handler: offline_handler
|
|
241
252
|
)
|
|
242
253
|
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"bootstrap-sha": "2535f5d5bf16e4cb1ca30e10dd2e925eb62dbd9b",
|
|
3
|
+
"packages": {
|
|
4
|
+
".": {
|
|
5
|
+
"release-type": "ruby",
|
|
6
|
+
"changelog-path": "CHANGELOG.md",
|
|
7
|
+
"bump-minor-pre-major": false,
|
|
8
|
+
"bump-patch-for-minor-pre-major": false,
|
|
9
|
+
"draft": false,
|
|
10
|
+
"prerelease": false,
|
|
11
|
+
"include-component-in-tag": false
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json",
|
|
15
|
+
"changelog-sections": [
|
|
16
|
+
{
|
|
17
|
+
"type": "feat",
|
|
18
|
+
"hidden": false,
|
|
19
|
+
"section": "Features"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"type": "fix",
|
|
23
|
+
"hidden": false,
|
|
24
|
+
"section": "Bug Fixes"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"type": "ci",
|
|
28
|
+
"hidden": false,
|
|
29
|
+
"section": "CI"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"type": "docs",
|
|
33
|
+
"hidden": false,
|
|
34
|
+
"section": "Docs"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
"type": "deps",
|
|
38
|
+
"hidden": false,
|
|
39
|
+
"section": "Dependency Updates"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"type": "perf",
|
|
43
|
+
"hidden": false,
|
|
44
|
+
"section": "Performance Improvements"
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"type": "refactor",
|
|
48
|
+
"hidden": false,
|
|
49
|
+
"section": "Refactoring"
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"type": "test",
|
|
53
|
+
"hidden": false,
|
|
54
|
+
"section": "Tests"
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"type": "chore",
|
|
58
|
+
"hidden": false,
|
|
59
|
+
"section": "Other"
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
}
|
data/renovate.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
|
3
|
+
"extends": ["config:recommended", ":semanticCommitScopeDisabled"],
|
|
4
|
+
"ignorePresets": [":semanticPrefixFixDepsChoreOthers"],
|
|
5
|
+
"semanticCommitType": "deps",
|
|
6
|
+
"git-submodules": { "enabled": true },
|
|
7
|
+
"packageRules": [
|
|
8
|
+
{
|
|
9
|
+
"matchUpdateTypes": [
|
|
10
|
+
"major",
|
|
11
|
+
"minor",
|
|
12
|
+
"patch",
|
|
13
|
+
"digest",
|
|
14
|
+
"pin",
|
|
15
|
+
"rollback",
|
|
16
|
+
"bump",
|
|
17
|
+
"replacement",
|
|
18
|
+
"lockFileMaintenance"
|
|
19
|
+
],
|
|
20
|
+
"enabled": false
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"matchManagers": ["git-submodules"],
|
|
24
|
+
"versioning": "semver-coerced",
|
|
25
|
+
"enabled": true
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}
|