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.
@@ -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 from_feature_state_models(feature_states, identity_id: nil, **args)
176
- to_flag_object = lambda { |feature_state, acc|
177
- acc[normalize_key(feature_state.feature.name)] =
178
- Flagsmith::Flags::Flag.from_feature_state_model(feature_state, identity_id)
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
- feature_states.each_with_object({}, &to_flag_object),
183
- **args
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Flagsmith
4
- VERSION = '4.3.0'
4
+ VERSION = '5.0.0'
5
5
  end
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
- segment_models = engine.get_identity_segments(environment, identity_model)
218
- segment_models.map { |sm| Flagsmith::Segments::Segment.new(id: sm.id, name: sm.name) }.compact
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::Flags::Collection.from_feature_state_models(
225
- engine.get_environment_feature_states(environment),
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::Flags::Collection.from_feature_state_models(
236
- engine.get_identity_feature_states(environment, identity_model),
237
- identity_id: identity_model.composite_key,
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
+ }
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flagsmith
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.3.0
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Stuart
8
8
  - Brian Moelk
9
9
  - Zach Aysan
10
- autorequire:
10
+ autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2024-12-06 00:00:00.000000000 Z
13
+ date: 2026-11-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -138,6 +138,20 @@ dependencies:
138
138
  - - ">="
139
139
  - !ruby/object:Gem::Version
140
140
  version: '0'
141
+ - !ruby/object:Gem::Dependency
142
+ name: jsonpath
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: '1.1'
148
+ type: :runtime
149
+ prerelease: false
150
+ version_requirements: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - "~>"
153
+ - !ruby/object:Gem::Version
154
+ version: '1.1'
141
155
  - !ruby/object:Gem::Dependency
142
156
  name: semantic
143
157
  requirement: !ruby/object:Gem::Requirement
@@ -162,21 +176,29 @@ executables: []
162
176
  extensions: []
163
177
  extra_rdoc_files: []
164
178
  files:
179
+ - ".release-please-manifest.json"
165
180
  - ".rspec"
166
181
  - ".rubocop.yml"
167
182
  - ".rubocop_todo.yml"
168
183
  - ".ruby-version"
184
+ - CHANGELOG.md
185
+ - CODEOWNERS
169
186
  - Gemfile
170
187
  - Gemfile.lock
171
188
  - LICENCE
172
189
  - README.md
173
190
  - Rakefile
174
- - flagsmith.gemspec
175
191
  - lib/flagsmith.rb
176
192
  - lib/flagsmith/engine/core.rb
177
193
  - lib/flagsmith/engine/environments/models.rb
194
+ - lib/flagsmith/engine/evaluation/core.rb
195
+ - lib/flagsmith/engine/features/constants.rb
178
196
  - lib/flagsmith/engine/features/models.rb
179
197
  - lib/flagsmith/engine/identities/models.rb
198
+ - lib/flagsmith/engine/mappers.rb
199
+ - lib/flagsmith/engine/mappers/environment.rb
200
+ - lib/flagsmith/engine/mappers/identity.rb
201
+ - lib/flagsmith/engine/mappers/segments.rb
180
202
  - lib/flagsmith/engine/organisations/models.rb
181
203
  - lib/flagsmith/engine/projects/models.rb
182
204
  - lib/flagsmith/engine/segments/constants.rb
@@ -194,12 +216,14 @@ files:
194
216
  - lib/flagsmith/sdk/offline_handlers.rb
195
217
  - lib/flagsmith/sdk/pooling_manager.rb
196
218
  - lib/flagsmith/sdk/realtime_client.rb
219
+ - lib/flagsmith/sdk/utils.rb
197
220
  - lib/flagsmith/version.rb
221
+ - release-please-config.json
198
222
  homepage: https://flagsmith.com
199
223
  licenses: []
200
224
  metadata:
201
225
  rubygems_mfa_required: 'true'
202
- post_install_message:
226
+ post_install_message:
203
227
  rdoc_options: []
204
228
  require_paths:
205
229
  - lib
@@ -214,8 +238,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
238
  - !ruby/object:Gem::Version
215
239
  version: '0'
216
240
  requirements: []
217
- rubygems_version: 3.3.26
218
- signing_key:
241
+ rubygems_version: 3.5.22
242
+ signing_key:
219
243
  specification_version: 4
220
244
  summary: Flagsmith - Ship features with confidence
221
245
  test_files: []
data/flagsmith.gemspec DELETED
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path('lib/flagsmith/version', __dir__)
4
-
5
- Gem::Specification.new do |spec|
6
- spec.required_ruby_version = '>= 3.0.0'
7
- spec.name = 'flagsmith'
8
- spec.version = Flagsmith::VERSION
9
- spec.authors = ['Tom Stuart', 'Brian Moelk', 'Zach Aysan']
10
- spec.email = ['tom@solidstategroup.com', 'bmoelk@gmail.com', 'zachaysan@gmail.com']
11
- # Specify which files should be added to the gem when it is released.
12
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
13
- spec.files = Dir.chdir(__dir__) do
14
- `git ls-files -z`.split("\x0").reject do |f|
15
- (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
16
- end
17
- end
18
-
19
- spec.summary = 'Flagsmith - Ship features with confidence'
20
- spec.description = 'Ruby Client for Flagsmith. Ship features with confidence using feature flags and remote config. Host yourself or use our hosted version at https://flagsmith.com'
21
- spec.homepage = 'https://flagsmith.com'
22
-
23
- spec.bindir = 'exe'
24
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
25
- spec.require_paths = ['lib']
26
-
27
- spec.add_development_dependency 'bundler'
28
- spec.add_development_dependency 'gem-release'
29
- spec.add_development_dependency 'pry'
30
- spec.add_development_dependency 'pry-byebug'
31
- spec.add_development_dependency 'rake'
32
- spec.add_development_dependency 'rspec'
33
- spec.add_development_dependency 'rubocop'
34
-
35
- spec.add_dependency 'faraday', '>= 2.0.1'
36
- spec.add_dependency 'faraday-retry'
37
- spec.add_dependency 'semantic'
38
- spec.metadata['rubygems_mfa_required'] = 'true'
39
- end