From a7c550ab69b8ed755ced1c7676690d93282a0507 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Thu, 7 Jun 2018 12:04:21 +0200 Subject: [PATCH 1/6] send Accept: application/json :bug: --- lib/request.js | 1 + 1 file changed, 1 insertion(+) 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: {} From 0136189aa4d0137cb647fec6cece3f399ab0eb4a Mon Sep 17 00:00:00 2001 From: Jannis R Date: Thu, 7 Jun 2018 12:59:50 +0200 Subject: [PATCH 2/6] tests: fix isRoughlyEqual calls :white_check_mark: --- test/db.js | 8 ++++---- test/insa.js | 8 ++++---- test/nahsh.js | 8 ++++---- test/oebb.js | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/test/db.js b/test/db.js index 20328df6..7741ffa4 100644 --- a/test/db.js +++ b/test/db.js @@ -56,8 +56,8 @@ const isJungfernheide = (s) => { (s.id === '008011167' || s.id === jungfernh) && s.name === 'Berlin Jungfernheide' && s.location && - isRoughlyEqual(s.location.latitude, 52.530408, .0005) && - isRoughlyEqual(s.location.longitude, 13.299424, .0005) + isRoughlyEqual(.0005, s.location.latitude, 52.530408) && + isRoughlyEqual(.0005, s.location.longitude, 13.299424) } const assertIsJungfernheide = (t, s) => { @@ -65,8 +65,8 @@ const assertIsJungfernheide = (t, s) => { t.ok(s.id === '008011167' || s.id === jungfernh, 'id should be 8011167') t.equal(s.name, 'Berlin Jungfernheide') t.ok(s.location) - t.ok(isRoughlyEqual(s.location.latitude, 52.530408, .0005)) - t.ok(isRoughlyEqual(s.location.longitude, 13.299424, .0005)) + t.ok(isRoughlyEqual(.0005, s.location.latitude, 52.530408)) + t.ok(isRoughlyEqual(.0005, s.location.longitude, 13.299424)) } // todo: DRY with assertValidStationProducts diff --git a/test/insa.js b/test/insa.js index f510e28a..7e2b1b38 100644 --- a/test/insa.js +++ b/test/insa.js @@ -40,8 +40,8 @@ const isMagdeburgHbf = s => { (s.id === '8010224' || s.id === '008010224') && s.name === 'Magdeburg Hbf' && s.location && - isRoughlyEqual(s.location.latitude, 52.130352, 0.001) && - isRoughlyEqual(s.location.longitude, 11.626891, 0.001) + isRoughlyEqual(.001, s.location.latitude, 52.130352) && + isRoughlyEqual(.001, s.location.longitude, 11.626891) ) } @@ -50,8 +50,8 @@ const assertIsMagdeburgHbf = (t, s) => { t.ok(s.id === '8010224' || s.id === '008010224', 'id should be 8010224') t.equal(s.name, 'Magdeburg Hbf') t.ok(s.location) - t.ok(isRoughlyEqual(s.location.latitude, 52.130352, 0.001)) - t.ok(isRoughlyEqual(s.location.longitude, 11.626891, 0.001)) + t.ok(isRoughlyEqual(.001, s.location.latitude, 52.130352)) + t.ok(isRoughlyEqual(.001, s.location.longitude, 11.626891)) } // todo: DRY with assertValidStationProducts diff --git a/test/nahsh.js b/test/nahsh.js index 50a561b7..6ee4d33f 100644 --- a/test/nahsh.js +++ b/test/nahsh.js @@ -43,8 +43,8 @@ const isKielHbf = (s) => { (s.id === '8000199') && s.name === 'Kiel Hbf' && s.location && - isRoughlyEqual(s.location.latitude, 54.314982, .0005) && - isRoughlyEqual(s.location.longitude, 10.131976, .0005) + isRoughlyEqual(.0005, s.location.latitude, 54.314982) && + isRoughlyEqual(.0005, s.location.longitude, 10.131976) } const assertIsKielHbf = (t, s) => { @@ -52,8 +52,8 @@ const assertIsKielHbf = (t, s) => { t.ok(s.id === '8000199', 'id should be 8000199') t.equal(s.name, 'Kiel Hbf') t.ok(s.location) - t.ok(isRoughlyEqual(s.location.latitude, 54.314982, .0005)) - t.ok(isRoughlyEqual(s.location.longitude, 10.131976, .0005)) + t.ok(isRoughlyEqual(.0005, s.location.latitude, 54.314982)) + t.ok(isRoughlyEqual(.0005, s.location.longitude, 10.131976)) } // todo: DRY with assertValidStationProducts diff --git a/test/oebb.js b/test/oebb.js index a45dc0b1..9db6a42f 100644 --- a/test/oebb.js +++ b/test/oebb.js @@ -60,8 +60,8 @@ const isSalzburgHbf = (s) => { (s.id === '008100002' || s.id === '8100002') && s.name === 'Salzburg Hbf' && s.location && - isRoughlyEqual(s.location.latitude, 47.812851, .0005) && - isRoughlyEqual(s.location.longitude, 13.045604, .0005) + isRoughlyEqual(.0005, s.location.latitude, 47.812851) && + isRoughlyEqual(.0005, s.location.longitude, 13.045604) } const assertIsSalzburgHbf = (t, s) => { @@ -69,8 +69,8 @@ const assertIsSalzburgHbf = (t, s) => { t.ok(s.id === '008100002' || s.id === '8100002', 'id should be 8100002') t.equal(s.name, 'Salzburg Hbf') t.ok(s.location) - t.ok(isRoughlyEqual(s.location.latitude, 47.812851, .0005)) - t.ok(isRoughlyEqual(s.location.longitude, 13.045604, .0005)) + t.ok(isRoughlyEqual(.0005, s.location.latitude, 47.812851)) + t.ok(isRoughlyEqual(.0005, s.location.longitude, 13.045604)) } // todo: DRY with assertValidStationProducts From 21c273cec7e71612b6a32b89068a211e56a8fa5d Mon Sep 17 00:00:00 2001 From: Jannis R Date: Thu, 7 Jun 2018 18:52:12 +0200 Subject: [PATCH 3/6] journey leg passed stations: parse & expose delays --- parse/departure.js | 2 ++ parse/journey-leg.js | 2 ++ parse/stopover.js | 18 +++++++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) 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 From 665bed9f79ae2e4fe85c324308870ca920dd82bb Mon Sep 17 00:00:00 2001 From: Jannis R Date: Mon, 4 Jun 2018 19:00:19 +0200 Subject: [PATCH 4/6] rename location(id) -> station(id) :boom: see #17 for reasons closes #17 --- index.js | 4 ++-- p/db/example.js | 2 +- p/insa/example.js | 2 +- p/nahsh/example.js | 2 +- p/oebb/example.js | 2 +- p/vbb/example.js | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) 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/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, From 2d11cfae1ef224b9bed7ca90c456007938bc6c7d Mon Sep 17 00:00:00 2001 From: Jannis R Date: Mon, 4 Jun 2018 19:01:09 +0200 Subject: [PATCH 5/6] adapt docs to 3c9c5cb :memo: --- docs/readme.md | 2 +- docs/{location.md => station.md} | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) rename docs/{location.md => station.md} (86%) 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) ``` From 46c05eba05cfc688c100c2f5f5d04c82bba2e395 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Mon, 4 Jun 2018 19:01:21 +0200 Subject: [PATCH 6/6] adapt tests to 3c9c5cb :white_check_mark: --- test/db.js | 4 ++-- test/insa.js | 4 ++-- test/nahsh.js | 4 ++-- test/oebb.js | 4 ++-- test/vbb.js | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/db.js b/test/db.js index 7741ffa4..ff578620 100644 --- a/test/db.js +++ b/test/db.js @@ -367,8 +367,8 @@ test('locations named Jungfernheide', co(function* (t) { t.end() })) -test('location', co(function* (t) { - const loc = yield client.location(regensburgHbf) +test('station', co(function* (t) { + const loc = yield client.station(regensburgHbf) assertValidStation(t, loc) t.equal(loc.id, regensburgHbf) diff --git a/test/insa.js b/test/insa.js index 7e2b1b38..003b5be7 100644 --- a/test/insa.js +++ b/test/insa.js @@ -312,9 +312,9 @@ test('locations named Magdeburg', co(function*(t) { t.end() })) -test('location', co(function*(t) { +test('station', co(function*(t) { const magdeburgBuckau = '8013456' - const loc = yield client.location(magdeburgBuckau) + const loc = yield client.station(magdeburgBuckau) assertValidStation(t, loc) t.equal(loc.id, magdeburgBuckau) diff --git a/test/nahsh.js b/test/nahsh.js index 6ee4d33f..51198bb8 100644 --- a/test/nahsh.js +++ b/test/nahsh.js @@ -369,8 +369,8 @@ test('locations named Kiel', co(function* (t) { t.end() })) -test('location', co(function* (t) { - const loc = yield client.location(schleswig) +test('station', co(function* (t) { + const loc = yield client.station(schleswig) assertValidStation(t, loc) t.equal(loc.id, schleswig) diff --git a/test/oebb.js b/test/oebb.js index 9db6a42f..d57b4a7e 100644 --- a/test/oebb.js +++ b/test/oebb.js @@ -418,8 +418,8 @@ test('locations named Salzburg', co(function* (t) { t.end() })) -test('location', co(function* (t) { - const loc = yield client.location(grazHbf) +test('station', co(function* (t) { + const loc = yield client.station(grazHbf) assertValidStation(t, loc) t.equal(loc.id, grazHbf) diff --git a/test/vbb.js b/test/vbb.js index cc159894..96a8d4ab 100644 --- a/test/vbb.js +++ b/test/vbb.js @@ -456,8 +456,8 @@ test('locations', co(function* (t) { t.end() })) -test('location', co(function* (t) { - const loc = yield client.location(spichernstr) +test('station', co(function* (t) { + const loc = yield client.station(spichernstr) assertValidStation(t, loc) t.equal(loc.id, spichernstr)