UNPKG

gw2interface

Version:
40 lines (34 loc) 1.25 kB
request = require 'request' q = require 'q' class 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 apiEndpoint in apiEndpoints apiEndpointName = apiEndpoint.split('/').reduce (x, y) -> if x == null or x == "" then y else x + y[0].toUpperCase() + y[1..-1].toLowerCase() buildMethod = (endPoint) -> (options = {}) -> @callEndpoint endPoint, options Gw2Interface::[apiEndpointName] = buildMethod apiEndpoint callEndpoint: (endPoint, options = {}) -> deferred = q.defer() endPointUrl = baseUrl + apiVersion + endPoint request.get endPointUrl, qs: options, (error, response, body) -> if !error and response.statusCode == 200 deferred.resolve JSON.parse body else deferred.reject error deferred.promise module.exports = Gw2Interface