gw2interface
Version:
Guild Wars 2 API
63 lines (50 loc) • 1.73 kB
JavaScript
// Generated by CoffeeScript 1.8.0
var Gw2Interface, q, request;
request = require('request');
q = require('q');
Gw2Interface = (function() {
var apiEndpoint, apiEndpointName, apiEndpoints, apiVersion, baseUrl, buildMethod, _i, _len;
function Gw2Interface() {}
baseUrl = "https://api.guildwars2.com/";
apiVersion = "v2";
apiEndpoints = ["/commerce/exchange", "/commerce/exchange/coins", "/commerce/exchange/gems", "/commerce/listings", "/commerce/prices", "/items", "/quaggans", "/recipes", "/recipes/search", "/worlds"];
for (_i = 0, _len = apiEndpoints.length; _i < _len; _i++) {
apiEndpoint = apiEndpoints[_i];
apiEndpointName = apiEndpoint.split('/').reduce(function(x, y) {
if (x === null || x === "") {
return y;
} else {
return x + y[0].toUpperCase() + y.slice(1).toLowerCase();
}
});
buildMethod = function(endPoint) {
return function(options) {
if (options == null) {
options = {};
}
return this.callEndpoint(endPoint, options);
};
};
Gw2Interface.prototype[apiEndpointName] = buildMethod(apiEndpoint);
}
Gw2Interface.prototype.callEndpoint = function(endPoint, options) {
var deferred, endPointUrl;
if (options == null) {
options = {};
}
deferred = q.defer();
endPointUrl = baseUrl + apiVersion + endPoint;
request.get(endPointUrl, {
qs: options
}, function(error, response, body) {
if (!error && response.statusCode === 200) {
return deferred.resolve(JSON.parse(body));
} else {
return deferred.reject(error);
}
});
return deferred.promise;
};
return Gw2Interface;
})();
module.exports = Gw2Interface;