UNPKG

node-511

Version:

Lightweight node.js wrapper for the 511.org real time driving times and transit departure APIs

69 lines (58 loc) 1.98 kB
// Generated by CoffeeScript 1.7.1 (function() { var AbstractService, request, xml2js, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; request = require('request'); xml2js = require('xml2js'); AbstractService = (function() { function AbstractService(url) { this.invoke = __bind(this.invoke, this); this.url = (function() { if (url != null) { return url; } else { throw new Error('No URL provided for endpoint'); } })(); this.timeout = 5000; this.userAgent = "node-511 API wrapper for node.js - https://github.com/abstractvector/node-511"; } AbstractService.prototype.invoke = function(parameters, callback) { var self, service; if (parameters == null) { parameters = {}; } if (callback == null) { callback = function() {}; } self = this; service = this.constructor.name; return this._request(this.url, parameters, function(err, response, body) { if ((err != null ? err.code : void 0) === 'ETIMEDOUT') { return callback(new Error("Call timed out after " + self.timeout + "ms when calling " + service)); } return xml2js.parseString(body, function(err, result) { if (err) { return callback(new Error("Error when parsing XML response for " + service + ": " + err.message)); } return callback(null, self._parse(result, body, response)); }); }); }; AbstractService.prototype._request = function(url, parameters, callback) { return request({ url: url, qs: parameters, headers: { "User-Agent": this.userAgent }, timeout: this.timeout }, callback); }; AbstractService.prototype._parse = function(obj) { return {}; }; return AbstractService; })(); module.exports = AbstractService; }).call(this);