From 5bbdac99e7e20f8015f1eb91916e72e942cb1272 Mon Sep 17 00:00:00 2001 From: Kyle Derkacz Date: Tue, 4 Nov 2014 19:28:36 -0800 Subject: [PATCH 1/2] Adding an endpoint to pull the list of agencies --- lib/index.js | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 866ea6a..89d8e07 100644 --- a/lib/index.js +++ b/lib/index.js @@ -587,6 +587,45 @@ function client () { return nearest; } + function getAgencies (callback) { + var out = {}; + + if (typeof callback !== "function") { + return {name: "TypeError", message: "callback must be a function"}; + } + + out.agencies = {}; + query("agencyList", '', function (err, data) { + var i, agencies; + try { + if (err) { + throw err; + } + + + if (isTi) { + agencies = data.getElementsByTagName("agency"); + } else { + agencies = data.document.getElementsByTagName("agency"); + } + + for (i = 0; i < agencies.length; i++) { + var tag = agencies.item(i).getAttribute('tag'); + out.agencies[tag] = { + tag: tag, + title: agencies.item(i).getAttribute('title'), + region: agencies.item(i).getAttribute('regiontitle'), + shortTitle: agencies.item(i).getAttribute('shorttitle') + } + } + + callback(null, out); + } catch (e) { + callback(e, null); + } + }) + } + /* Function: cacheAgency Load the agency data. Somewhat slow as this is often a huge file. @@ -976,7 +1015,7 @@ function client () { */ function query (command, str, cb) { - var url = baseURL + command + "&a=" + agency + str; + var url = baseURL + command + (agency ? "&a=" + agency + str : ""); request(url, function (err, response, data) { if (err) { @@ -1108,6 +1147,7 @@ function client () { exports.setActive = setActive; exports.guessActive = guessActive; + exports.getAgencies = getAgencies; exports.getAgencyCache = getAgencyCache; exports.setAgencyCache = setAgencyCache; exports.cacheAgency = cacheAgency; From d78206e7cf8799bef3fa30d9b4d3ec5f7d4c9bdf Mon Sep 17 00:00:00 2001 From: Kyle Derkacz Date: Tue, 4 Nov 2014 19:37:26 -0800 Subject: [PATCH 2/2] Adding documentation for the getAgencies endpoint --- lib/index.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/index.js b/lib/index.js index 89d8e07..01742a6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -587,6 +587,44 @@ function client () { return nearest; } + /* + Function: getAgencies + Get a list of the agencies that nextbus provides data for. + + Parameters: + callback - *function (err, data)* called when the process is complete + + Example: + > nextbus.getAgencies(callback); + + Will provide callback with an object resembling + (start code) + { agencies: + { 'actransit': + { tag: 'actransit', + title: 'AC Transit', + region: 'California-Northern', + shortTitle: null }, + 'art': + { tag: 'art', + title: 'Asheville Redefines Transit', + region: 'North Carolina', + shortTitle: null }, + 'calu-pa': + { tag: 'calu-pa', + title: 'California University of Pennsylvania', + region: 'Pennsylvania', + shortTitle: null }, + 'camarillo': + { tag: 'camarillo', + title: 'Camarillo Area (CAT)', + region: 'California-Southern', + shortTitle: 'Camarillo (CAT)' }, + ... + } + } + (end) + */ function getAgencies (callback) { var out = {};