Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Commit

Permalink
adds option to configure feed_id on schedule call
Browse files Browse the repository at this point in the history
  • Loading branch information
aamaliaa committed Aug 13, 2017
1 parent 0902011 commit a1754c1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
10 changes: 4 additions & 6 deletions README.md
Expand Up @@ -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);
});
```
Expand All @@ -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
Expand Down
19 changes: 13 additions & 6 deletions lib/mta.js
Expand Up @@ -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') {
Expand Down Expand Up @@ -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');
Expand All @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions test/config.js

This file was deleted.

29 changes: 25 additions & 4 deletions 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,
Expand Down Expand Up @@ -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');
Expand All @@ -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) {
Expand Down

0 comments on commit a1754c1

Please sign in to comment.