mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
add lines()
This commit is contained in:
parent
7685d5afeb
commit
9d8260bf5f
6 changed files with 109 additions and 0 deletions
62
docs/lines.md
Normal file
62
docs/lines.md
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
# `lines([opt])`
|
||||||
|
|
||||||
|
**Fetches all lines known to the HAFAS endpoint**, e.g. warnings about disruptions, planned construction work, and general notices about the operating situation.
|
||||||
|
|
||||||
|
## Example
|
||||||
|
|
||||||
|
As an example, we're going to use the [SVV profile](../p/svv):
|
||||||
|
|
||||||
|
```js
|
||||||
|
const createClient = require('hafas-client')
|
||||||
|
const svvProfile = require('hafas-client/p/svv')
|
||||||
|
|
||||||
|
const client = createClient(svvProfile, 'my-awesome-program')
|
||||||
|
|
||||||
|
console.log(await client.lines('S1'))
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id": "obb-1-S1-V-j20-1",
|
||||||
|
"type": "line",
|
||||||
|
"name": "S1",
|
||||||
|
"public": true,
|
||||||
|
"mode": "train",
|
||||||
|
"product": "bahn-s-bahn",
|
||||||
|
"operator": {
|
||||||
|
"type": "operator",
|
||||||
|
"id": "montafonerbahn-ag",
|
||||||
|
"name": "Montafonerbahn AG"
|
||||||
|
},
|
||||||
|
"directions": [
|
||||||
|
"Bludenz Bahnhof",
|
||||||
|
"Bregenz Hafen Bahnhof",
|
||||||
|
"Lindau Hbf",
|
||||||
|
"Bregenz Bahnhof",
|
||||||
|
"Schruns Bahnhof",
|
||||||
|
"Lochau Bahnhof"
|
||||||
|
],
|
||||||
|
},
|
||||||
|
// …
|
||||||
|
{
|
||||||
|
"id": "svv-42-50-j20-2",
|
||||||
|
"type": "line",
|
||||||
|
"name": "S1",
|
||||||
|
"public": true,
|
||||||
|
"mode": "train",
|
||||||
|
"product": "bahn-s-bahn",
|
||||||
|
"operator": {
|
||||||
|
"type": "operator",
|
||||||
|
"id": "salzburg-ag-salzburger-lokalbahn",
|
||||||
|
"name": "Salzburg AG - Salzburger Lokalbahn"
|
||||||
|
},
|
||||||
|
"directions": [
|
||||||
|
"Lamprechtshausen Bahnhof",
|
||||||
|
"Salzburg Hauptbahnhof",
|
||||||
|
"Acharting S-Bahn",
|
||||||
|
"Weitwörth-Nussdorf Bahnhof"
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
```
|
|
@ -12,6 +12,7 @@
|
||||||
- [`radar(north, west, south, east, [opt])`](radar.md) – find all vehicles currently in a certain area
|
- [`radar(north, west, south, east, [opt])`](radar.md) – find all vehicles currently in a certain area
|
||||||
- [`reachableFrom(address, [opt])`](reachable-from.md) – get all stations reachable from an address within `n` minutes
|
- [`reachableFrom(address, [opt])`](reachable-from.md) – get all stations reachable from an address within `n` minutes
|
||||||
- [`remarks([opt])`](remarks.md) – get all remarks
|
- [`remarks([opt])`](remarks.md) – get all remarks
|
||||||
|
- [`lines(query, [opt])`](lines.md) – get all lines matching a name
|
||||||
- [`serverInfo([opt])`](server-info.md) – fetch meta information from HAFAS
|
- [`serverInfo([opt])`](server-info.md) – fetch meta information from HAFAS
|
||||||
|
|
||||||
## Migrating from an old `hafas-client` version
|
## Migrating from an old `hafas-client` version
|
||||||
|
|
12
format/lines-req.js
Normal file
12
format/lines-req.js
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const formatLinesReq = (ctx, query) => {
|
||||||
|
return {
|
||||||
|
meth: 'LineMatch',
|
||||||
|
req: {
|
||||||
|
input: query,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = formatLinesReq
|
30
index.js
30
index.js
|
@ -3,6 +3,7 @@
|
||||||
const isObj = require('lodash/isObject')
|
const isObj = require('lodash/isObject')
|
||||||
const sortBy = require('lodash/sortBy')
|
const sortBy = require('lodash/sortBy')
|
||||||
const pRetry = require('p-retry')
|
const pRetry = require('p-retry')
|
||||||
|
const omit = require('lodash/omit')
|
||||||
|
|
||||||
const defaultProfile = require('./lib/default-profile')
|
const defaultProfile = require('./lib/default-profile')
|
||||||
const validateProfile = require('./lib/validate-profile')
|
const validateProfile = require('./lib/validate-profile')
|
||||||
|
@ -502,6 +503,34 @@ const createClient = (profile, userAgent, opt = {}) => {
|
||||||
return res.msgL.map(w => profile.parseWarning(ctx, w))
|
return res.msgL.map(w => profile.parseWarning(ctx, w))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const lines = async (query, opt = {}) => {
|
||||||
|
if (!isNonEmptyString(query)) {
|
||||||
|
throw new TypeError('query must be a non-empty string.')
|
||||||
|
}
|
||||||
|
|
||||||
|
const req = profile.formatLinesReq({profile, opt}, query)
|
||||||
|
const {
|
||||||
|
res, common,
|
||||||
|
} = await profile.request({profile, opt}, userAgent, req)
|
||||||
|
|
||||||
|
if (!Array.isArray(res.lineL)) return []
|
||||||
|
const ctx = {profile, opt, common, res}
|
||||||
|
return res.lineL.map(l => {
|
||||||
|
const parseDirRef = i => (res.common.dirL[i] || {}).txt || null
|
||||||
|
return {
|
||||||
|
...omit(l.line, ['id', 'fahrtNr']),
|
||||||
|
id: l.lineId,
|
||||||
|
// todo: what is locX?
|
||||||
|
directions: Array.isArray(l.dirRefL)
|
||||||
|
? l.dirRefL.map(parseDirRef)
|
||||||
|
: null,
|
||||||
|
trips: Array.isArray(l.jnyL)
|
||||||
|
? l.jnyL.map(t => profile.parseTrip(ctx, t))
|
||||||
|
: null,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const serverInfo = async (opt = {}) => {
|
const serverInfo = async (opt = {}) => {
|
||||||
const {res, common} = await profile.request({profile, opt}, userAgent, {
|
const {res, common} = await profile.request({profile, opt}, userAgent, {
|
||||||
meth: 'ServerInfo',
|
meth: 'ServerInfo',
|
||||||
|
@ -528,6 +557,7 @@ const createClient = (profile, userAgent, opt = {}) => {
|
||||||
locations,
|
locations,
|
||||||
stop,
|
stop,
|
||||||
nearby,
|
nearby,
|
||||||
|
lines,
|
||||||
serverInfo,
|
serverInfo,
|
||||||
}
|
}
|
||||||
if (profile.trip) client.trip = trip
|
if (profile.trip) client.trip = trip
|
||||||
|
|
|
@ -11,6 +11,7 @@ const formatRadarReq = require('../format/radar-req')
|
||||||
const formatReachableFromReq = require('../format/reachable-from-req')
|
const formatReachableFromReq = require('../format/reachable-from-req')
|
||||||
const formatRefreshJourneyReq = require('../format/refresh-journey-req')
|
const formatRefreshJourneyReq = require('../format/refresh-journey-req')
|
||||||
const formatRemarksReq = require('../format/remarks-req')
|
const formatRemarksReq = require('../format/remarks-req')
|
||||||
|
const formatLinesReq = require('../format/lines-req')
|
||||||
|
|
||||||
const parseDateTime = require('../parse/date-time')
|
const parseDateTime = require('../parse/date-time')
|
||||||
const parsePlatform = require('../parse/platform')
|
const parsePlatform = require('../parse/platform')
|
||||||
|
@ -64,6 +65,7 @@ const defaultProfile = {
|
||||||
formatReachableFromReq,
|
formatReachableFromReq,
|
||||||
formatRefreshJourneyReq,
|
formatRefreshJourneyReq,
|
||||||
formatRemarksReq,
|
formatRemarksReq,
|
||||||
|
formatLinesReq,
|
||||||
transformJourneysQuery: id,
|
transformJourneysQuery: id,
|
||||||
|
|
||||||
parseDateTime,
|
parseDateTime,
|
||||||
|
|
|
@ -40,6 +40,8 @@ const parseArgs = [
|
||||||
['reachableFrom', 0, parseJsObject],
|
['reachableFrom', 0, parseJsObject],
|
||||||
['reachableFrom', 1, parseJsObject],
|
['reachableFrom', 1, parseJsObject],
|
||||||
['remarks', 0, parseJsObject],
|
['remarks', 0, parseJsObject],
|
||||||
|
['lines', 0, toString],
|
||||||
|
['lines', 1, parseJsObject],
|
||||||
['serverInfo', 0, parseJsObject],
|
['serverInfo', 0, parseJsObject],
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue