diff --git a/package-lock.json b/package-lock.json index cfffe73..1d710d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "syncromatics-track-api", - "version": "3.35.1-development-1", + "version": "3.36.1-development.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index cd71d88..63893c5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "syncromatics-track-api", - "version": "3.36.0", + "version": "3.36.1", "description": "Library to interact with the Syncromatics Track API", "main": "dist/index.js", "scripts": { diff --git a/src/examples/get_stop_arrivals.test.js b/src/examples/get_stop_arrivals.test.js new file mode 100644 index 0000000..feb5731 --- /dev/null +++ b/src/examples/get_stop_arrivals.test.js @@ -0,0 +1,27 @@ +import chai from 'chai'; +import chaiAsPromised from 'chai-as-promised'; +import fetchMock from 'fetch-mock'; +import Track from '../index'; +import { charlie, stopArrivals as mockStopArrivals } from '../mocks'; + +chai.should(); +chai.use(chaiAsPromised); + +describe('When retrieving arrivals for a stop', () => { + const api = new Track({ autoRenew: false }); + + beforeEach(() => charlie.setUpSuccessfulMock(api.client)); + beforeEach(() => mockStopArrivals.setUpSuccessfulMock(api.client)); + beforeEach(() => fetchMock.catch(503)); + afterEach(fetchMock.restore); + + it('should get a list of arrivals for the stop', () => { + api.logIn({ username: 'charlie@example.com', password: 'securepassword' }); + + const arrivalsPromise = api.customer('SYNC').stop(1) + .arrivals() + .then(arrivals => arrivals); // Do things with list of arrivals + + return arrivalsPromise; + }); +}); diff --git a/src/mocks/index.js b/src/mocks/index.js index 719259f..48397ed 100644 --- a/src/mocks/index.js +++ b/src/mocks/index.js @@ -32,6 +32,7 @@ export { default as servicePackages } from './servicePackages'; export { default as services } from './services'; export { default as signs } from './signs'; export { default as stops } from './stops'; +export { default as stopArrivals } from './stopArrivals'; export { default as tags } from './tags'; export { default as trips } from './trips'; export { default as twitter } from './twitter'; diff --git a/src/mocks/stopArrivals.js b/src/mocks/stopArrivals.js index 4d35f6b..83592ae 100644 --- a/src/mocks/stopArrivals.js +++ b/src/mocks/stopArrivals.js @@ -11,20 +11,23 @@ const stopArrivals = { }, list: [ { - secondsToArrival: 60, - as_of: '2017-01-01T00:00:00.000-07:00', - source: 'realtime', + arrive_variance: 30, + as_of: '2017-01-01T00:00:15.000-07:00', pattern: { href: '/1/SYNC/patterns/1' }, route: { href: '/1/SYNC/routes/1' }, + scheduled_arrival: '2017-01-01T00:00:45.000-07:00', + seconds_to_arrival: 60, + source: 'realtime', stop: { href: '/1/SYNC/stops/1' }, + trip: { href: '/1/SYNC/trips/1' }, vehicle: { href: '/1/SYNC/vehicles/1' }, }, { - secondsToArrival: 360, - as_of: '2017-01-01T00:00:00.000-07:00', - source: 'realtime', + as_of: '2017-01-01T00:00:15.000-07:00', pattern: { href: '/1/SYNC/patterns/1' }, route: { href: '/1/SYNC/routes/1' }, + seconds_to_arrival: 360, + source: 'realtime', stop: { href: '/1/SYNC/stops/1' }, vehicle: { href: '/1/SYNC/vehicles/2' }, }, diff --git a/src/resources/Stop.js b/src/resources/Stop.js index 8c998d7..de47f96 100644 --- a/src/resources/Stop.js +++ b/src/resources/Stop.js @@ -63,6 +63,25 @@ class Stop extends Resource { .then(stop => new Stop(this.client, this, stop)); } + /** + * Fetches upcoming arrival predictions for this stop. + * For more information, read official Track-API documentation. + * @param {Object} [options] Options for the request + * @param {number} [options.count] Optional maximum number of arrivals to return. + * @returns {Promise} If successful, an array of arrival prediction objects for this stop + */ + arrivals(options = {}) { + const { count } = options; + let url = `${this.href}/arrivals`; + + if (count) { + url += `?count=${count}`; + } + + return this.client.get(url) + .then(response => response.json()); + } + /** * Saves data for a stop via the client * @returns {Promise} If successful, returns a stop with the id included diff --git a/src/resources/Stop.test.js b/src/resources/Stop.test.js index 288ed53..1a49723 100644 --- a/src/resources/Stop.test.js +++ b/src/resources/Stop.test.js @@ -3,7 +3,7 @@ import chaiAsPromised from 'chai-as-promised'; import fetchMock from 'fetch-mock'; import Client from '../Client'; import Stop from './Stop'; -import { stops as mockStops } from '../mocks'; +import { stops as mockStops, stopArrivals as mockStopArrivals } from '../mocks'; chai.should(); chai.use(chaiAsPromised); @@ -84,3 +84,19 @@ describe('When updating a stop', () => { it('should set the href', () => promise.then(v => v.href).should.eventually.equal('/1/SYNC/stops/1')); it('should set the name', () => promise.then(v => v.name).should.eventually.equal(updateValue)); }); + +describe('When fetching arrivals for a stop', () => { + const client = new Client(); + + beforeEach(() => mockStopArrivals.setUpSuccessfulMock(client)); + beforeEach(() => fetchMock.catch(503)); + afterEach(fetchMock.restore); + + let promise; + beforeEach(() => { + promise = new Stop(client, Stop.makeHref('SYNC', 1)).arrivals(); + }); + + it('should resolve the promise', () => promise.should.be.fulfilled); + it('should return the list of arrivals', () => promise.should.eventually.deep.equal(mockStopArrivals.list)); +});