mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
journeys/refreshJourney/journeysFromTrip: realtimeDataFrom -> realtimeDataUpdatedAt 💥✅📝
This commit is contained in:
parent
c4470ca962
commit
751ae21d18
6 changed files with 46 additions and 12 deletions
|
@ -39,7 +39,12 @@ const leg = journey.legs.find(l => l.line.product === 'nationalExpress')
|
||||||
const previousStopover = leg.stopovers.find(st => st.departure && new Date(st.departure) < Date.now())
|
const previousStopover = leg.stopovers.find(st => st.departure && new Date(st.departure) < Date.now())
|
||||||
|
|
||||||
// find journeys from the ICE train to Köln Hbf
|
// find journeys from the ICE train to Köln Hbf
|
||||||
const journeys = await client.journeysFromTrip(leg.id, previousStopover, kölnHbf)
|
const {
|
||||||
|
journeys,
|
||||||
|
realtimeDataUpdatedAt,
|
||||||
|
} = await client.journeysFromTrip(leg.id, previousStopover, kölnHbf)
|
||||||
```
|
```
|
||||||
|
|
||||||
`journeys` will be an array of [FPTF `journey`s](https://github.com/public-transport/friendly-public-transport-format/blob/3bd36faa721e85d9f5ca58fb0f38cdbedb87bbca/spec/readme.md#journey), as documented in [`journeys()`](journeys.md).
|
`journeys` is an array of [FPTF `journey`s](https://github.com/public-transport/friendly-public-transport-format/blob/3bd36faa721e85d9f5ca58fb0f38cdbedb87bbca/spec/readme.md#journey), as documented in [`journeys()`](journeys.md).
|
||||||
|
|
||||||
|
`realtimeDataUpdatedAt` is a UNIX timestamp reflecting the latest moment when (at least some of) the response's realtime data have been updated.
|
||||||
|
|
|
@ -97,7 +97,12 @@ await client.journeys('900000003201', '900000100008', {
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
`journeys()` will resolve with an object with the `journeys` & `earlierRef`/`laterRef` fields. It might look like this:
|
`journeys()` will resolve with an object with the following fields:
|
||||||
|
- `journeys`
|
||||||
|
- `earlierRef`/`laterRef` – pass them as `opt.earlierThan`/`opt.laterThan` into another `journeys()` call to retrieve the next "page" of journeys
|
||||||
|
- `realtimeDataUpdatedAt` – a UNIX timestamp reflecting the latest moment when (at least some of) the response's realtime data have been updated
|
||||||
|
|
||||||
|
This object might look like this:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
{
|
{
|
||||||
|
@ -267,6 +272,7 @@ await client.journeys('900000003201', '900000100008', {
|
||||||
} ],
|
} ],
|
||||||
earlierRef: '…', // use with the `earlierThan` option
|
earlierRef: '…', // use with the `earlierThan` option
|
||||||
laterRef: '…' // use with the `laterThan` option
|
laterRef: '…' // use with the `laterThan` option
|
||||||
|
realtimeDataUpdatedAt: 1531259400, // 2018-07-10T23:50:00+02
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,10 @@ const client = createClient(vbbProfile)
|
||||||
const {journeys} = await client.journeys('900000003201', '900000100008', {results: 1})
|
const {journeys} = await client.journeys('900000003201', '900000100008', {results: 1})
|
||||||
|
|
||||||
// later, fetch up-to-date info on the journey
|
// later, fetch up-to-date info on the journey
|
||||||
await client.refreshJourney(journeys[0].refreshToken, {stopovers: true, remarks: true})
|
const journey = await client.refreshJourney(journeys[0].refreshToken, {stopovers: true, remarks: true})
|
||||||
|
const {realtimeDataUpdatedAt} = journey
|
||||||
```
|
```
|
||||||
|
|
||||||
`refreshJourney()` will return a *single* [*Friendly Public Transport Format* v2 draft](https://github.com/public-transport/friendly-public-transport-format/blob/3bd36faa721e85d9f5ca58fb0f38cdbedb87bbca/spec/readme.md) `journey`, in the same format as with `journeys()`.
|
`journey` is a *single* [*Friendly Public Transport Format* v2 draft](https://github.com/public-transport/friendly-public-transport-format/blob/3bd36faa721e85d9f5ca58fb0f38cdbedb87bbca/spec/readme.md) `journey`, in the same format as returned by [`journeys()`](journeys.md).
|
||||||
|
|
||||||
|
`realtimeDataUpdatedAt` is a UNIX timestamp reflecting the latest moment when (at least some of) the response's realtime data have been updated.
|
||||||
|
|
20
index.js
20
index.js
|
@ -231,8 +231,9 @@ const createClient = (profile, userAgent, opt = {}) => {
|
||||||
earlierRef: res.outCtxScrB,
|
earlierRef: res.outCtxScrB,
|
||||||
laterRef: res.outCtxScrF,
|
laterRef: res.outCtxScrF,
|
||||||
journeys,
|
journeys,
|
||||||
// todo [breaking]: rename to realtimeDataUpdatedAt
|
realtimeDataUpdatedAt: res.planrtTS && res.planrtTS !== '0'
|
||||||
realtimeDataFrom: res.planrtTS ? parseInt(res.planrtTS) : null,
|
? parseInt(res.planrtTS)
|
||||||
|
: null,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -261,8 +262,9 @@ const createClient = (profile, userAgent, opt = {}) => {
|
||||||
const ctx = {profile, opt, common, res}
|
const ctx = {profile, opt, common, res}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// todo [breaking]: rename to realtimeDataUpdatedAt
|
realtimeDataUpdatedAt: res.planrtTS && res.planrtTS !== '0'
|
||||||
realtimeDataFrom: res.planrtTS ? parseInt(res.planrtTS) : null,
|
? parseInt(res.planrtTS)
|
||||||
|
: null,
|
||||||
...profile.parseJourney(ctx, res.outConL[0])
|
...profile.parseJourney(ctx, res.outConL[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -368,8 +370,7 @@ const createClient = (profile, userAgent, opt = {}) => {
|
||||||
if (!Array.isArray(res.outConL)) return []
|
if (!Array.isArray(res.outConL)) return []
|
||||||
|
|
||||||
const ctx = {profile, opt, common, res}
|
const ctx = {profile, opt, common, res}
|
||||||
// todo [breaking]: return object with realtimeDataUpdatedAt
|
const journeys = res.outConL
|
||||||
return res.outConL
|
|
||||||
.map(rawJourney => profile.parseJourney(ctx, rawJourney))
|
.map(rawJourney => profile.parseJourney(ctx, rawJourney))
|
||||||
.map((journey) => {
|
.map((journey) => {
|
||||||
// For the first (transit) leg, HAFAS sometimes returns *all* past
|
// For the first (transit) leg, HAFAS sometimes returns *all* past
|
||||||
|
@ -388,6 +389,13 @@ const createClient = (profile, userAgent, opt = {}) => {
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
journeys,
|
||||||
|
realtimeDataUpdatedAt: res.planrtTS && res.planrtTS !== '0'
|
||||||
|
? parseInt(res.planrtTS)
|
||||||
|
: null,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const locations = async (query, opt = {}) => {
|
const locations = async (query, opt = {}) => {
|
||||||
|
|
|
@ -15,16 +15,17 @@ const testRefreshJourney = async (cfg) => {
|
||||||
test: t,
|
test: t,
|
||||||
fetchJourneys,
|
fetchJourneys,
|
||||||
refreshJourney,
|
refreshJourney,
|
||||||
|
validate,
|
||||||
fromId,
|
fromId,
|
||||||
toId,
|
toId,
|
||||||
when,
|
when,
|
||||||
// todo: validate
|
|
||||||
} = cfg
|
} = cfg
|
||||||
|
|
||||||
const modelRes = await fetchJourneys(fromId, toId, {
|
const modelRes = await fetchJourneys(fromId, toId, {
|
||||||
results: 1, departure: when,
|
results: 1, departure: when,
|
||||||
stopovers: false
|
stopovers: false
|
||||||
})
|
})
|
||||||
|
validate(t, modelRes, 'journeysResult', 'modelRes')
|
||||||
const [model] = modelRes.journeys
|
const [model] = modelRes.journeys
|
||||||
|
|
||||||
// todo: move to journeys validator?
|
// todo: move to journeys validator?
|
||||||
|
@ -34,6 +35,8 @@ const testRefreshJourney = async (cfg) => {
|
||||||
const refreshed = await refreshJourney(model.refreshToken, {
|
const refreshed = await refreshJourney(model.refreshToken, {
|
||||||
stopovers: false
|
stopovers: false
|
||||||
})
|
})
|
||||||
|
validate(t, refreshed, 'refreshJourneyResult', 'refreshed')
|
||||||
|
|
||||||
t.same(simplify(refreshed), simplify(model))
|
t.same(simplify(refreshed), simplify(model))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -428,6 +428,14 @@ const validateJourneysResult = (val, res, name = 'journeysResult') => {
|
||||||
a.ok(isObj(res), name + ' must be an object')
|
a.ok(isObj(res), name + ' must be an object')
|
||||||
// todo: `earlierRef`, `laterRef`
|
// todo: `earlierRef`, `laterRef`
|
||||||
val.journeys(val, res.journeys, name + '.journeys')
|
val.journeys(val, res.journeys, name + '.journeys')
|
||||||
|
|
||||||
|
val.realtimeDataUpdatedAt(val, res.realtimeDataUpdatedAt, name + '.realtimeDataUpdatedAt')
|
||||||
|
}
|
||||||
|
|
||||||
|
const validateRefreshJourneyResult = (val, res, name = 'refreshJourneyResult') => {
|
||||||
|
val.journey(val, res, name + '.journey')
|
||||||
|
|
||||||
|
val.realtimeDataUpdatedAt(val, res.realtimeDataUpdatedAt, name + '.realtimeDataUpdatedAt')
|
||||||
}
|
}
|
||||||
|
|
||||||
const validateTrip = (val, trip, name = 'trip') => {
|
const validateTrip = (val, trip, name = 'trip') => {
|
||||||
|
@ -619,6 +627,7 @@ module.exports = {
|
||||||
journey: () => validateJourney,
|
journey: () => validateJourney,
|
||||||
journeys: () => validateJourneys,
|
journeys: () => validateJourneys,
|
||||||
journeysResult: () => validateJourneysResult,
|
journeysResult: () => validateJourneysResult,
|
||||||
|
refreshJourneyResult: () => validateRefreshJourneyResult,
|
||||||
trip: () => validateTrip,
|
trip: () => validateTrip,
|
||||||
arrival: createValidateArrival,
|
arrival: createValidateArrival,
|
||||||
departure: createValidateDeparture,
|
departure: createValidateDeparture,
|
||||||
|
|
Loading…
Add table
Reference in a new issue