bartjs
Version:
A client for interacting with the Bay Area Rapid Transit (BART) API
127 lines (98 loc) • 3.41 kB
JavaScript
// Dependencies
const request = require('request');
const qs = require('query-string');
const parser = require('xml2json');
const extend = require('deep-extend');
const turf = require('@turf/turf');
// BART Station GeoJSON Object
const stations = require('../stations');
// Constructor to interface with the API
function Bart(options) {
if (!(this instanceof Bart)) { return new Bart(options) }
this.options = extend({
apiKey: this.apiKey || 'MW9S-E7SL-26DU-VV8V',
apiBase: 'http://api.bart.gov/api/',
stations: stations}, options);
}
// TODO
// * Allow passing options to near method
// to customize search radius
// * Write tests for endpoints
// * Write tests for near method
Bart.prototype._buildEndpoint = function(endpoint) {
const endpoints = {
etd: 'etd.aspx?',
stn: 'stn.aspx?',
bsa: 'bsa.aspx?',
route: 'route.aspx?',
sched: 'sched.aspx?',
version: 'version.aspx?',
};
return this.options.apiBase + endpoints[endpoint]
}
Bart.prototype._buildQuery = function(params) {
return qs.stringify(extend(params, {key: this.options.apiKey})
)}
Bart.prototype._buildRequest = function(endpoint, query) {
return this._buildEndpoint(endpoint) + this._buildQuery(query)
}
// Base method for all requests -- builds a request URL
// from the endpoint and query parameters, then issues a
// get request and returns the resposne body as JSON.
Bart.prototype._makeCall = function(endpoint, query) {
return new Promise(function(resolve, reject) {
const url = this._buildRequest(endpoint, query);
request(url, function(error, response, body) {
if (error) {
return reject(err);
}
resolve(parser.toJson(body, {object: true}).root);
});
}.bind(this));
}
// Get estimated departure times
// Valid commands: etd, help
Bart.prototype.etd = function(query) {
return this._makeCall('etd', query);
}
// Get station location and information
// Valid commands: help, stninfo, stnaccess, stns
Bart.prototype.stationInfo = function(query) {
return this._makeCall('stn', query);
}
// Get current BART system safety advisories.
// Valid commands: bsa, count, elev, help
Bart.prototype.advisories = function(query) {
return this._makeCall('bsa', query);
}
// Get information about a particular route.
// Valid commands: help, routeinfo, routes
Bart.prototype.routeInfo = function(query) {
return this._makeCall('route', query);
}
// Get schedule information.
// Valid commands: arrive, depart, fare, help,
// holiday, routesched, scheds, special, stnsched
Bart.prototype.scheduleInfo = function(query) {
return this._makeCall('sched', query);
}
// Check the current version of the BART API
// Valid commands: version
Bart.prototype.apiVersion = function(query) {
return this._makeCall('version')
}
// Obtain a GeoJSON features object containing
// nearby BART stations
Bart.prototype.near = function(coordinates) {
const circle = turf.circle(turf.point(coordinates), 0.0155, 10, 'degrees');
const pointCollection = turf.featureCollection([circle]);
return turf.within(this.options.stations, pointCollection);
}
// Obtain a GeoJSON feature object containing
// the nearest BART station to the inputted lat, lon
// coordinate pair inputted as an array.
Bart.prototype.nearest = function(coordinates) {
return turf.nearest(coordinates, this.options.stations);
}
module.exports = Bart;
;