node-511
Version:
Lightweight node.js wrapper for the 511.org real time driving times and transit departure APIs
72 lines (64 loc) • 2.31 kB
JavaScript
// Generated by CoffeeScript 1.7.1
(function() {
var Client, url;
url = require('url');
Client = (function() {
Client.tokenRegistrationURL = 'http://511.org/developer-resources_api-security-token_rtt.asp';
function Client(options) {
var bindInvoke, error, path, service, urlObj, _ref, _ref1, _ref2, _service;
if (options == null) {
options = {};
}
this.token = (function() {
if ((_ref = options.token) != null) {
return _ref;
} else {
throw new Error("No token specified - please see: " + Client.tokenRegistrationURL);
}
})();
this.host = (_ref1 = options.host) != null ? _ref1 : 'http://services.my511.org';
this.paths = {
GetAgencies: '/Transit2.0/GetAgencies.aspx',
GetRoutesForAgencies: '/Transit2.0/GetRoutesForAgencies.aspx',
GetRoutesForAgency: '/Transit2.0/GetRoutesForAgency.aspx',
GetStopsForRoute: '/Transit2.0/GetStopsForRoute.aspx',
GetStopsForRoutes: '/Transit2.0/GetStopsForRoutes.aspx',
GetNextDeparturesByStopName: '/Transit2.0/GetNextDeparturesByStopName.aspx'
};
this.services = [];
_ref2 = this.paths;
for (service in _ref2) {
path = _ref2[service];
try {
urlObj = url.parse(this.host);
urlObj.pathname = path;
_service = new (require("./apis/" + service))(url.format(urlObj));
bindInvoke = function(s) {
return function(callback, options) {
if (options == null) {
options = {};
}
if (callback == null) {
callback = function() {};
}
options.token = this.token;
return s.invoke(options, callback);
};
};
this[service] = bindInvoke(_service);
} catch (_error) {
error = _error;
console.warn("Error loading service: " + service);
this[service] = function(callback) {
if (callback == null) {
callback = function() {};
}
return callback(new Error("Service " + service + " was not loaded correctly"));
};
}
}
}
return Client;
})();
module.exports = Client;
}).call(this);