Skip to content

Commit 1c1c926

Browse files
committed
Add identity overrides to flagsmith client
1 parent 24443cc commit 1c1c926

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

lib/flagsmith.rb

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ class Client # rubocop:disable Metrics/ClassLength
4949
#
5050
# You can see full description in the Flagsmith::Config
5151

52-
attr_reader :config, :environment
52+
attr_reader :config, :environment, :identity_overrides_by_identifier
5353

5454
delegate Flagsmith::Config::OPTIONS => :@config
5555

5656
def initialize(config)
5757
@_mutex = Mutex.new
5858
@config = Flagsmith::Config.new(config)
59+
@identity_overrides_by_identifier = {}
5960

6061
validate_offline_mode!
6162

@@ -113,6 +114,16 @@ def environment_data_polling_manager
113114
# You only need to call this if you wish to bypass environment_refresh_interval_seconds.
114115
def update_environment
115116
@_mutex.synchronize { @environment = environment_from_api }
117+
update_identity_overrides
118+
end
119+
120+
def update_identity_overrides
121+
return unless @environment
122+
123+
@identity_overrides_by_identifier = {}
124+
@environment.identity_overrides.each do |identity|
125+
@identity_overrides_by_identifier[identity.identifier] = identity
126+
end
116127
end
117128

118129
def environment_from_api
@@ -177,7 +188,7 @@ def get_identity_segments(identifier, traits = {})
177188
'Local evaluation or offline handler is required to obtain identity segments.'
178189
end
179190

180-
identity_model = build_identity_model(identifier, traits)
191+
identity_model = get_identity_model(identifier, traits)
181192
segment_models = engine.get_identity_segments(environment, identity_model)
182193
segment_models.map { |sm| Flagsmith::Segments::Segment.new(id: sm.id, name: sm.name) }.compact
183194
end
@@ -194,7 +205,7 @@ def environment_flags_from_document
194205
end
195206

196207
def get_identity_flags_from_document(identifier, traits = {})
197-
identity_model = build_identity_model(identifier, traits)
208+
identity_model = get_identity_model(identifier, traits)
198209

199210
Flagsmith::Flags::Collection.from_feature_state_models(
200211
engine.get_identity_feature_states(environment, identity_model),
@@ -275,19 +286,28 @@ def process_identity_flags_from_api(identifier, traits = {})
275286
)
276287
end
277288

278-
def build_identity_model(identifier, traits = {})
289+
# rubocop:disable Metrics/MethodLength
290+
def get_identity_model(identifier, traits = {})
279291
unless environment
280292
raise Flagsmith::ClientError,
281-
'Unable to build identity model when no local environment present.'
293+
'Unable to get identity model when no local environment present.'
282294
end
283295

284296
trait_models = traits.map do |key, value|
285297
Flagsmith::Engine::Identities::Trait.new(trait_key: key, trait_value: value)
286298
end
299+
300+
if identity_overrides_by_identifier.key? identifier
301+
identity = identity_overrides_by_identifier[identifier]
302+
identity.update_traits trait_models
303+
return identity
304+
end
305+
287306
Flagsmith::Engine::Identity.new(
288307
identity_traits: trait_models, environment_api_key: environment_key, identifier: identifier
289308
)
290309
end
310+
# rubocop:enable Metrics/MethodLength
291311

292312
def generate_identities_data(identifier, traits = {})
293313
{

0 commit comments

Comments
 (0)