diff --git a/index.js b/index.js index aff36d06..91c9cf50 100644 --- a/index.js +++ b/index.js @@ -137,6 +137,26 @@ const createClient = (profile, request = _request) => { }) } + const location = (station) => { + if ('object' === typeof station) station = profile.formatStation(station.id) + else if ('string' === typeof station) station = profile.formatStation(station) + else throw new Error('station must be an object or a string.') + + return request(profile, { + meth: 'LocDetails', + req: { + locL: [station] + } + }) + .then((d) => { + if (!d || !Array.isArray(d.locL) || !d.locL[0]) { + // todo: proper stack trace? + throw new Error('invalid response') + } + return profile.parseLocation(profile, d.locL[0], d.lines) + }) + } + const nearby = (location, opt = {}) => { if ('object' !== typeof location || Array.isArray(location)) { throw new Error('location must be an object.') @@ -248,7 +268,7 @@ const createClient = (profile, request = _request) => { }) } - const client = {departures, journeys, locations, nearby} + const client = {departures, journeys, locations, location, nearby} if (profile.journeyLeg) client.journeyLeg = journeyLeg if (profile.radar) client.radar = radar Object.defineProperty(client, 'profile', {value: profile}) diff --git a/test/db.js b/test/db.js index 8c04637c..b60a4aa7 100644 --- a/test/db.js +++ b/test/db.js @@ -304,3 +304,13 @@ test('locations named Jungfernheide', co(function* (t) { t.end() })) + +test('location', co(function* (t) { + const regensburgHbf = '8000309' + const loc = yield client.location(regensburgHbf) + + assertValidStation(t, loc) + t.equal(loc.id, regensburgHbf) + + t.end() +})) diff --git a/test/oebb.js b/test/oebb.js index d790be34..90a7b350 100644 --- a/test/oebb.js +++ b/test/oebb.js @@ -362,6 +362,16 @@ test('locations named Salzburg', co(function* (t) { t.end() })) +test('location', co(function* (t) { + const grazHbf = '8100173' + const loc = yield client.location(grazHbf) + + assertValidStation(t, loc) + t.equal(loc.id, grazHbf) + + t.end() +})) + test('radar Salzburg', co(function* (t) { const vehicles = yield client.radar(47.827203, 13.001261, 47.773278, 13.07562, { duration: 5 * 60, when diff --git a/test/util.js b/test/util.js index d304a957..d338f9bb 100644 --- a/test/util.js +++ b/test/util.js @@ -6,11 +6,9 @@ const {DateTime} = require('luxon') const isValidWGS84 = require('is-coordinates') const validateFptfWith = (t, item, allowedTypes, name) => { - try { + t.doesNotThrow(() => { validateFptf.recurse(allowedTypes, item, name) - } catch (err) { - t.ifError(err) - } + }) } const assertValidStation = (t, s, coordsOptional = false) => { diff --git a/test/vbb.js b/test/vbb.js index bb271361..95d34545 100644 --- a/test/vbb.js +++ b/test/vbb.js @@ -361,6 +361,15 @@ test('locations', co(function* (t) { t.end() })) +test('location', co(function* (t) { + const loc = yield client.location(spichernstr) + + assertValidStation(t, loc) + t.equal(loc.id, spichernstr) + + t.end() +})) + test('radar', co(function* (t) {