Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,83 @@ 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 = {};

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.
Expand Down Expand Up @@ -976,7 +1053,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) {
Expand Down Expand Up @@ -1108,6 +1185,7 @@ function client () {

exports.setActive = setActive;
exports.guessActive = guessActive;
exports.getAgencies = getAgencies;
exports.getAgencyCache = getAgencyCache;
exports.setAgencyCache = setAgencyCache;
exports.cacheAgency = cacheAgency;
Expand Down