@kathondvla/display-responsibilities
v1.0.22
Published
This module converts an array of /responsibilities into an array of strings
Downloads
2
Maintainers
Keywords
Readme
display-responsibilities
This is a module that converts an array of /responsibilities into an array of strings.
Installing
Installation is simple using npm :
$ cd [your_project]
$ npm install --save display-responsibilities
#About
The main idea of this module is to receive all /responsibilities of a person and transform it into an array of strings. The returned strings indicates how those responsibilities have to be displayed in all apps. In this way we will see the same displayed pattern for person responsibilities in all our apps. The module is NOT in charge of solving all necessary dependencies like getting organisation's names, position's names, etc. So you need to provide a function that make all api calls.
Usage
Start by requiring the module in your code.
var DisplayResponsibilities = require('display-responsibilities');
Then instantiate a client:
var client = new DisplayResponsibilities();
Now we are ready to start converting responsibilities.
Function Definitions
Below is a description of the different types of functions that you can use. It describes the inputs and outputs of the different functions. Most of these function return a Q promise.
convertToStrings (responsibilities,getFromApi,callback)
This is a q promise method and it allows you to transform /responsibilities objects into an array of strings to be displayed on a screen. It will also receive a function as a parameter where you have to solve all api calls inside. This getFromApi could be a q promise function and should looks like:
// href ~ /organisations/uuid
function getFromApi(href){
var deferred = Q.defer();
var baseUrl = "http://api.vsko.be";
var completeURL = baseUrl+href;
var config = {
// ... user, pass, headers ...
};
needle.get(completeURL,config,function(error,response) {
if (error) {
return deferred.reject(error);
}
deferred.resolve(response.body);
});
return deferred.promise;
};
or you can use a callback function too:
function getFromApi(href,callback){
var firstPart = "http://api.vsko.be";
var config = {
// ... user, pass, headers ...
};
var completeURL = firstPart+href;
needle.get(completeURL,config,function(error,response) {
if (error) {
callback(error);
}else{
callback(undefined,response.body);
}
});
}
If you are using the module in client side (like Angular.js) your getFromApi function could be:
function getFromApi(href){
var deferred = $q.defer();
var firstPart = "http://api.vsko.be";
var completeURL = firstPart+href;
$http.get(completeURL,{
crossDomain: true,
})
.success(function(data) {
deferred.resolve(data);
})
.error(function(error) {
deferred.reject(err)
});
return deferred.promise;
};
Then you can use "convertToStrings" function as a promise:
client.convertToStrings(responsibilities,getFromApi)
.then(function(responsibilities){
responsibilities.forEach(function(stringResponsibility){
console.log(stringResponsibility);
});
});
or with a callback way:
client.convertToStrings(responsibilities,getFromApi,function(error,response){
if(!error){
response.forEach(function(stringResponsibility){
console.log(stringResponsibility);
});
}
});
Tests
We use mocha for testing. Inside test folder you will see a file called config.sample.json that looks like:
{
"needleRetry": {
"fullDocument": false
},
"needle": {
"username": "user",
"password": "pass",
"open_timeout": 0,
"decode_response": false,
"follow_max": 100,
"headers": { "TOKEN": "pass"}
},
"retry": {
"retries": 3
}
}
You need to rename it as config.json and change "user", "pass" and {"TOKEN" : "pass"} in order test to run ok. Finally to run test just do:
$ npm test
and you will see:
#Converting simple responsibilities
✓ should include customPositionTitle in response
✓ should exclude not active responsibilities
✓ should expand subResponsibilities
#Converting responsibilities
✓ should return 13 strings for Dominic
✓ should return 7 strings (4 Diary + 3 others) for Dirk
✓ should return 12 strings (6 Diary + 8 others) for Rita
✓ should return 4 strings (2 Diary + 5 others) for Nadine
✓ should return 2 string (1 Diary + 5 others) for Gerda
✓ should return 3 strings (1 Diary + 2 others) for Ann D.
✓ should return 15 strings (5 Diary + 10 others) for Ann L.
✓ should return 3 string (1 Diary + 2 others) for Alain
✓ should return 2 string (1 Diary + 9 others) for Bart
#Converting responsibilities in boundary cases
✓ should be rejected if no parameters are passed
✓ should be rejected if undefined responsibilities are passed
✓ should be rejected if responsibilities parameter is not an array
✓ should be rejected if responsibilities parameter is null
✓ should NOT be rejected if responsibilities is an empty array
✓ should be rejected for empty function is passed
✓ should be rejected if no getFromApi function is passed
✓ should NOT be rejected if getFromApi is not a promise, but a regular callback function
20 passing