diff --git a/docs/readme.md b/docs/readme.md index ea55aa82..fa511bb8 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -4,7 +4,7 @@ - [`journeyLeg(ref, lineName, [opt])`](journey-leg.md) – get details for a leg of a journey - [`departures(station, [opt])`](departures.md) – query the next departures at a station - [`locations(query, [opt])`](locations.md) – find stations, POIs and addresses -- [`location(id)`](location.md) – get details about a location +- [`station(id)`](station.md) – get details about a station - [`nearby(location, [opt])`](nearby.md) – show stations & POIs around - [`radar(north, west, south, east, [opt])`](radar.md) – find all vehicles currently in a certain area diff --git a/docs/location.md b/docs/station.md similarity index 86% rename from docs/location.md rename to docs/station.md index 72cf9a40..1a0dd31b 100644 --- a/docs/location.md +++ b/docs/station.md @@ -1,9 +1,9 @@ -# `location(station)` +# `station(id)` -`station` must be in one of these formats: +`id` must be in one of these formats: ```js -// a station ID, in a format compatible to the profile you use +// a station ID, in a format compatible with the profile you use '900000123456' // an FPTF `station` object @@ -29,7 +29,7 @@ const vbbProfile = require('hafas-client/p/vbb') const client = createClient(vbbProfile) -client.location('900000042101') // U Spichernstr. +client.station('900000042101') // U Spichernstr. .then(console.log) .catch(console.error) ``` diff --git a/index.js b/index.js index 1db64568..4826e28d 100644 --- a/index.js +++ b/index.js @@ -221,7 +221,7 @@ const createClient = (profile, request = _request) => { }) } - const location = (station) => { + const station = (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.') @@ -368,7 +368,7 @@ const createClient = (profile, request = _request) => { }) } - const client = {departures, journeys, locations, location, nearby} + const client = {departures, journeys, locations, station, nearby} if (profile.journeyLeg) client.journeyLeg = journeyLeg if (profile.radar) client.radar = radar Object.defineProperty(client, 'profile', {value: profile}) diff --git a/lib/request.js b/lib/request.js index 3cde8fbb..7ecaabba 100644 --- a/lib/request.js +++ b/lib/request.js @@ -20,6 +20,7 @@ const request = (profile, data) => { headers: { 'Content-Type': 'application/json', 'Accept-Encoding': 'gzip, deflate', + 'Accept': 'application/json', 'user-agent': 'https://github.com/public-transport/hafas-client' }, query: {} diff --git a/p/db/example.js b/p/db/example.js index f740bea9..f9fe23e6 100644 --- a/p/db/example.js +++ b/p/db/example.js @@ -10,7 +10,7 @@ client.journeys('8011167', '8000261', {results: 1, tickets: true}) // client.departures('8011167', {duration: 1}) // client.locations('Berlin Jungfernheide') // client.locations('Atze Musiktheater', {poi: true, addressses: false, fuzzy: false}) -// client.location('8000309') // Regensburg Hbf +// client.station('8000309') // Regensburg Hbf // client.nearby(52.4751309, 13.3656537, {results: 1}) .then((data) => { diff --git a/p/insa/example.js b/p/insa/example.js index 8cbe8679..2260f34b 100644 --- a/p/insa/example.js +++ b/p/insa/example.js @@ -10,7 +10,7 @@ client.journeys('008010226', '008013456', {results: 1}) // client.departures('008010226', { duration: 5 }) // client.locations('Magdeburg Hbf', {results: 2}) // client.locations('Kunstmuseum Kloster Unser Lieben Frauen Magdeburg', {results: 2}) -// client.location('008010226') // Magdeburg-Neustadt +// client.station('008010226') // Magdeburg-Neustadt // client.nearby({ // type: 'location', // latitude: 52.148842, diff --git a/p/nahsh/example.js b/p/nahsh/example.js index ada76608..f3ec6237 100644 --- a/p/nahsh/example.js +++ b/p/nahsh/example.js @@ -10,7 +10,7 @@ client.journeys('8000103', '8000199', {results: 10, tickets: true}) // client.departures('8000199', {duration: 10}) // client.journeyLeg('1|30161|5|100|14032018', 'Bus 52') // client.locations('Schleswig', {results: 1}) -// client.location('706990') // Kiel Holunderbusch +// client.station('706990') // Kiel Holunderbusch // client.nearby({type: 'location', latitude: 54.295691, longitude: 10.116424}, {distance: 60}) // client.radar(54.4, 10.0, 54.2, 10.2, {results: 10}) diff --git a/p/oebb/example.js b/p/oebb/example.js index 2dcccf39..4f780f66 100644 --- a/p/oebb/example.js +++ b/p/oebb/example.js @@ -9,7 +9,7 @@ const client = createClient(oebbProfile) client.journeys('1291501', '8100002', {results: 1}) // client.departures('8100002', {duration: 1}) // client.locations('Salzburg', {results: 2}) -// client.location('8100173') // Graz Hbf +// client.station('8100173') // Graz Hbf // client.nearby(47.812851, 13.045604, {distance: 60}) // client.radar({ // north: 47.827203, diff --git a/p/vbb/example.js b/p/vbb/example.js index 82563af6..1234692a 100644 --- a/p/vbb/example.js +++ b/p/vbb/example.js @@ -9,7 +9,7 @@ const client = createClient(vbbProfile) client.journeys('900000003201', '900000024101', {results: 1, polylines: true}) // client.departures('900000013102', {duration: 1}) // client.locations('Alexanderplatz', {results: 2}) -// client.location('900000042101') // Spichernstr +// client.station('900000042101') // Spichernstr // client.nearby(52.5137344, 13.4744798, {distance: 60}) // client.radar({ // north: 52.52411, diff --git a/parse/departure.js b/parse/departure.js index 158eb1ea..1a496bc7 100644 --- a/parse/departure.js +++ b/parse/departure.js @@ -30,6 +30,8 @@ const createParseDeparture = (profile, stations, lines, remarks) => { res.delay = Math.round((realtime - planned) / 1000) } else res.delay = null + // todo: DRY with parseStopover + // todo: DRY with parseJourneyLeg if (d.stbStop.aCncl || d.stbStop.dCncl) { res.cancelled = true Object.defineProperty(res, 'canceled', {value: true}) diff --git a/parse/journey-leg.js b/parse/journey-leg.js index bbc3f9da..331f0c36 100644 --- a/parse/journey-leg.js +++ b/parse/journey-leg.js @@ -23,6 +23,8 @@ const createParseJourneyLeg = (profile, stations, lines, remarks, polylines) => arrival: arr.toISO() } + // todo: DRY with parseDeparture + // todo: DRY with parseStopover if (pt.dep.dTimeR && pt.dep.dTimeS) { const realtime = profile.parseDateTime(profile, j.date, pt.dep.dTimeR) const planned = profile.parseDateTime(profile, j.date, pt.dep.dTimeS) diff --git a/parse/stopover.js b/parse/stopover.js index 8315c814..52f69b17 100644 --- a/parse/stopover.js +++ b/parse/stopover.js @@ -7,16 +7,32 @@ const createParseStopover = (profile, stations, lines, remarks, date) => { const res = { station: stations[parseInt(st.locX)] || null, arrival: null, - departure: null + arrivalDelay: null, + departure: null, + departureDelay: null } + + // todo: DRY with parseDeparture + // todo: DRY with parseJourneyLeg if (st.aTimeR || st.aTimeS) { const arr = profile.parseDateTime(profile, date, st.aTimeR || st.aTimeS) res.arrival = arr.toISO() } + if (st.aTimeR && st.aTimeS) { + const realtime = profile.parseDateTime(profile, date, st.aTimeR) + const planned = profile.parseDateTime(profile, date, st.aTimeS) + res.arrivalDelay = Math.round((realtime - planned) / 1000) + } + if (st.dTimeR || st.dTimeS) { const dep = profile.parseDateTime(profile, date, st.dTimeR || st.dTimeS) res.departure = dep.toISO() } + if (st.dTimeR && st.dTimeS) { + const realtime = profile.parseDateTime(profile, date, st.dTimeR) + const planned = profile.parseDateTime(profile, date, st.dTimeS) + res.departureDelay = Math.round((realtime - planned) / 1000) + } // mark stations the train passes without stopping if(st.dInS === false && st.aOutS === false) res.passBy = true diff --git a/test/db.js b/test/db.js index 328171f9..2d183500 100644 --- a/test/db.js +++ b/test/db.js @@ -278,8 +278,8 @@ test('locations named Jungfernheide', co(function* (t) { t.end() })) -test('location', co(function* (t) { - const s = yield client.location(regensburgHbf) +test('station', co(function* (t) { + const s = yield client.station(regensburgHbf) validate(t, s, 'station', 'station') t.equal(s.id, regensburgHbf) diff --git a/test/insa.js b/test/insa.js index 9b3facce..82bd9bfb 100644 --- a/test/insa.js +++ b/test/insa.js @@ -217,8 +217,8 @@ test('locations named Magdeburg', co(function*(t) { t.end() })) -test('location Magdeburg-Buckau', co(function* (t) { - const s = yield client.location(magdeburgBuckau) +test('station Magdeburg-Buckau', co(function* (t) { + const s = yield client.station(magdeburgBuckau) validate(t, s, 'station', 'station') t.equal(s.id, magdeburgBuckau) diff --git a/test/nahsh.js b/test/nahsh.js index 5648f972..ab82635b 100644 --- a/test/nahsh.js +++ b/test/nahsh.js @@ -267,8 +267,8 @@ test('locations named Kiel', co(function* (t) { t.end() })) -test('location', co(function* (t) { - const s = yield client.location(kielHbf) +test('station', co(function* (t) { + const s = yield client.station(kielHbf) validate(t, s, 'station', 'station') t.equal(s.id, kielHbf) diff --git a/test/oebb.js b/test/oebb.js index 253ac9c1..c88c85e5 100644 --- a/test/oebb.js +++ b/test/oebb.js @@ -303,8 +303,8 @@ test('locations named Salzburg', co(function* (t) { t.end() })) -test('location', co(function* (t) { - const loc = yield client.location(wienRenngasse) +test('station', co(function* (t) { + const loc = yield client.station(wienRenngasse) // todo: find a way to always get products from the API // todo: cfg.stationProductsOptional option diff --git a/test/vbb.js b/test/vbb.js index fcbf68fa..8cebcb5f 100644 --- a/test/vbb.js +++ b/test/vbb.js @@ -342,8 +342,8 @@ test('locations', co(function* (t) { t.end() })) -test('location', co(function* (t) { - const s = yield client.location(spichernstr) +test('station', co(function* (t) { + const s = yield client.station(spichernstr) validate(t, s, 'station', 'station') t.equal(s.id, spichernstr)