From 5b754aaa5506dbda781cbae0a6992c7a14405ac9 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Tue, 24 Jul 2018 17:37:36 +0200 Subject: [PATCH] refreshJourney method --- index.js | 42 ++++++++++++++++++++++++++++++++++++++++-- parse/journey.js | 3 ++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 671290a5..54235449 100644 --- a/index.js +++ b/index.js @@ -59,7 +59,7 @@ const createClient = (profile, userAgent, request = _request) => { dirLoc: dir, jnyFltrL: [products], dur: opt.duration, - getPasslist: false, // todo: what is this? + getPasslist: false, // todo stbFltrEquiv: !opt.includeRelatedStations } }) @@ -224,6 +224,44 @@ const createClient = (profile, userAgent, request = _request) => { return more(when, journeysRef) } + const refreshJourney = (refreshToken, opt = {}) => { + if ('string' !== typeof refreshToken || !refreshToken) { + new Error('refreshToken must be a non-empty string.') + } + + opt = Object.assign({ + stopovers: false, // return stations on the way? + tickets: false, // return tickets? + polylines: false, // return leg shapes? + remarks: true // parse & expose hints & warnings? + }, opt) + + return request(profile, opt, { + meth: 'Reconstruction', + req: { + ctxRecon: refreshToken, + getIST: true, // todo: make an option + getPasslist: !!opt.stopovers, + getPolyline: !!opt.polylines, + getTariff: !!opt.tickets + } + }) + .then((d) => { + if (!Array.isArray(d.outConL) || !d.outConL[0]) { + throw new Error('invalid response') + } + + const parse = profile.parseJourney(profile, opt, { + locations: d.locations, + lines: d.lines, + hints: d.hints, + warnings: d.warnings, + polylines: opt.polylines && d.common.polyL || [] + }) + return parse(d.outConL[0]) + }) + } + const locations = (query, opt = {}) => { if (!isNonEmptyString(query)) { throw new Error('query must be a non-empty string.') @@ -420,7 +458,7 @@ const createClient = (profile, userAgent, request = _request) => { }) } - const client = {departures, arrivals, journeys, locations, station, nearby} + const client = {departures, arrivals, journeys, refreshJourney, locations, station, nearby} if (profile.trip) client.trip = trip if (profile.radar) client.radar = radar Object.defineProperty(client, 'profile', {value: profile}) diff --git a/parse/journey.js b/parse/journey.js index a852469f..0ebe1001 100644 --- a/parse/journey.js +++ b/parse/journey.js @@ -20,7 +20,8 @@ const createParseJourney = (profile, opt, data) => { const legs = j.secL.map(leg => parseLeg(j, leg)) const res = { type: 'journey', - legs + legs, + refreshToken: j.ctxRecon || null } if (opt.remarks && Array.isArray(j.msgL)) {