Skip to content

Instantly share code, notes, and snippets.

@atuttle
Created August 2, 2021 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atuttle/f695c8f27011ea2e7176e30f57d25a67 to your computer and use it in GitHub Desktop.
Save atuttle/f695c8f27011ea2e7176e30f57d25a67 to your computer and use it in GitHub Desktop.
component
accessors=true
{
property configService;
variables.defaultApiUrl = 'https://REDACTED/api/flags';
variables.semaphore = createObject("component", "com.semaphore.semaphore");
variables.apiUrl = server.system.environment['FEATURE_FLAGS_API_URL'] ?: variables.defaultApiUrl;
//custom init function, loads current feature flags state from API at
//startup once the necessary dependencies are auto-wired
public function init( required utilService ){
variables.utilService = arguments.utilService;
//load current feature flags
this.refreshFlagsFromAPI();
}
public boolean function flagEnabled( required string flagId ){
var flagIsOn = variables.semaphore.checkForUser( arguments.flagId, getCurrentUserAttributes() );
return flagIsOn;
}
public void function refreshFlagsFromAPI(){
variables.semaphore.setAllFlags( this.getAllFlagsFromAPI() );
}
public struct function getAllFlags(){
return variables.semaphore.getAllFlags();
}
public struct function buildUserAttributesFromPubsiteUser( required user ){
throw(message: "feature flags not ready for pubsite yet!");
}
public struct function buildUserAttributesFromAdminUser( required user ){
return {
'cust': configService.getCustomer(),
'env': configService.getEnv(),
'userId': user.getUserId(),
'guid': user.getGuid(),
'firstName': user.getFirstName(),
'lastName': user.getLastName(),
'roles': user.getRoles()
};
}
private function getCurrentUserAttributes(){
return request.featureFlagsUserAttributes;
}
private struct function getAllFlagsFromAPI(){
var apiResponse = variables.utilService.httpGet( variables.apiURL );
if ( !isJson(apiResponse.fileContent) ){
throw(message: "Feature Flag API response is not valid json! 😱", detail: apiResponse.fileContent);
}
var flags = deserializeJson( apiResponse.fileContent );
return flags;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment