flagsmith 4.3.0 → 5.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d20d6b2aee787d2d25478b00eea5f1e06ad4f345097102b1381faf7455774d2
4
- data.tar.gz: c782d13647ae7c5905e0a0b46cb0085a395adbb71c95101e0b424b719e2b1a52
3
+ metadata.gz: 762e4371604e455f15016c74783f28b39383f314d5ca5d0a07dc3f70d5410c7b
4
+ data.tar.gz: a791e1905a651beb4dbcf8564b582232bd153e86b646945513f6fe7a5eb6fa57
5
5
  SHA512:
6
- metadata.gz: 135da46a33bde44fb78a54c769dc25caab7566b18c68a870306ef645cad2de6de75ee599af0fd851fa3a3562bae92bec57e57089f31fa4f9cb282fadf47df01a
7
- data.tar.gz: ead1e0df360be3a4637892f8fec1c10415967a072604b42dc113ee5034a06a235294300de59014ab35c829aceb46acca9454f29138e02dd610fd97abaadae293
6
+ metadata.gz: a01bd726d58150a70bd13186baaea23fe12d9c90455d81e5421b58676576e60f53de914f64978d1dd94805dbdc31a2dbd3266998bed1a60eecfc111492b8e90a
7
+ data.tar.gz: 01ba85177dc76451b9c4d0a1e9960b03f9adcec247477c3bad6c73814af719ce1aa5061416203a5dabc0aeba5f6f3e9f1f587d0e970be9c84742921532bbc1fb
@@ -0,0 +1 @@
1
+ {".":"5.0.0"}
data/CHANGELOG.md ADDED
@@ -0,0 +1,232 @@
1
+ # Changelog
2
+
3
+ ## [5.0.0](https://github.com/Flagsmith/flagsmith-ruby-client/compare/v4.3.0...v5.0.0) (2025-11-12)
4
+
5
+
6
+ ### ⚠ BREAKING CHANGES
7
+
8
+ * evaluation context mappers ([#87](https://github.com/Flagsmith/flagsmith-ruby-client/issues/87))
9
+
10
+ ### Features
11
+
12
+ * added-version-in-user-agent-header ([#86](https://github.com/Flagsmith/flagsmith-ruby-client/issues/86)) ([a7216f5](https://github.com/Flagsmith/flagsmith-ruby-client/commit/a7216f51ce99142473b0c12a72940d406543c4bd))
13
+ * evaluation context mappers ([#87](https://github.com/Flagsmith/flagsmith-ruby-client/issues/87)) ([287ce6d](https://github.com/Flagsmith/flagsmith-ruby-client/commit/287ce6d173cf72564da88b8e9c98d042a1deb941))
14
+
15
+
16
+ ### CI
17
+
18
+ * add release please ([#77](https://github.com/Flagsmith/flagsmith-ruby-client/issues/77)) ([a815cd9](https://github.com/Flagsmith/flagsmith-ruby-client/commit/a815cd9f2274f9bda9842786f2a09a358f203f46))
19
+
20
+
21
+ ### Docs
22
+
23
+ * removing hero image from SDK readme ([#78](https://github.com/Flagsmith/flagsmith-ruby-client/issues/78)) ([154c5b2](https://github.com/Flagsmith/flagsmith-ruby-client/commit/154c5b22f0b10c7a013605cf1265c6338f986c62))
24
+
25
+
26
+ ### Other
27
+
28
+ * add root CODEOWNERS ([#83](https://github.com/Flagsmith/flagsmith-ruby-client/issues/83)) ([da0c6e0](https://github.com/Flagsmith/flagsmith-ruby-client/commit/da0c6e045212dd3f7912f0e5e847be0bd5c3ad66))
29
+ * versioned test data ([#80](https://github.com/Flagsmith/flagsmith-ruby-client/issues/80)) ([18eedd9](https://github.com/Flagsmith/flagsmith-ruby-client/commit/18eedd9d51a6a5489fed16e47c167a8bb4c3ae4c))
30
+
31
+ <a id="v4.3.0"></a>
32
+ ## [v4.3.0](https://github.com/Flagsmith/flagsmith-ruby-client/releases/tag/v4.3.0) - 2024-12-06
33
+
34
+ ## What's Changed
35
+ * Bump rexml from 3.2.8 to 3.3.9 by [@dependabot](https://github.com/dependabot) in [#71](https://github.com/Flagsmith/flagsmith-ruby-client/pull/71)
36
+ * fix: allow false as a flag value by [@bdchauvette](https://github.com/bdchauvette) in [#68](https://github.com/Flagsmith/flagsmith-ruby-client/pull/68)
37
+ * feat: Enable realtime mode (SSE) by [@zachaysan](https://github.com/zachaysan) in [#73](https://github.com/Flagsmith/flagsmith-ruby-client/pull/73)
38
+ * Make segment operators nil-safe by [@rolodato](https://github.com/rolodato) in [#69](https://github.com/Flagsmith/flagsmith-ruby-client/pull/69)
39
+ * chore: Bump to 4.3.0 by [@zachaysan](https://github.com/zachaysan) in [#74](https://github.com/Flagsmith/flagsmith-ruby-client/pull/74)
40
+
41
+ ## New Contributors
42
+ * [@bdchauvette](https://github.com/bdchauvette) made their first contribution in [#68](https://github.com/Flagsmith/flagsmith-ruby-client/pull/68)
43
+ * [@rolodato](https://github.com/rolodato) made their first contribution in [#69](https://github.com/Flagsmith/flagsmith-ruby-client/pull/69)
44
+
45
+ **Full Changelog**: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v4.2.0...v4.3.0
46
+
47
+ [Changes][v4.3.0]
48
+
49
+
50
+ <a id="v4.2.0"></a>
51
+ ## [v4.2.0](https://github.com/Flagsmith/flagsmith-ruby-client/releases/tag/v4.2.0) - 2024-10-22
52
+
53
+ ## What's Changed
54
+ * feat: Support transient identities and traits by [@khvn26](https://github.com/khvn26) in [#66](https://github.com/Flagsmith/flagsmith-ruby-client/pull/66)
55
+
56
+
57
+ **Full Changelog**: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v4.1.1...v4.2.0
58
+
59
+ [Changes][v4.2.0]
60
+
61
+
62
+ <a id="v4.1.1"></a>
63
+ ## [v4.1.1](https://github.com/Flagsmith/flagsmith-ruby-client/releases/tag/v4.1.1) - 2024-05-22
64
+
65
+ ## What's Changed
66
+ * fix: Squash exceptions for polling manager by [@zachaysan](https://github.com/zachaysan) in [#55](https://github.com/Flagsmith/flagsmith-ruby-client/pull/55)
67
+ * Bump rexml from 3.2.6 to 3.2.8 by [@dependabot](https://github.com/dependabot) in [#60](https://github.com/Flagsmith/flagsmith-ruby-client/pull/60)
68
+
69
+
70
+ **Full Changelog**: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v4.1.0...v4.1.1
71
+
72
+ [Changes][v4.1.1]
73
+
74
+
75
+ <a id="v4.1.0"></a>
76
+ ## [v4.1.0](https://github.com/Flagsmith/flagsmith-ruby-client/releases/tag/v4.1.0) - 2024-04-19
77
+
78
+ ## What's Changed
79
+ * feat: Add identity overrides to client by [@zachaysan](https://github.com/zachaysan) in [#50](https://github.com/Flagsmith/flagsmith-ruby-client/pull/50)
80
+ * fix: Make local evaluation multivariate work by [@zachaysan](https://github.com/zachaysan) in [#51](https://github.com/Flagsmith/flagsmith-ruby-client/pull/51)
81
+ * Bump puma from 6.3.1 to 6.4.2 in /example by [@dependabot](https://github.com/dependabot) in [#39](https://github.com/Flagsmith/flagsmith-ruby-client/pull/39)
82
+ * chore: remove examples by [@dabeeeenster](https://github.com/dabeeeenster) in [#48](https://github.com/Flagsmith/flagsmith-ruby-client/pull/48)
83
+ * fix: Mute analytics processor on failure by [@zachaysan](https://github.com/zachaysan) in [#57](https://github.com/Flagsmith/flagsmith-ruby-client/pull/57)
84
+ * fix: Stop repeated calls to update environment when polling manager is set by [@zachaysan](https://github.com/zachaysan) in [#58](https://github.com/Flagsmith/flagsmith-ruby-client/pull/58)
85
+
86
+ ## New Contributors
87
+ * [@dabeeeenster](https://github.com/dabeeeenster) made their first contribution in [#48](https://github.com/Flagsmith/flagsmith-ruby-client/pull/48)
88
+
89
+ **Full Changelog**: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v4.0.1...v4.1.0
90
+
91
+ [Changes][v4.1.0]
92
+
93
+
94
+ <a id="v4.0.1"></a>
95
+ ## [v4.0.1](https://github.com/Flagsmith/flagsmith-ruby-client/releases/tag/v4.0.1) - 2024-02-06
96
+
97
+ ## What's Changed
98
+ * fix: Remove dev dependences by [@zachaysan](https://github.com/zachaysan) in [#45](https://github.com/Flagsmith/flagsmith-ruby-client/pull/45)
99
+ * chore: Bump version to 4.0.1 by [@zachaysan](https://github.com/zachaysan) in [#46](https://github.com/Flagsmith/flagsmith-ruby-client/pull/46)
100
+
101
+
102
+ **Full Changelog**: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v4.0.0...v4.0.1
103
+
104
+ [Changes][v4.0.1]
105
+
106
+
107
+ <a id="v4.0.0"></a>
108
+ ## [Version 4.0.0 (v4.0.0)](https://github.com/Flagsmith/flagsmith-ruby-client/releases/tag/v4.0.0) - 2024-01-30
109
+
110
+ ## What's Changed
111
+ * Bump puma from 5.6.4 to 6.3.1 in /example by [@dependabot](https://github.com/dependabot) in [#35](https://github.com/Flagsmith/flagsmith-ruby-client/pull/35)
112
+ * chore: update CI Ruby version to 3.1 by [@khvn26](https://github.com/khvn26) in [#34](https://github.com/Flagsmith/flagsmith-ruby-client/pull/34)
113
+ * fix: Pin the faraday gem file to avoid TypeError superclass mismatch by [@zachaysan](https://github.com/zachaysan) in [#37](https://github.com/Flagsmith/flagsmith-ruby-client/pull/37)
114
+ * feat: Add offline mode for using the client locally by [@zachaysan](https://github.com/zachaysan) in [#38](https://github.com/Flagsmith/flagsmith-ruby-client/pull/38)
115
+ * chore: Remove faraday middleware by [@zachaysan](https://github.com/zachaysan) in [#40](https://github.com/Flagsmith/flagsmith-ruby-client/pull/40)
116
+ * chore: Bump version to 4.0.0 by [@zachaysan](https://github.com/zachaysan) in [#41](https://github.com/Flagsmith/flagsmith-ruby-client/pull/41)
117
+
118
+ ## New Contributors
119
+ * [@zachaysan](https://github.com/zachaysan) made their first contribution in [#37](https://github.com/Flagsmith/flagsmith-ruby-client/pull/37)
120
+
121
+ **Full Changelog**: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v3.2.0...v4.0.0
122
+
123
+ [Changes][v4.0.0]
124
+
125
+
126
+ <a id="v3.2.0"></a>
127
+ ## [Version 3.2.0 (v3.2.0)](https://github.com/Flagsmith/flagsmith-ruby-client/releases/tag/v3.2.0) - 2023-07-25
128
+
129
+ ## What's Changed
130
+ * Prevent publish workflow from running by [@matthewelwell](https://github.com/matthewelwell) in [#25](https://github.com/Flagsmith/flagsmith-ruby-client/pull/25)
131
+ * Bump rack from 2.2.3.1 to 2.2.6.4 in /example by [@dependabot](https://github.com/dependabot) in [#27](https://github.com/Flagsmith/flagsmith-ruby-client/pull/27)
132
+ * Update dependencies by [@matthewelwell](https://github.com/matthewelwell) in [#28](https://github.com/Flagsmith/flagsmith-ruby-client/pull/28)
133
+ * ci: add rubocop by [@matthewelwell](https://github.com/matthewelwell) in [#32](https://github.com/Flagsmith/flagsmith-ruby-client/pull/32)
134
+ * feat: implement `IN` operator by [@khvn26](https://github.com/khvn26) in [#31](https://github.com/Flagsmith/flagsmith-ruby-client/pull/31)
135
+
136
+ ## New Contributors
137
+ * [@khvn26](https://github.com/khvn26) made their first contribution in [#31](https://github.com/Flagsmith/flagsmith-ruby-client/pull/31)
138
+
139
+ **Full Changelog**: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v3.1.1...v3.2.0
140
+
141
+ [Changes][v3.2.0]
142
+
143
+
144
+ <a id="v3.1.1"></a>
145
+ ## [Version 3.1.1 (v3.1.1)](https://github.com/Flagsmith/flagsmith-ruby-client/releases/tag/v3.1.1) - 2023-02-22
146
+
147
+ ## What's Changed
148
+ * Fix analytics flush timer by [@matthewelwell](https://github.com/matthewelwell) in [#23](https://github.com/Flagsmith/flagsmith-ruby-client/pull/23)
149
+ * Release 3.1.1 by [@matthewelwell](https://github.com/matthewelwell) in [#24](https://github.com/Flagsmith/flagsmith-ruby-client/pull/24)
150
+
151
+
152
+ **Full Changelog**: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v3.1.0...v3.1.1
153
+
154
+ [Changes][v3.1.1]
155
+
156
+
157
+ <a id="v3.1.0"></a>
158
+ ## [Version 3.1.0 (v3.1.0)](https://github.com/Flagsmith/flagsmith-ruby-client/releases/tag/v3.1.0) - 2022-11-01
159
+
160
+ ## What's Changed
161
+ * Add modulo operator by [@matthewelwell](https://github.com/matthewelwell) in [#21](https://github.com/Flagsmith/flagsmith-ruby-client/pull/21)
162
+ * Add get identity segments method by [@matthewelwell](https://github.com/matthewelwell) in [#18](https://github.com/Flagsmith/flagsmith-ruby-client/pull/18)
163
+ * Add IS_SET and IS_NOT_SET segment operators by [@matthewelwell](https://github.com/matthewelwell) in [#20](https://github.com/Flagsmith/flagsmith-ruby-client/pull/20)
164
+ * Release 3.1.0 by [@matthewelwell](https://github.com/matthewelwell) in [#19](https://github.com/Flagsmith/flagsmith-ruby-client/pull/19)
165
+
166
+
167
+ **Full Changelog**: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v3.0.2...v3.1.0
168
+
169
+ [Changes][v3.1.0]
170
+
171
+
172
+ <a id="v3.0.2"></a>
173
+ ## [Version 3.0.2 (v3.0.2)](https://github.com/Flagsmith/flagsmith-ruby-client/releases/tag/v3.0.2) - 2022-07-13
174
+
175
+ ## What's Changed
176
+ * Use feature name instead of id for analytics by [@matthewelwell](https://github.com/matthewelwell) in [#17](https://github.com/Flagsmith/flagsmith-ruby-client/pull/17)
177
+ * Bump rack from 2.2.3 to 2.2.3.1 in /example by [@dependabot](https://github.com/dependabot) in [#12](https://github.com/Flagsmith/flagsmith-ruby-client/pull/12)
178
+ * Bump rexml from 3.2.4 to 3.2.5 by [@dependabot](https://github.com/dependabot) in [#8](https://github.com/Flagsmith/flagsmith-ruby-client/pull/8)
179
+ * Release 3.0.2 by [@matthewelwell](https://github.com/matthewelwell) in [#16](https://github.com/Flagsmith/flagsmith-ruby-client/pull/16)
180
+
181
+ ## New Contributors
182
+ * [@dependabot](https://github.com/dependabot) made their first contribution in [#12](https://github.com/Flagsmith/flagsmith-ruby-client/pull/12)
183
+
184
+ **Full Changelog**: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v3.0.1...v3.0.2
185
+
186
+ [Changes][v3.0.2]
187
+
188
+
189
+ <a id="v3.0.1"></a>
190
+ ## [Version 3.0.1 (v3.0.1)](https://github.com/Flagsmith/flagsmith-ruby-client/releases/tag/v3.0.1) - 2022-06-09
191
+
192
+ ## What's Changed
193
+ * Add BaseFlag and DefaultFlag classes by [@matthewelwell](https://github.com/matthewelwell) in [#14](https://github.com/Flagsmith/flagsmith-ruby-client/pull/14)
194
+ * Release 3.0.1 by [@matthewelwell](https://github.com/matthewelwell) in [#15](https://github.com/Flagsmith/flagsmith-ruby-client/pull/15)
195
+
196
+
197
+ **Full Changelog**: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v3.0.0...v3.0.1
198
+
199
+ [Changes][v3.0.1]
200
+
201
+
202
+ <a id="v3.0.0"></a>
203
+ ## [Version 3.0.0 (v3.0.0)](https://github.com/Flagsmith/flagsmith-ruby-client/releases/tag/v3.0.0) - 2022-06-07
204
+
205
+ ## What's Changed
206
+ * Example app by [@byReham](https://github.com/byReham) in [#10](https://github.com/Flagsmith/flagsmith-ruby-client/pull/10)
207
+ * Update default url by [@matthewelwell](https://github.com/matthewelwell) in [#11](https://github.com/Flagsmith/flagsmith-ruby-client/pull/11)
208
+ * Release/3.0.0 by [@byReham](https://github.com/byReham) in [#9](https://github.com/Flagsmith/flagsmith-ruby-client/pull/9)
209
+
210
+ ## New Contributors
211
+ * [@byReham](https://github.com/byReham) made their first contribution in [#10](https://github.com/Flagsmith/flagsmith-ruby-client/pull/10)
212
+ * [@matthewelwell](https://github.com/matthewelwell) made their first contribution in [#11](https://github.com/Flagsmith/flagsmith-ruby-client/pull/11)
213
+
214
+ **Full Changelog**: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v2.0.0...v3.0.0
215
+
216
+ [Changes][v3.0.0]
217
+
218
+
219
+ [v4.3.0]: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v4.2.0...v4.3.0
220
+ [v4.2.0]: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v4.1.1...v4.2.0
221
+ [v4.1.1]: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v4.1.0...v4.1.1
222
+ [v4.1.0]: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v4.0.1...v4.1.0
223
+ [v4.0.1]: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v4.0.0...v4.0.1
224
+ [v4.0.0]: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v3.2.0...v4.0.0
225
+ [v3.2.0]: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v3.1.1...v3.2.0
226
+ [v3.1.1]: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v3.1.0...v3.1.1
227
+ [v3.1.0]: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v3.0.2...v3.1.0
228
+ [v3.0.2]: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v3.0.1...v3.0.2
229
+ [v3.0.1]: https://github.com/Flagsmith/flagsmith-ruby-client/compare/v3.0.0...v3.0.1
230
+ [v3.0.0]: https://github.com/Flagsmith/flagsmith-ruby-client/tree/v3.0.0
231
+
232
+ <!-- Generated by https://github.com/rhysd/changelog-from-release v3.9.0 -->
data/CODEOWNERS ADDED
@@ -0,0 +1 @@
1
+ * @flagsmith/flagsmith-back-end
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- flagsmith (4.3.0)
4
+ flagsmith (5.0.0)
5
5
  faraday (>= 2.0.1)
6
6
  faraday-retry
7
+ jsonpath (~> 1.1)
7
8
  semantic
8
9
 
9
10
  GEM
@@ -21,8 +22,11 @@ GEM
21
22
  faraday (~> 2.0)
22
23
  gem-release (2.2.2)
23
24
  json (2.7.1)
25
+ jsonpath (1.1.5)
26
+ multi_json
24
27
  language_server-protocol (3.17.0.3)
25
28
  method_source (1.0.0)
29
+ multi_json (1.17.0)
26
30
  net-http (0.4.1)
27
31
  uri
28
32
  parallel (1.24.0)
@@ -69,7 +73,7 @@ GEM
69
73
  ruby-progressbar (1.13.0)
70
74
  semantic (1.6.1)
71
75
  unicode-display_width (2.5.0)
72
- uri (0.13.0)
76
+ uri (0.13.2)
73
77
 
74
78
  PLATFORMS
75
79
  ruby
data/README.md CHANGED
@@ -1,5 +1,3 @@
1
- <img width="100%" src="https://github.com/Flagsmith/flagsmith/raw/main/static-files/hero.png"/>
2
-
3
1
  # Flagsmith Ruby Client
4
2
 
5
3
  The SDK clients for Ruby [https://www.flagsmith.com/](https://www.flagsmith.com/). Flagsmith allows you to manage feature flags and remote config across multiple projects, environments and organizations.
@@ -5,84 +5,143 @@ require 'securerandom'
5
5
 
6
6
  require_relative 'environments/models'
7
7
  require_relative 'features/models'
8
+ require_relative 'features/constants'
8
9
  require_relative 'identities/models'
9
10
  require_relative 'organisations/models'
10
11
  require_relative 'projects/models'
11
12
  require_relative 'segments/evaluator'
12
13
  require_relative 'segments/models'
13
14
  require_relative 'utils/hash_func'
15
+ require_relative 'mappers'
14
16
 
15
17
  module Flagsmith
18
+ # Core evaluation logic for feature flags
16
19
  module Engine
17
- # Flags engine methods
18
- class Engine
19
- include Flagsmith::Engine::Segments::Evaluator
20
-
21
- def get_identity_feature_state(environment, identity, feature_name, override_traits = nil)
22
- feature_states = get_identity_feature_states_dict(environment, identity, override_traits).values
20
+ extend self
21
+ include Flagsmith::Engine::Utils::HashFunc
22
+ include Flagsmith::Engine::Features::TargetingReasons
23
+ include Flagsmith::Engine::Segments::Evaluator
24
+
25
+ # Get evaluation result from evaluation context
26
+ #
27
+ # @param evaluation_context [Hash] The evaluation context
28
+ # @return [Hash] Evaluation result with flags and segments
29
+ def get_evaluation_result(evaluation_context)
30
+ evaluation_context = get_enriched_context(evaluation_context)
31
+ segments, segment_overrides = evaluate_segments(evaluation_context)
32
+ flags = evaluate_features(evaluation_context, segment_overrides)
33
+ {
34
+ flags: flags,
35
+ segments: segments
36
+ }
37
+ end
23
38
 
24
- feature_state = feature_states.find { |f| f.feature.name == feature_name }
39
+ # Returns { segments: EvaluationResultSegments; segmentOverrides: Record<string, SegmentOverride>; }
40
+ def evaluate_segments(evaluation_context)
41
+ return [], {} if evaluation_context[:segments].nil?
25
42
 
26
- raise Flagsmith::FeatureStateNotFound, 'Feature State Not Found' if feature_state.nil?
43
+ identity_segments = get_segments_from_context(evaluation_context)
27
44
 
28
- feature_state
45
+ segments = identity_segments.map do |segment|
46
+ { name: segment[:name], metadata: segment[:metadata] }.compact
29
47
  end
30
48
 
31
- def get_identity_feature_states(environment, identity, override_traits = nil)
32
- feature_states = get_identity_feature_states_dict(environment, identity, override_traits).values
49
+ segment_overrides = process_segment_overrides(identity_segments)
50
+
51
+ [segments, segment_overrides]
52
+ end
33
53
 
34
- return feature_states.select(&:enabled?) if environment.project.hide_disabled_flags
54
+ # Returns Record<string: override.name, SegmentOverride>
55
+ def process_segment_overrides(identity_segments) # rubocop:disable Metrics/MethodLength
56
+ segment_overrides = {}
35
57
 
36
- feature_states
58
+ identity_segments.each do |segment|
59
+ Array(segment[:overrides]).each do |override|
60
+ next unless should_apply_override(override, segment_overrides)
61
+
62
+ segment_overrides[override[:name]] = {
63
+ feature: override,
64
+ segment_name: segment[:name]
65
+ }
66
+ end
37
67
  end
38
68
 
39
- def get_environment_feature_state(environment, feature_name)
40
- features_state = environment.feature_states.find { |f| f.feature.name == feature_name }
69
+ segment_overrides
70
+ end
41
71
 
42
- raise Flagsmith::FeatureStateNotFound, 'Feature State Not Found' if features_state.nil?
72
+ def evaluate_features(evaluation_context, segment_overrides)
73
+ identity_key = get_identity_key(evaluation_context)
43
74
 
44
- features_state
75
+ (evaluation_context[:features] || {}).each_with_object({}) do |(_, feature), flags|
76
+ segment_override = segment_overrides[feature[:name]]
77
+ final_feature = segment_override ? segment_override[:feature] : feature
78
+
79
+ flag_result = build_flag_result(final_feature, identity_key, segment_override)
80
+ flags[final_feature[:name].to_sym] = flag_result
45
81
  end
82
+ end
46
83
 
47
- def get_environment_feature_states(environment)
48
- return environment.feature_states.select(&:enabled?) if environment.project.hide_disabled_flags
84
+ # Returns {value: any; reason?: string}
85
+ def evaluate_feature_value(feature, identity_key = nil)
86
+ return get_multivariate_feature_value(feature, identity_key) if feature[:variants]&.any? && identity_key
49
87
 
50
- environment.feature_states
51
- end
88
+ { value: feature[:value], reason: nil }
89
+ end
52
90
 
53
- private
91
+ # Returns {value: any; reason?: string}
92
+ def get_multivariate_feature_value(feature, identity_key)
93
+ percentage_value = hashed_percentage_for_object_ids([feature[:key], identity_key])
94
+ sorted_variants = (feature[:variants] || []).sort_by { |v| v[:priority] || WEAKEST_PRIORITY }
54
95
 
55
- def get_identity_feature_states_dict(environment, identity, override_traits = nil)
56
- # Get feature states from the environment
57
- feature_states = {}
58
- override = ->(fs) { feature_states[fs.feature.id] = fs }
59
- environment.feature_states.each(&override)
96
+ variant = find_matching_variant(sorted_variants, percentage_value)
97
+ variant || { value: feature[:value], reason: nil }
98
+ end
60
99
 
61
- override_by_matching_segments(environment, identity, override_traits) do |fs|
62
- override.call(fs) unless higher_segment_priority?(feature_states, fs)
63
- end
100
+ def find_matching_variant(sorted_variants, percentage_value)
101
+ start_percentage = 0
102
+ sorted_variants.each do |variant|
103
+ limit = start_percentage + variant[:weight]
104
+ return { value: variant[:value], reason: "#{TARGETING_REASON_SPLIT}; weight=#{variant[:weight]}" } if start_percentage <= percentage_value && percentage_value < limit
64
105
 
65
- # Override with any feature states defined directly the identity
66
- identity.identity_features.each(&override)
67
- feature_states
106
+ start_percentage = limit
68
107
  end
108
+ nil
109
+ end
69
110
 
70
- # Override with any feature states defined by matching segments
71
- def override_by_matching_segments(environment, identity, override_traits)
72
- identity_segments = get_identity_segments(environment, identity, override_traits)
73
- identity_segments.each do |matching_segment|
74
- matching_segment.feature_states.each do |feature_state|
75
- yield feature_state if block_given?
76
- end
77
- end
78
- end
111
+ # returns boolean
112
+ def should_apply_override(override, existing_overrides)
113
+ current_override = existing_overrides[override[:name]]
114
+ !current_override || stronger_priority?(override[:priority], current_override[:feature][:priority])
115
+ end
79
116
 
80
- def higher_segment_priority?(collection, feature_state)
81
- collection.key?(feature_state.feature.id) &&
82
- collection[feature_state.feature.id].higher_segment_priority?(
83
- feature_state
84
- )
85
- end
117
+ private
118
+
119
+ def build_flag_result(feature, identity_key, segment_override)
120
+ evaluated = evaluate_feature_value(feature, identity_key)
121
+
122
+ flag_result = {
123
+ name: feature[:name],
124
+ enabled: feature[:enabled],
125
+ value: evaluated[:value],
126
+ reason: evaluated[:reason] || (segment_override ? "#{TARGETING_REASON_TARGETING_MATCH}; segment=#{segment_override[:segment_name]}" : TARGETING_REASON_DEFAULT)
127
+ }
128
+
129
+ flag_result[:metadata] = feature[:metadata] if feature[:metadata]
130
+ flag_result
131
+ end
132
+
133
+ # Extract identity key from evaluation context
134
+ #
135
+ # @param evaluation_context [Hash] The evaluation context
136
+ # @return [String, nil] The identity key or nil if no identity
137
+ def get_identity_key(evaluation_context)
138
+ return nil unless evaluation_context[:identity]
139
+
140
+ evaluation_context[:identity][:key]
141
+ end
142
+
143
+ def stronger_priority?(priority_a, priority_b)
144
+ (priority_a || WEAKEST_PRIORITY) < (priority_b || WEAKEST_PRIORITY)
86
145
  end
87
146
  end
88
147
  end
@@ -4,17 +4,20 @@ module Flagsmith
4
4
  module Engine
5
5
  # EnvironmentModel
6
6
  class Environment
7
- attr_reader :id, :api_key
7
+ attr_reader :id, :api_key, :name
8
8
  attr_accessor :project, :feature_states, :amplitude_config, :segment_config,
9
9
  :mixpanel_config, :heap_config, :identity_overrides
10
10
 
11
- def initialize(id:, api_key:, project:, feature_states: [], identity_overrides: [])
11
+ # rubocop:disable Metrics/ParameterLists
12
+ def initialize(id:, api_key:, project:, name: nil, feature_states: [], identity_overrides: [])
12
13
  @id = id
13
14
  @api_key = api_key
15
+ @name = name
14
16
  @project = project
15
17
  @feature_states = feature_states
16
18
  @identity_overrides = identity_overrides
17
19
  end
20
+ # rubocop:enable Metrics/ParameterLists
18
21
 
19
22
  class << self
20
23
  # rubocop:disable Metrics/MethodLength
@@ -28,7 +31,7 @@ module Flagsmith
28
31
  Flagsmith::Engine::Identity.build(io)
29
32
  end
30
33
 
31
- new(**json.slice(:id, :api_key).merge(
34
+ new(**json.slice(:id, :api_key, :name).merge(
32
35
  project: project,
33
36
  feature_states: feature_states,
34
37
  identity_overrides: identity_overrides
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../utils/hash_func'
4
+ require_relative '../features/constants'
5
+ require_relative '../segments/evaluator'
6
+
7
+ module Flagsmith
8
+ module Engine
9
+ module Evaluation
10
+ # Core evaluation logic for feature flags
11
+ module Core
12
+ extend self
13
+ include Flagsmith::Engine::Utils::HashFunc
14
+ include Flagsmith::Engine::Features::TargetingReasons
15
+ include Flagsmith::Engine::Segments::Evaluator
16
+ # Get evaluation result from evaluation context
17
+ #
18
+ # @param evaluation_context [Hash] The evaluation context
19
+ # @return [Hash] Evaluation result with flags and segments
20
+ def get_evaluation_result(evaluation_context)
21
+ evaluation_context = get_enriched_context(evaluation_context)
22
+ segments, segment_overrides = evaluate_segments(evaluation_context)
23
+ flags = evaluate_features(evaluation_context, segment_overrides)
24
+ {
25
+ flags: flags,
26
+ segments: segments
27
+ }
28
+ end
29
+
30
+ # Returns { segments: EvaluationResultSegments; segmentOverrides: Record<string, SegmentOverride>; }
31
+ def evaluate_segments(evaluation_context)
32
+ return [], {} if evaluation_context[:segments].nil?
33
+
34
+ identity_segments = get_segments_from_context(evaluation_context)
35
+
36
+ segments = identity_segments.map do |segment|
37
+ { name: segment[:name], metadata: segment[:metadata] }.compact
38
+ end
39
+
40
+ segment_overrides = process_segment_overrides(identity_segments)
41
+
42
+ [segments, segment_overrides]
43
+ end
44
+
45
+ # Returns Record<string: override.name, SegmentOverride>
46
+ def process_segment_overrides(identity_segments) # rubocop:disable Metrics/MethodLength
47
+ segment_overrides = {}
48
+
49
+ identity_segments.each do |segment|
50
+ Array(segment[:overrides]).each do |override|
51
+ next unless should_apply_override(override, segment_overrides)
52
+
53
+ segment_overrides[override[:name]] = {
54
+ feature: override,
55
+ segment_name: segment[:name]
56
+ }
57
+ end
58
+ end
59
+
60
+ segment_overrides
61
+ end
62
+
63
+ def evaluate_features(evaluation_context, segment_overrides)
64
+ identity_key = get_identity_key(evaluation_context)
65
+
66
+ (evaluation_context[:features] || {}).each_with_object({}) do |(_, feature), flags|
67
+ segment_override = segment_overrides[feature[:name]]
68
+ final_feature = segment_override ? segment_override[:feature] : feature
69
+
70
+ flag_result = build_flag_result(final_feature, identity_key, segment_override)
71
+ flags[final_feature[:name].to_sym] = flag_result
72
+ end
73
+ end
74
+
75
+ # Returns {value: any; reason?: string}
76
+ def evaluate_feature_value(feature, identity_key = nil)
77
+ return get_multivariate_feature_value(feature, identity_key) if feature[:variants]&.any? && identity_key
78
+
79
+ { value: feature[:value], reason: nil }
80
+ end
81
+
82
+ # Returns {value: any; reason?: string}
83
+ def get_multivariate_feature_value(feature, identity_key)
84
+ percentage_value = hashed_percentage_for_object_ids([feature[:key], identity_key])
85
+ sorted_variants = (feature[:variants] || []).sort_by { |v| v[:priority] || WEAKEST_PRIORITY }
86
+
87
+ variant = find_matching_variant(sorted_variants, percentage_value)
88
+ variant || { value: feature[:value], reason: nil }
89
+ end
90
+
91
+ def find_matching_variant(sorted_variants, percentage_value)
92
+ start_percentage = 0
93
+ sorted_variants.each do |variant|
94
+ limit = start_percentage + variant[:weight]
95
+ return { value: variant[:value], reason: "#{TARGETING_REASON_SPLIT}; weight=#{variant[:weight]}" } if start_percentage <= percentage_value && percentage_value < limit
96
+
97
+ start_percentage = limit
98
+ end
99
+ nil
100
+ end
101
+
102
+ # returns boolean
103
+ def should_apply_override(override, existing_overrides)
104
+ current_override = existing_overrides[override[:name]]
105
+ !current_override || stronger_priority?(override[:priority], current_override[:feature][:priority])
106
+ end
107
+
108
+ private
109
+
110
+ def build_flag_result(feature, identity_key, segment_override)
111
+ evaluated = evaluate_feature_value(feature, identity_key)
112
+
113
+ flag_result = {
114
+ name: feature[:name],
115
+ enabled: feature[:enabled],
116
+ value: evaluated[:value],
117
+ reason: evaluated[:reason] || (segment_override ? "#{TARGETING_REASON_TARGETING_MATCH}; segment=#{segment_override[:segment_name]}" : TARGETING_REASON_DEFAULT)
118
+ }
119
+
120
+ flag_result[:metadata] = feature[:metadata] if feature[:metadata]
121
+ flag_result
122
+ end
123
+
124
+ # Extract identity key from evaluation context
125
+ #
126
+ # @param evaluation_context [Hash] The evaluation context
127
+ # @return [String, nil] The identity key or nil if no identity
128
+ def get_identity_key(evaluation_context)
129
+ return nil unless evaluation_context[:identity]
130
+
131
+ evaluation_context[:identity][:key] ||
132
+ "#{evaluation_context[:environment][:key]}_#{evaluation_context[:identity][:identifier]}"
133
+ end
134
+
135
+ # returns boolean
136
+ def stronger_priority?(priority_a, priority_b)
137
+ (priority_a || WEAKEST_PRIORITY) < (priority_b || WEAKEST_PRIORITY)
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end