Skip to content

Commit 830d959

Browse files
GetEnabledFeatures API added (#218)
* GetEnabledFeatures API added * Copyright year corrected. * indent correction.
1 parent 5f9b7d5 commit 830d959

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

OptimizelySDKCore/OptimizelySDKCore/Optimizely.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ extern NSString * _Nonnull const OptimizelyNotificationsUserDictionaryExperiment
183183
userId:(nullable NSString *)userId
184184
attributes:(nullable NSDictionary<NSString *, NSString *> *)attributes;
185185

186+
/**
187+
* Get the list of features that are enabled for the user.
188+
* @param userId The user ID to be used for bucketing.
189+
* @param attributes The user's attributes.
190+
* @return NSArray<NSString> List of the feature keys that are enabled for the user.
191+
*/
192+
- (NSArray<NSString *> *_Nonnull)getEnabledFeatures:(nullable NSString *)userId
193+
attributes:(nullable NSDictionary<NSString *, NSString *> *)attributes;
194+
186195
#pragma mark - trackEvent methods
187196
/**
188197
* Track an event

OptimizelySDKCore/OptimizelySDKCore/Optimizely.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,19 @@ - (NSString *)getFeatureVariableString:(nullable NSString *)featureKey
353353
userId:userId
354354
attributes:attributes];
355355
}
356+
357+
-(NSArray<NSString *> *)getEnabledFeatures:(NSString *)userId
358+
attributes:(NSDictionary<NSString *,NSString *> *)attributes {
359+
360+
NSMutableArray<NSString *> *enabledFeatures = [NSMutableArray new];
361+
for (OPTLYFeatureFlag *feature in self.config.featureFlags) {
362+
NSString *featureKey = feature.key;
363+
if ([self isFeatureEnabled:featureKey userId:userId attributes:attributes]) {
364+
[enabledFeatures addObject:featureKey];
365+
}
366+
}
367+
return enabledFeatures;
368+
}
356369

357370
#pragma mark trackEvent methods
358371
- (void)track:(NSString *)eventKey userId:(NSString *)userId

OptimizelySDKCore/OptimizelySDKCoreTests/OptimizelyTest.m

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016-2017, Optimizely, Inc. and contributors *
2+
* Copyright 2016-2018, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -928,6 +928,24 @@ - (void)testGetFeatureVariableValueForTypeWithRolloutRule {
928928
OCMVerify([decisionServiceMock getVariationForFeature:featureFlag userId:kUserId attributes:nil]);
929929
[decisionServiceMock stopMocking];
930930
}
931+
932+
#pragma mark - GetEnabledFeatures Tests
933+
934+
// should return empty feature array as no feature is enabled for user
935+
-(void)testGetEnabledFeaturesWithNoFeatureEnabledForUser {
936+
id optimizelyMock = OCMPartialMock(self.optimizely);
937+
OCMStub([optimizelyMock isFeatureEnabled:[OCMArg any] userId:kUserId attributes:self.attributes]).andReturn(false);
938+
XCTAssertEqual([optimizelyMock getEnabledFeatures:kUserId attributes:self.attributes].count, 0);
939+
OCMVerify([optimizelyMock isFeatureEnabled:[OCMArg any] userId:kUserId attributes:self.attributes]);
940+
[optimizelyMock stopMocking];
941+
}
942+
943+
// should return empty feature array as no feature is enabled for user
944+
-(void)testGetEnabledFeaturesWithSomeFeaturesEnabledForUser {
945+
NSArray<NSString *> *enabledFeatures = @[@"booleanFeature", @"booleanSingleVariableFeature", @"multiVariateFeature"];
946+
NSArray<NSString *> *features = [self.optimizely getEnabledFeatures:kUserId attributes:self.attributes];
947+
XCTAssertEqualObjects(features, enabledFeatures);
948+
}
931949

932950
#pragma mark - Helper Methods
933951

OptimizelySDKShared/OptimizelySDKShared/OPTLYClient.m

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016, Optimizely, Inc. and contributors *
2+
* Copyright 2016, 2018, Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -197,6 +197,19 @@ - (int)getFeatureVariableInteger:(nullable NSString *)featureKey
197197
attributes:attributes];
198198
}
199199
}
200+
201+
- (NSArray<NSString *> *)getEnabledFeatures:(nullable NSString *)userId
202+
attributes:(nullable NSDictionary<NSString *,NSString *> *)attributes {
203+
if (self.optimizely == nil) {
204+
[self.logger logMessage:OPTLYLoggerMessagesClientDummyOptimizelyError
205+
withLevel:OptimizelyLogLevelError];
206+
return [NSArray new];
207+
}
208+
else {
209+
return [self.optimizely getEnabledFeatures:userId
210+
attributes:attributes];
211+
}
212+
}
200213

201214

202215
- (NSString * _Nullable)getFeatureVariableString:(nullable NSString *)featureKey

0 commit comments

Comments
 (0)