2015-11-11 07:33:19 +03:00
|
|
|
(function () {
|
2018-04-30 13:33:36 +03:00
|
|
|
var apiUrl = '{{api-url}}',
|
2015-12-15 13:41:53 +03:00
|
|
|
clientId,
|
|
|
|
clientSecret,
|
|
|
|
url,
|
|
|
|
init;
|
|
|
|
|
2015-11-11 07:33:19 +03:00
|
|
|
function generateQueryString(object) {
|
2015-12-10 17:46:58 +03:00
|
|
|
var queries = [],
|
2015-11-11 07:33:19 +03:00
|
|
|
i;
|
|
|
|
|
|
|
|
if (!object) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i in object) {
|
|
|
|
if (object.hasOwnProperty(i) && (!!object[i] || object[i] === false)) {
|
2015-12-10 17:46:58 +03:00
|
|
|
queries.push(i + '=' + encodeURIComponent(object[i]));
|
2015-11-11 07:33:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 17:46:58 +03:00
|
|
|
if (queries.length) {
|
|
|
|
return '?' + queries.join('&');
|
|
|
|
}
|
|
|
|
return '';
|
2015-11-11 07:33:19 +03:00
|
|
|
}
|
|
|
|
|
2015-12-15 13:41:53 +03:00
|
|
|
url = {
|
2015-11-11 07:33:19 +03:00
|
|
|
api: function () {
|
|
|
|
var args = Array.prototype.slice.call(arguments),
|
2015-12-18 12:15:24 +03:00
|
|
|
queryOptions,
|
|
|
|
requestUrl = apiUrl;
|
2015-11-11 07:33:19 +03:00
|
|
|
|
2016-02-29 09:25:38 +03:00
|
|
|
queryOptions = args.pop();
|
|
|
|
|
|
|
|
if (queryOptions && typeof queryOptions !== 'object') {
|
|
|
|
args.push(queryOptions);
|
2015-11-11 07:33:19 +03:00
|
|
|
queryOptions = {};
|
|
|
|
}
|
|
|
|
|
2016-02-29 09:25:38 +03:00
|
|
|
queryOptions = queryOptions || {};
|
|
|
|
|
2015-12-15 13:41:53 +03:00
|
|
|
queryOptions.client_id = clientId;
|
|
|
|
queryOptions.client_secret = clientSecret;
|
2015-11-11 07:33:19 +03:00
|
|
|
|
|
|
|
if (args.length) {
|
|
|
|
args.forEach(function (el) {
|
2015-12-18 12:15:24 +03:00
|
|
|
requestUrl += el.replace(/^\/|\/$/g, '') + '/';
|
2015-11-11 07:33:19 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-18 12:15:24 +03:00
|
|
|
return requestUrl + generateQueryString(queryOptions);
|
2015-11-11 07:33:19 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-15 13:41:53 +03:00
|
|
|
init = function (options) {
|
|
|
|
clientId = options.clientId ? options.clientId : '';
|
|
|
|
clientSecret = options.clientSecret ? options.clientSecret : '';
|
2015-12-15 17:16:00 +03:00
|
|
|
apiUrl = options.url ? options.url : (apiUrl.match(/{\{api-url}}/) ? '' : apiUrl);
|
2015-12-15 13:41:53 +03:00
|
|
|
};
|
|
|
|
|
2015-11-11 07:33:19 +03:00
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
window.ghost = window.ghost || {};
|
|
|
|
window.ghost.url = url;
|
2015-12-15 13:41:53 +03:00
|
|
|
window.ghost.init = init;
|
2015-11-11 07:33:19 +03:00
|
|
|
}
|
2015-12-10 17:46:58 +03:00
|
|
|
|
2015-11-11 07:33:19 +03:00
|
|
|
if (typeof module !== 'undefined') {
|
2015-12-15 13:41:53 +03:00
|
|
|
module.exports = {
|
|
|
|
url: url,
|
|
|
|
init: init
|
|
|
|
};
|
2015-11-11 07:33:19 +03:00
|
|
|
}
|
|
|
|
})();
|