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

Commit a1754c1

Browse files
committed
adds option to configure feed_id on schedule call
1 parent 0902011 commit a1754c1

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ mta.status('subway').then(function (result) {
7272
The API route this method hits is updated by the MTA every 60 seconds.
7373

7474
### Get real-time subway schedule data
75-
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).
75+
Only available for the routes found in this [list](http://datamine.mta.info/list-of-feeds).
7676

77-
Given a single subway stop id (or an array of stop ids), it gives schedule data for both northbound and southbound trains.
77+
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.
7878

7979
```Javascript
80-
mta.schedule(635).then(function (result) {
80+
mta.schedule(635, 1).then(function (result) {
8181
console.log(result);
8282
});
8383
```
@@ -88,10 +88,8 @@ The API route this method hits is updated by the MTA every 30 seconds.
8888

8989
See [test cases](https://github.com/aamaliaa/mta/blob/master/test/mta.js) for more examples.
9090

91-
Replace `'your-api-key'` in `test/config.js` with your own MTA API key then run:
92-
9391
```
94-
npm test
92+
MTA_API_KEY='your-api-key-here' npm test
9593
```
9694

9795
## To do

lib/mta.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var Mta = module.exports = function (options) {
2121
this.options = options || {};
2222

2323
_.extend({
24-
feed_id: 0
24+
feed_id: 1,
2525
}, this.options);
2626

2727
if (this.options.key === 'your-api-key') {
@@ -84,15 +84,22 @@ Mta.prototype.status = function (service) {
8484
/**
8585
* Gets MTA schedule status
8686
* @param {String|Array} stopId
87+
* @param {String} feedId optional
8788
* @return {Object} schedule
8889
*/
89-
Mta.prototype.schedule = function (stopId) {
90+
Mta.prototype.schedule = function (stopId, feedId) {
9091

9192
var schedule = {};
9293
var results = false;
9394
var stopIds;
94-
var feedUrl = this.urls.gtfs + qs.stringify(_.pick(this.options, [ 'feed_id', 'key' ]));
95-
var msg, direction, obj;
95+
var direction, obj;
96+
var options = _.pick(this.options, [ 'feed_id', 'key' ]);
97+
98+
if (feedId) {
99+
options.feed_id = feedId;
100+
}
101+
102+
var feedUrl = this.urls.gtfs + qs.stringify(options);
96103

97104
if (!this.options.key) {
98105
throw new Error('schedule method requires MTA API key');
@@ -105,10 +112,10 @@ Mta.prototype.schedule = function (stopId) {
105112

106113
if (_.isArray(stopId)) {
107114
stopIds = stopId;
108-
} else if (_.isNumber(stopId)) {
115+
} else if (_.isNumber(stopId) || _.isString(stopId)) {
109116
stopIds = [ stopId ];
110117
} else {
111-
throw new Error('invalid stop id(s)')
118+
throw new Error('invalid stop id(s)');
112119
}
113120

114121
_.each(stopIds, function (stop) {

test/config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/mta.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
var should = require('should');
2-
var config = require('./config');
32
var Mta = require('../lib/mta');
43

4+
var config = {
5+
key: process.env.MTA_API_KEY,
6+
};
7+
58
describe('MTA', function () {
69
var mta,
710
serviceType,
@@ -68,12 +71,12 @@ describe('MTA', function () {
6871
});
6972
});
7073

71-
it('api key is set in test/config.js', function (done) {
72-
config.key.should.not.equal('your-api-key');
74+
it('MTA_API_KEY should be set', function (done) {
75+
config.key.should.not.equal('');
7376
done();
7477
});
7578

76-
it('should get schedule info for 1 MTA subway station', function () {
79+
it('should get schedule info for 1 MTA subway station (number input)', function () {
7780
return mta.schedule(stopId)
7881
.then(function (result) {
7982
result.should.have.property('schedule');
@@ -82,6 +85,24 @@ describe('MTA', function () {
8285
});
8386
});
8487

88+
it('should get schedule info for 1 MTA subway station (string input)', function () {
89+
return mta.schedule('128')
90+
.then(function (result) {
91+
result.should.have.property('schedule');
92+
result.should.have.property('updatedOn');
93+
result.schedule['128'].should.exist;
94+
});
95+
});
96+
97+
it('should get schedule info for a station with a different feed_id', function () {
98+
return mta.schedule('A15', 26)
99+
.then(function (result) {
100+
result.should.have.property('schedule');
101+
result.should.have.property('updatedOn');
102+
result.schedule['A15'].should.exist;
103+
});
104+
});
105+
85106
it('should get schedule info for multiple MTA subway stations', function () {
86107
return mta.schedule(stopIds)
87108
.then(function (result) {

0 commit comments

Comments
 (0)