diff --git a/README.md b/README.md index d018560..0a5a109 100644 --- a/README.md +++ b/README.md @@ -72,12 +72,12 @@ mta.status('subway').then(function (result) { The API route this method hits is updated by the MTA every 60 seconds. ### Get real-time subway schedule data -Only available for the following routes: 1, 2, 3, 4, 5, 6, S, L, and Staten Island Railway (http://datamine.mta.info/list-of-feeds). +Only available for the routes found in this [list](http://datamine.mta.info/list-of-feeds). -Given a single subway stop id (or an array of stop ids), it gives schedule data for both northbound and southbound trains. +Given a single subway stop id (or an array of stop ids) and an optional feedId, it gives schedule data for both northbound and southbound trains. ```Javascript -mta.schedule(635).then(function (result) { +mta.schedule(635, 1).then(function (result) { console.log(result); }); ``` @@ -88,10 +88,8 @@ The API route this method hits is updated by the MTA every 30 seconds. See [test cases](https://github.com/aamaliaa/mta/blob/master/test/mta.js) for more examples. -Replace `'your-api-key'` in `test/config.js` with your own MTA API key then run: - ``` -npm test +MTA_API_KEY='your-api-key-here' npm test ``` ## To do diff --git a/lib/mta.js b/lib/mta.js index d2c8261..aad5cdc 100644 --- a/lib/mta.js +++ b/lib/mta.js @@ -21,7 +21,7 @@ var Mta = module.exports = function (options) { this.options = options || {}; _.extend({ - feed_id: 0 + feed_id: 1, }, this.options); if (this.options.key === 'your-api-key') { @@ -84,15 +84,22 @@ Mta.prototype.status = function (service) { /** * Gets MTA schedule status * @param {String|Array} stopId + * @param {String} feedId optional * @return {Object} schedule */ -Mta.prototype.schedule = function (stopId) { +Mta.prototype.schedule = function (stopId, feedId) { var schedule = {}; var results = false; var stopIds; - var feedUrl = this.urls.gtfs + qs.stringify(_.pick(this.options, [ 'feed_id', 'key' ])); - var msg, direction, obj; + var direction, obj; + var options = _.pick(this.options, [ 'feed_id', 'key' ]); + + if (feedId) { + options.feed_id = feedId; + } + + var feedUrl = this.urls.gtfs + qs.stringify(options); if (!this.options.key) { throw new Error('schedule method requires MTA API key'); @@ -105,10 +112,10 @@ Mta.prototype.schedule = function (stopId) { if (_.isArray(stopId)) { stopIds = stopId; - } else if (_.isNumber(stopId)) { + } else if (_.isNumber(stopId) || _.isString(stopId)) { stopIds = [ stopId ]; } else { - throw new Error('invalid stop id(s)') + throw new Error('invalid stop id(s)'); } _.each(stopIds, function (stop) { diff --git a/test/config.js b/test/config.js deleted file mode 100644 index 5ee63db..0000000 --- a/test/config.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - key: 'your-api-key' -}; diff --git a/test/mta.js b/test/mta.js index d0ef636..6f11b50 100644 --- a/test/mta.js +++ b/test/mta.js @@ -1,7 +1,10 @@ var should = require('should'); -var config = require('./config'); var Mta = require('../lib/mta'); +var config = { + key: process.env.MTA_API_KEY, +}; + describe('MTA', function () { var mta, serviceType, @@ -68,12 +71,12 @@ describe('MTA', function () { }); }); - it('api key is set in test/config.js', function (done) { - config.key.should.not.equal('your-api-key'); + it('MTA_API_KEY should be set', function (done) { + config.key.should.not.equal(''); done(); }); - it('should get schedule info for 1 MTA subway station', function () { + it('should get schedule info for 1 MTA subway station (number input)', function () { return mta.schedule(stopId) .then(function (result) { result.should.have.property('schedule'); @@ -82,6 +85,24 @@ describe('MTA', function () { }); }); + it('should get schedule info for 1 MTA subway station (string input)', function () { + return mta.schedule('128') + .then(function (result) { + result.should.have.property('schedule'); + result.should.have.property('updatedOn'); + result.schedule['128'].should.exist; + }); + }); + + it('should get schedule info for a station with a different feed_id', function () { + return mta.schedule('A15', 26) + .then(function (result) { + result.should.have.property('schedule'); + result.should.have.property('updatedOn'); + result.schedule['A15'].should.exist; + }); + }); + it('should get schedule info for multiple MTA subway stations', function () { return mta.schedule(stopIds) .then(function (result) {