mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
location method: implementation
This commit is contained in:
parent
39636f12bf
commit
b79c2387dd
5 changed files with 52 additions and 5 deletions
22
index.js
22
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 = {}) => {
|
const nearby = (location, opt = {}) => {
|
||||||
if ('object' !== typeof location || Array.isArray(location)) {
|
if ('object' !== typeof location || Array.isArray(location)) {
|
||||||
throw new Error('location must be an object.')
|
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.journeyLeg) client.journeyLeg = journeyLeg
|
||||||
if (profile.radar) client.radar = radar
|
if (profile.radar) client.radar = radar
|
||||||
Object.defineProperty(client, 'profile', {value: profile})
|
Object.defineProperty(client, 'profile', {value: profile})
|
||||||
|
|
10
test/db.js
10
test/db.js
|
@ -304,3 +304,13 @@ test('locations named Jungfernheide', co(function* (t) {
|
||||||
|
|
||||||
t.end()
|
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()
|
||||||
|
}))
|
||||||
|
|
10
test/oebb.js
10
test/oebb.js
|
@ -362,6 +362,16 @@ test('locations named Salzburg', co(function* (t) {
|
||||||
t.end()
|
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) {
|
test('radar Salzburg', co(function* (t) {
|
||||||
const vehicles = yield client.radar(47.827203, 13.001261, 47.773278, 13.07562, {
|
const vehicles = yield client.radar(47.827203, 13.001261, 47.773278, 13.07562, {
|
||||||
duration: 5 * 60, when
|
duration: 5 * 60, when
|
||||||
|
|
|
@ -6,11 +6,9 @@ const {DateTime} = require('luxon')
|
||||||
const isValidWGS84 = require('is-coordinates')
|
const isValidWGS84 = require('is-coordinates')
|
||||||
|
|
||||||
const validateFptfWith = (t, item, allowedTypes, name) => {
|
const validateFptfWith = (t, item, allowedTypes, name) => {
|
||||||
try {
|
t.doesNotThrow(() => {
|
||||||
validateFptf.recurse(allowedTypes, item, name)
|
validateFptf.recurse(allowedTypes, item, name)
|
||||||
} catch (err) {
|
})
|
||||||
t.ifError(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const assertValidStation = (t, s, coordsOptional = false) => {
|
const assertValidStation = (t, s, coordsOptional = false) => {
|
||||||
|
|
|
@ -361,6 +361,15 @@ test('locations', co(function* (t) {
|
||||||
t.end()
|
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) {
|
test('radar', co(function* (t) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue