location method: implementation

This commit is contained in:
Jannis R 2018-01-26 17:08:07 +01:00
parent 39636f12bf
commit b79c2387dd
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
5 changed files with 52 additions and 5 deletions

View file

@ -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})

View file

@ -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()
}))

View file

@ -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

View file

@ -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) => {

View file

@ -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) {