2 'app/yangman/models/historylist.model',
3 'app/yangman/models/collectionlist.model',
4 'app/yangman/models/history-request.model',
5 ], function (HistoryListModel, CollectionListModel, HistoryRequestModel) {
8 angular.module('app.yangman').service('RequestsService', RequestsService);
10 RequestsService.$inject = ['PathUtilsService', 'ParametersService', 'ParsingJsonService', 'YangUtilsService'];
12 function RequestsService(PathUtilsService, ParametersService, ParsingJsonService, YangUtilsService){
16 service.applyParams = applyParams;
17 service.createEmptyCollectionList = createEmptyCollectionList;
18 service.createEmptyHistoryList = createEmptyHistoryList;
19 service.createHistoryRequestFromElement = createHistoryRequestFromElement;
20 service.createHistoryRequest = createHistoryRequest;
21 service.scanDataParams = scanDataParams;
22 service.replaceStringInText = replaceStringInText;
25 * Scan used parameters in current line of codemirror
26 * @param {ParametersListModel} paramsObj - list of parameters to be searched for
27 * @param {string} lineString - line from current codemirror to be inspected
28 * @returns array of {ParameterModel}
30 function scanDataParams(paramsObj, lineString) {
32 var usedParamLabelArray = [];
34 var params = lineString ? lineString.match(/<<(?!<<)[a-zA-Z0-9]+>>/g) : null;
39 .forEach(function (param) {
40 usedParamLabelArray.push(removeUnwantedChars(param));
44 var returnedParamsList = paramsObj.list.filter( function (param){
45 var paramIndex = usedParamLabelArray.indexOf(param.name);
47 if ( paramIndex !== -1 ) {
48 return usedParamLabelArray.splice(paramIndex, 1);
55 usedParamLabelArray.forEach(function (param){
56 returnedParamsList.push(ParametersService.createParameter({ name: param }));
59 return returnedParamsList;
62 * remove chars greater then and less then from parameter definition
66 function removeUnwantedChars(val){
67 var string = val.substring(2);
68 return string.substring(0, string.indexOf('>>'));
78 function onlyUnique(value, index, self) {
79 return self.indexOf(value) === index;
84 * Replace all parameters with its values
89 function applyParams(paramsObj, data) {
90 var dataStr = JSON.stringify(data);
92 if (paramsObj && paramsObj.hasOwnProperty('list')) {
93 paramsObj.list.forEach(function (param){
94 dataStr = service.replaceStringInText(dataStr, '<<' + param.name + '>>', param.value);
98 return ParsingJsonService.parseJson(dataStr);
102 * Service for replacing string in text
104 * @param strToReplace
108 function replaceStringInText(text, strToReplace, newStr) {
109 var replacedText = text;
110 if (text.indexOf(strToReplace) > -1) {
111 replacedText = text.split(strToReplace).join(newStr);
117 * Service for creating basic history object
119 * @param receivedData
128 function createHistoryRequest(sentData, receivedData, path, operation, status, name, collection, timestamp,
129 responseStatus, responseStatusText, responseTime) {
130 var receivedDataProcessed = status === 'success' ? receivedData : null,
131 result = new HistoryRequestModel(PathUtilsService, YangUtilsService, ParsingJsonService);
133 result.setData(sentData, receivedDataProcessed, status, path, operation, name, collection, timestamp,
134 responseStatus, responseStatusText, responseTime);
140 * Creating {HistoryRequest} from elem containing all necessary data
141 * @param {Object} elem
144 function createHistoryRequestFromElement(elem) {
145 if (!elem.hasOwnProperty('timestamp')){
146 elem.timestamp = Date.now();
149 return service.createHistoryRequest(elem.sentData, elem.receivedData, elem.path, elem.method,
150 elem.status, elem.name, elem.collection, elem.timestamp, elem.responseStatus,
151 elem.responseStatusText, elem.responseTime
156 * Service for creating empty collection list
158 * @param getApiFunction
159 * @returns {CollectionList}
161 function createEmptyCollectionList(name){
162 var result = new CollectionListModel(ParsingJsonService, service);
163 result.setName(name);
168 * Service for creating empty history list
172 function createEmptyHistoryList(name){
173 var result = new HistoryListModel(ParsingJsonService, service);
174 result.setName(name);