Created
September 29, 2014 03:42
-
-
Save fernandofleury/bdadf35eba34b055eb80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
angular.module('cbApp.service.clickbus', []) | |
.factory('clickbusApi', clickbusApi); | |
clickbusApi.$inject = ['$http', '$sessionStorage', 'parameters']; | |
function clickbusApi($http, $sessionStorage, parameters) { | |
return { | |
page: page, | |
get: get | |
}; | |
/** | |
* Api communication through GET method. Others methods must the used through REST services | |
* @param {string} params (as string) Request through the provided url | |
* @param {object} params (as object) Configurable object | |
* @param {string} params.url Request through the provided url | |
* @param {boolean} params.cache Cache the API response | |
* @param {string} params.storage Property name to be stored | |
* @param {string} params.refresh Ignores the cached response (If there is any) | |
* @param {function} callback Callback to be executed once the request is finished. Or the storage object is returned | |
*/ | |
function get(params, callback) { | |
if (typeof params !== 'string' && $sessionStorage[params.storage] && !params.refresh) { | |
callback($sessionStorage[params.storage]); | |
} else { | |
$http.get(parameters.api.default + (params.url ? params.url : params)) | |
.success(function(data) { | |
if (params.cache) { | |
$sessionStorage[params.storage] = data; | |
} | |
callback(data); | |
}) | |
.error(function(data) { | |
console.log(data); | |
}); | |
} | |
} | |
/** | |
* Facade method to communicate with the current page mock api | |
* @param {string} Route | |
* @return {function} Returns a promise object | |
*/ | |
function page(route) { | |
return $http.get(parameters.api[route]); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment