merge any-endpoint into oebb

This commit is contained in:
Julius Tens 2017-12-28 17:39:09 +01:00
commit 0de6776815
14 changed files with 118 additions and 118 deletions

View file

@ -33,7 +33,7 @@ With `opt`, you can override the default options, which look like this:
*Note:* As stated in the [*Friendly Public Transport Format* `1.0.1`](https://github.com/public-transport/friendly-public-transport-format/tree/1.0.1), the `when` include the current delay. The `delay` field, if present, expresses how much the former differs from the schedule. *Note:* As stated in the [*Friendly Public Transport Format* `1.0.1`](https://github.com/public-transport/friendly-public-transport-format/tree/1.0.1), the `when` include the current delay. The `delay` field, if present, expresses how much the former differs from the schedule.
You may pass the `journeyId` field into [`journeyPart(ref, lineName, [opt])`](journey-part.md) to get details on the vehicle's journey. You may pass the `journeyId` field into [`journeyLeg(ref, lineName, [opt])`](journey-leg.md) to get details on the vehicle's journey.
As an example, we're going to use the VBB profile: As an example, we're going to use the VBB profile:

View file

@ -1,4 +1,4 @@
# `journeyPart(ref, lineName, [opt])` # `journeyLeg(ref, lineName, [opt])`
This method can be used to refetch information about a leg of a journey. Note that it is not supported by every profile/endpoint. This method can be used to refetch information about a leg of a journey. Note that it is not supported by every profile/endpoint.
@ -13,8 +13,8 @@ const client = createClient(vbbProfile)
// Hauptbahnhof to Heinrich-Heine-Str. // Hauptbahnhof to Heinrich-Heine-Str.
client.journeys('900000003201', '900000100008', {results: 1}) client.journeys('900000003201', '900000100008', {results: 1})
.then(([journey]) => { .then(([journey]) => {
const part = journey.parts[0] const leg = journey.legs[0]
return client.journeyPart(part.id, part.line.name) return client.journeyLeg(leg.id, leg.line.name)
}) })
.then(console.log) .then(console.log)
.catch(console.error) .catch(console.error)
@ -41,7 +41,7 @@ const vbbProfile = require('hafas-client/p/vbb')
const client = createClient(vbbProfile) const client = createClient(vbbProfile)
client.journeyPart('1|31431|28|86|17122017', 'S9', {when: 1513534689273}) client.journeyLeg('1|31431|28|86|17122017', 'S9', {when: 1513534689273})
.then(console.log) .then(console.log)
.catch(console.error) .catch(console.error)
``` ```

View file

@ -86,7 +86,7 @@ The response may look like this:
```js ```js
[ { [ {
parts: [ { legs: [ {
id: '1|31041|35|86|17122017', id: '1|31041|35|86|17122017',
origin: { origin: {
type: 'station', type: 'station',

View file

@ -169,7 +169,7 @@ const createClient = (profile) => {
}) })
} }
const journeyPart = (ref, lineName, opt = {}) => { const journeyLeg = (ref, lineName, opt = {}) => {
opt = Object.assign({ opt = Object.assign({
passedStations: true // return stations on the way? passedStations: true // return stations on the way?
}, opt) }, opt)
@ -185,15 +185,15 @@ const createClient = (profile) => {
} }
}) })
.then((d) => { .then((d) => {
const parse = profile.parseJourneyPart(profile, d.locations, d.lines, d.remarks) const parse = profile.parseJourneyLeg(profile, d.locations, d.lines, d.remarks)
const part = { // pretend the part is contained in a journey const leg = { // pretend the leg is contained in a journey
type: 'JNY', type: 'JNY',
dep: minBy(d.journey.stopL, 'idx'), dep: minBy(d.journey.stopL, 'idx'),
arr: maxBy(d.journey.stopL, 'idx'), arr: maxBy(d.journey.stopL, 'idx'),
jny: d.journey jny: d.journey
} }
return parse(d.journey, part, !!opt.passedStations) return parse(d.journey, leg, !!opt.passedStations)
}) })
} }
@ -239,7 +239,7 @@ const createClient = (profile) => {
} }
const client = {departures, journeys, locations, nearby} const client = {departures, journeys, locations, nearby}
if (profile.journeyPart) client.journeyPart = journeyPart 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})
return client return client

View file

@ -2,7 +2,7 @@
const parseDateTime = require('../parse/date-time') const parseDateTime = require('../parse/date-time')
const parseDeparture = require('../parse/departure') const parseDeparture = require('../parse/departure')
const parseJourneyPart = require('../parse/journey-part') const parseJourneyLeg = require('../parse/journey-leg')
const parseJourney = require('../parse/journey') const parseJourney = require('../parse/journey')
const parseLine = require('../parse/line') const parseLine = require('../parse/line')
const parseLocation = require('../parse/location') const parseLocation = require('../parse/location')
@ -33,7 +33,7 @@ const defaultProfile = {
parseDateTime, parseDateTime,
parseDeparture, parseDeparture,
parseJourneyPart, parseJourneyLeg,
parseJourney, parseJourney,
parseLine, parseLine,
parseStationName: id, parseStationName: id,
@ -55,7 +55,7 @@ const defaultProfile = {
formatRectangle, formatRectangle,
filters, filters,
journeyPart: false, journeyLeg: false,
radar: false radar: false
} }

View file

@ -11,7 +11,7 @@ const types = {
parseDateTime: 'function', parseDateTime: 'function',
parseDeparture: 'function', parseDeparture: 'function',
parseJourneyPart: 'function', parseJourneyLeg: 'function',
parseJourney: 'function', parseJourney: 'function',
parseLine: 'function', parseLine: 'function',
parseStationName: 'function', parseStationName: 'function',

View file

@ -18,15 +18,15 @@ const m = {
}, },
regionalExp: { regionalExp: {
bitmask: 4, bitmask: 4,
name: 'InterRegio', name: 'RegionalExpress & InterRegio',
short: 'IR', short: 'RE/IR',
mode: 'train', mode: 'train',
product: 'regionalExp' product: 'regionalExp'
}, },
regional: { regional: {
bitmask: 8, bitmask: 8,
name: 'RegionalExpress & Regio', name: 'Regio',
short: 'RE/RB', short: 'RB',
mode: 'train', mode: 'train',
product: 'regional' product: 'regional'
}, },

View file

@ -178,7 +178,7 @@ const vbbProfile = {
formatStation, formatStation,
formatProducts, formatProducts,
journeyPart: true, journeyLeg: true,
radar: true radar: true
} }

View file

@ -7,7 +7,7 @@ module.exports = {
remark: require('./remark'), remark: require('./remark'),
operator: require('./operator'), operator: require('./operator'),
stopover: require('./stopover'), stopover: require('./stopover'),
journeyPart: require('./journey-part'), journeyLeg: require('./journey-leg'),
journey: require('./journey'), journey: require('./journey'),
nearby: require('./nearby'), nearby: require('./nearby'),
movement: require('./movement') movement: require('./movement')

View file

@ -4,7 +4,7 @@ const parseDateTime = require('./date-time')
const clone = obj => Object.assign({}, obj) const clone = obj => Object.assign({}, obj)
const createParseJourneyPart = (profile, stations, lines, remarks) => { const createParseJourneyLeg = (profile, stations, lines, remarks) => {
// todo: finish parse/remark.js first // todo: finish parse/remark.js first
const applyRemark = (j, rm) => {} const applyRemark = (j, rm) => {}
@ -13,7 +13,7 @@ const createParseJourneyPart = (profile, stations, lines, remarks) => {
// todo: what is pt.jny.dirFlg? // todo: what is pt.jny.dirFlg?
// todo: how does pt.freq work? // todo: how does pt.freq work?
// todo: what is pt.himL? // todo: what is pt.himL?
const parseJourneyPart = (j, pt, passed = true) => { // j = journey, pt = part const parseJourneyLeg = (j, pt, passed = true) => { // j = journey, pt = part
const dep = profile.parseDateTime(profile, j.date, pt.dep.dTimeR || pt.dep.dTimeS) const dep = profile.parseDateTime(profile, j.date, pt.dep.dTimeR || pt.dep.dTimeS)
const arr = profile.parseDateTime(profile, j.date, pt.arr.aTimeR || pt.arr.aTimeS) const arr = profile.parseDateTime(profile, j.date, pt.arr.aTimeR || pt.arr.aTimeS)
const res = { const res = {
@ -79,7 +79,7 @@ const createParseJourneyPart = (profile, stations, lines, remarks) => {
return res return res
} }
return parseJourneyPart return parseJourneyLeg
} }
module.exports = createParseJourneyPart module.exports = createParseJourneyLeg

View file

@ -1,26 +1,26 @@
'use strict' 'use strict'
const createParseJourneyPart = require('./journey-part') const createParseJourneyLeg = require('./journey-leg')
const clone = obj => Object.assign({}, obj) const clone = obj => Object.assign({}, obj)
const createParseJourney = (profile, stations, lines, remarks) => { const createParseJourney = (profile, stations, lines, remarks) => {
const parsePart = createParseJourneyPart(profile, stations, lines, remarks) const parseLeg = createParseJourneyLeg(profile, stations, lines, remarks)
// todo: c.sDays // todo: c.sDays
// todo: c.dep.dProgType, c.arr.dProgType // todo: c.dep.dProgType, c.arr.dProgType
// todo: c.conSubscr // todo: c.conSubscr
// todo: c.trfRes x vbb-parse-ticket // todo: c.trfRes x vbb-parse-ticket
const parseJourney = (j) => { const parseJourney = (j) => {
const parts = j.secL.map(part => parsePart(j, part)) const legs = j.secL.map(leg => parseLeg(j, leg))
const res = { const res = {
parts, legs,
origin: parts[0].origin, origin: legs[0].origin,
destination: parts[parts.length - 1].destination, destination: legs[legs.length - 1].destination,
departure: parts[0].departure, departure: legs[0].departure,
arrival: parts[parts.length - 1].arrival arrival: legs[legs.length - 1].arrival
} }
if (parts.some(p => p.cancelled)) { if (legs.some(p => p.cancelled)) {
res.cancelled = true res.cancelled = true
res.departure = res.arrival = null res.departure = res.arrival = null
} }

View file

@ -28,7 +28,7 @@ npm install hafas-client
## API ## API
- [`journeys(from, to, [opt])`](docs/journeys.md) get journeys between locations - [`journeys(from, to, [opt])`](docs/journeys.md) get journeys between locations
- [`journeyPart(ref, name, [opt])`](docs/journey-part.md) get details for a part of a journey - [`journeyLeg(ref, name, [opt])`](docs/journey-leg.md) get details for a leg of a journey
- [`departures(station, [opt])`](docs/departures.md) query the next departures at a station - [`departures(station, [opt])`](docs/departures.md) query the next departures at a station
- [`locations(query, [opt])`](docs/locations.md) find stations, POIs and addresses - [`locations(query, [opt])`](docs/locations.md) find stations, POIs and addresses
- [`nearby(latitude, longitude, [opt])`](docs/nearby.md) show stations & POIs around - [`nearby(latitude, longitude, [opt])`](docs/nearby.md) show stations & POIs around
@ -54,7 +54,7 @@ The returned [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript
```js ```js
[ { [ {
parts: [ { legs: [ {
id: '1|100067|48|81|17122017', id: '1|100067|48|81|17122017',
origin: { origin: {
type: 'station', type: 'station',

View file

@ -118,30 +118,30 @@ test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) {
} }
t.ok(isValidWhen(journey.arrival)) t.ok(isValidWhen(journey.arrival))
t.ok(Array.isArray(journey.parts)) t.ok(Array.isArray(journey.legs))
t.ok(journey.parts.length > 0, 'no parts') t.ok(journey.legs.length > 0, 'no legs')
const part = journey.parts[0] const leg = journey.legs[0]
assertValidStation(t, part.origin) assertValidStation(t, leg.origin)
assertValidStationProducts(t, part.origin.products) assertValidStationProducts(t, leg.origin.products)
if (!(yield findStation(part.origin.id))) { if (!(yield findStation(leg.origin.id))) {
console.error('unknown station', part.origin.id, part.origin.name) console.error('unknown station', leg.origin.id, leg.origin.name)
} }
t.ok(isValidWhen(part.departure)) t.ok(isValidWhen(leg.departure))
t.equal(typeof part.departurePlatform, 'string') t.equal(typeof leg.departurePlatform, 'string')
assertValidStation(t, part.destination) assertValidStation(t, leg.destination)
assertValidStationProducts(t, part.origin.products) assertValidStationProducts(t, leg.origin.products)
if (!(yield findStation(part.destination.id))) { if (!(yield findStation(leg.destination.id))) {
console.error('unknown station', part.destination.id, part.destination.name) console.error('unknown station', leg.destination.id, leg.destination.name)
} }
t.ok(isValidWhen(part.arrival)) t.ok(isValidWhen(leg.arrival))
t.equal(typeof part.arrivalPlatform, 'string') t.equal(typeof leg.arrivalPlatform, 'string')
assertValidLine(t, part.line) assertValidLine(t, leg.line)
t.ok(Array.isArray(part.passed)) t.ok(Array.isArray(leg.passed))
for (let stopover of part.passed) assertValidStopover(t, stopover) for (let stopover of leg.passed) assertValidStopover(t, stopover)
if (journey.price) assertValidPrice(t, journey.price) if (journey.price) assertValidPrice(t, journey.price)
} }
@ -158,18 +158,18 @@ test('Berlin Jungfernheide to Torfstraße 17', co.wrap(function* (t) {
t.ok(Array.isArray(journeys)) t.ok(Array.isArray(journeys))
t.ok(journeys.length >= 1, 'no journeys') t.ok(journeys.length >= 1, 'no journeys')
const journey = journeys[0] const journey = journeys[0]
const part = journey.parts[journey.parts.length - 1] const leg = journey.legs[journey.legs.length - 1]
assertValidStation(t, part.origin) assertValidStation(t, leg.origin)
assertValidStationProducts(t, part.origin.products) assertValidStationProducts(t, leg.origin.products)
if (!(yield findStation(part.origin.id))) { if (!(yield findStation(leg.origin.id))) {
console.error('unknown station', part.origin.id, part.origin.name) console.error('unknown station', leg.origin.id, leg.origin.name)
} }
if (part.origin.products) assertValidProducts(t, part.origin.products) if (leg.origin.products) assertValidProducts(t, leg.origin.products)
t.ok(isValidWhen(part.departure)) t.ok(isValidWhen(leg.departure))
t.ok(isValidWhen(part.arrival)) t.ok(isValidWhen(leg.arrival))
const d = part.destination const d = leg.destination
assertValidAddress(t, d) assertValidAddress(t, d)
t.equal(d.address, 'Torfstraße 17') t.equal(d.address, 'Torfstraße 17')
t.ok(isRoughlyEqual(.0001, d.latitude, 52.5416823)) t.ok(isRoughlyEqual(.0001, d.latitude, 52.5416823))
@ -187,18 +187,18 @@ test('Berlin Jungfernheide to ATZE Musiktheater', co.wrap(function* (t) {
t.ok(Array.isArray(journeys)) t.ok(Array.isArray(journeys))
t.ok(journeys.length >= 1, 'no journeys') t.ok(journeys.length >= 1, 'no journeys')
const journey = journeys[0] const journey = journeys[0]
const part = journey.parts[journey.parts.length - 1] const leg = journey.legs[journey.legs.length - 1]
assertValidStation(t, part.origin) assertValidStation(t, leg.origin)
assertValidStationProducts(t, part.origin.products) assertValidStationProducts(t, leg.origin.products)
if (!(yield findStation(part.origin.id))) { if (!(yield findStation(leg.origin.id))) {
console.error('unknown station', part.origin.id, part.origin.name) console.error('unknown station', leg.origin.id, leg.origin.name)
} }
if (part.origin.products) assertValidProducts(t, part.origin.products) if (leg.origin.products) assertValidProducts(t, leg.origin.products)
t.ok(isValidWhen(part.departure)) t.ok(isValidWhen(leg.departure))
t.ok(isValidWhen(part.arrival)) t.ok(isValidWhen(leg.arrival))
const d = part.destination const d = leg.destination
assertValidPoi(t, d) assertValidPoi(t, d)
t.equal(d.name, 'ATZE Musiktheater') t.equal(d.name, 'ATZE Musiktheater')
t.ok(isRoughlyEqual(.0001, d.latitude, 52.542399)) t.ok(isRoughlyEqual(.0001, d.latitude, 52.542399))

View file

@ -77,29 +77,29 @@ test('journeys  station to station', co.wrap(function* (t) {
t.strictEqual(journey.destination.id, amrumerStr) t.strictEqual(journey.destination.id, amrumerStr)
assertValidWhen(t, journey.arrival) assertValidWhen(t, journey.arrival)
t.ok(Array.isArray(journey.parts)) t.ok(Array.isArray(journey.legs))
t.strictEqual(journey.parts.length, 1) t.strictEqual(journey.legs.length, 1)
const part = journey.parts[0] const leg = journey.legs[0]
t.equal(typeof part.id, 'string') t.equal(typeof leg.id, 'string')
t.ok(part.id) t.ok(leg.id)
assertValidStation(t, part.origin) assertValidStation(t, leg.origin)
assertValidStationProducts(t, part.origin.products) assertValidStationProducts(t, leg.origin.products)
t.ok(part.origin.name.indexOf('(Berlin)') === -1) t.ok(leg.origin.name.indexOf('(Berlin)') === -1)
t.strictEqual(part.origin.id, spichernstr) t.strictEqual(leg.origin.id, spichernstr)
assertValidWhen(t, part.departure) assertValidWhen(t, leg.departure)
assertValidStation(t, part.destination) assertValidStation(t, leg.destination)
assertValidStationProducts(t, part.destination.products) assertValidStationProducts(t, leg.destination.products)
t.strictEqual(part.destination.id, amrumerStr) t.strictEqual(leg.destination.id, amrumerStr)
assertValidWhen(t, part.arrival) assertValidWhen(t, leg.arrival)
assertValidLine(t, part.line) assertValidLine(t, leg.line)
t.ok(findStation(part.direction)) t.ok(findStation(leg.direction))
t.ok(part.direction.indexOf('(Berlin)') === -1) t.ok(leg.direction.indexOf('(Berlin)') === -1)
t.ok(Array.isArray(part.passed)) t.ok(Array.isArray(leg.passed))
for (let passed of part.passed) assertValidStopover(t, passed) for (let passed of leg.passed) assertValidStopover(t, passed)
// todo: find a journey where there ticket info is always available // todo: find a journey where there ticket info is always available
if (journey.tickets) { if (journey.tickets) {
@ -128,11 +128,11 @@ test('journeys  only subway', co.wrap(function* (t) {
t.ok(journeys.length > 1) t.ok(journeys.length > 1)
for (let journey of journeys) { for (let journey of journeys) {
for (let part of journey.parts) { for (let leg of journey.legs) {
if (part.line) { if (leg.line) {
assertValidLine(t, part.line) assertValidLine(t, leg.line)
t.equal(part.line.mode, 'train') t.equal(leg.line.mode, 'train')
t.equal(part.line.product, 'subway') t.equal(leg.line.product, 'subway')
} }
} }
} }
@ -159,26 +159,26 @@ test('journeys  fails with no product', co.wrap(function* (t) {
} }
})) }))
test('journey part details', co.wrap(function* (t) { test('journey leg details', co.wrap(function* (t) {
const journeys = yield client.journeys(spichernstr, amrumerStr, { const journeys = yield client.journeys(spichernstr, amrumerStr, {
results: 1, when results: 1, when
}) })
const p = journeys[0].parts[0] const p = journeys[0].legs[0]
t.ok(p.id, 'precondition failed') t.ok(p.id, 'precondition failed')
t.ok(p.line.name, 'precondition failed') t.ok(p.line.name, 'precondition failed')
const part = yield client.journeyPart(p.id, p.line.name, {when}) const leg = yield client.journeyLeg(p.id, p.line.name, {when})
t.equal(typeof part.id, 'string') t.equal(typeof leg.id, 'string')
t.ok(part.id) t.ok(leg.id)
assertValidLine(t, part.line) assertValidLine(t, leg.line)
t.equal(typeof part.direction, 'string') t.equal(typeof leg.direction, 'string')
t.ok(part.direction) t.ok(leg.direction)
t.ok(Array.isArray(part.passed)) t.ok(Array.isArray(leg.passed))
for (let passed of part.passed) assertValidStopover(t, passed) for (let passed of leg.passed) assertValidStopover(t, passed)
t.end() t.end()
})) }))
@ -194,18 +194,18 @@ test('journeys  station to address', co.wrap(function* (t) {
t.ok(Array.isArray(journeys)) t.ok(Array.isArray(journeys))
t.strictEqual(journeys.length, 1) t.strictEqual(journeys.length, 1)
const journey = journeys[0] const journey = journeys[0]
const part = journey.parts[journey.parts.length - 1] const leg = journey.legs[journey.legs.length - 1]
assertValidStation(t, part.origin) assertValidStation(t, leg.origin)
assertValidStationProducts(t, part.origin.products) assertValidStationProducts(t, leg.origin.products)
assertValidWhen(t, part.departure) assertValidWhen(t, leg.departure)
const dest = part.destination const dest = leg.destination
assertValidAddress(t, dest) assertValidAddress(t, dest)
t.strictEqual(dest.address, 'Torfstraße 17') t.strictEqual(dest.address, 'Torfstraße 17')
t.ok(isRoughlyEqual(.0001, dest.latitude, 52.5416823)) t.ok(isRoughlyEqual(.0001, dest.latitude, 52.5416823))
t.ok(isRoughlyEqual(.0001, dest.longitude, 13.3491223)) t.ok(isRoughlyEqual(.0001, dest.longitude, 13.3491223))
assertValidWhen(t, part.arrival) assertValidWhen(t, leg.arrival)
t.end() t.end()
})) }))
@ -221,18 +221,18 @@ test('journeys  station to POI', co.wrap(function* (t) {
t.ok(Array.isArray(journeys)) t.ok(Array.isArray(journeys))
t.strictEqual(journeys.length, 1) t.strictEqual(journeys.length, 1)
const journey = journeys[0] const journey = journeys[0]
const part = journey.parts[journey.parts.length - 1] const leg = journey.legs[journey.legs.length - 1]
assertValidStation(t, part.origin) assertValidStation(t, leg.origin)
assertValidStationProducts(t, part.origin.products) assertValidStationProducts(t, leg.origin.products)
assertValidWhen(t, part.departure) assertValidWhen(t, leg.departure)
const dest = part.destination const dest = leg.destination
assertValidPoi(t, dest) assertValidPoi(t, dest)
t.strictEqual(dest.name, 'ATZE Musiktheater') t.strictEqual(dest.name, 'ATZE Musiktheater')
t.ok(isRoughlyEqual(.0001, dest.latitude, 52.543333)) t.ok(isRoughlyEqual(.0001, dest.latitude, 52.543333))
t.ok(isRoughlyEqual(.0001, dest.longitude, 13.351686)) t.ok(isRoughlyEqual(.0001, dest.longitude, 13.351686))
assertValidWhen(t, part.arrival) assertValidWhen(t, leg.arrival)
t.end() t.end()
})) }))