Skip to content

Commit

Permalink
rest.exe: adapt to ctx-based parse fns
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Feb 6, 2020
1 parent d8b0f2d commit fa97ee8
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 202 deletions.
13 changes: 8 additions & 5 deletions parse-rest/arrival-or-departure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// todo: d.JourneyStatus
// todo: d.prognosisType

const createParseArrOrDep = (profile, opt, data, type) => {
const parseArrOrDep = (d) => {
const createParseArrOrDep = (type) => {
const parseArrOrDep = (ctx, d) => {
const {profile, opt} = ctx

const product = {
name: d.name,
number: d.trainNumber,
Expand All @@ -14,16 +16,17 @@ const createParseArrOrDep = (profile, opt, data, type) => {

const res = {
tripId: d.JourneyDetailRef && d.JourneyDetailRef.ref || null,
stop: profile.parseLocation(profile, opt, data, {
stop: profile.parseLocation(ctx, {
type: d.type,
name: d.stop,
id: d.stopid,
extId: d.stopExtId
}),
line: profile.parseLine(profile, opt, data)(product) || null,
line: profile.parseLine(ctx, product) || null,
direction: type === 'departure' && d.direction || null, // todo: arrivals
// todo: is there `d.rtDate` & `d.tz` & `d.rtTz`?
...profile.parseWhen(profile, d.date, null, d.time, d.rtTime, null, !!d.cancelled),
...profile.parseWhen(ctx, d.date, null, d.time, d.rtTime, null, !!d.cancelled),
// todo: use profile.parsePlatform
platform: d.track || null
}

Expand Down
7 changes: 4 additions & 3 deletions parse-rest/hint.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'use strict'

const omit = require('lodash/omit')
// todo: get the original parseHint differently
const _parseHint = require('../parse/hint')

const parseHint = (profile, hint, _) => {
return _parseHint(profile, {
const parseHint = (ctx, hint) => {
return _parseHint(ctx, {
...omit(hint, ['value']),
code: hint.key,
txtN: hint.value
// todo: map hint.routeIdxFrom & hint.routeIdxTo
}, _)
})
}

module.exports = parseHint
137 changes: 66 additions & 71 deletions parse-rest/journey-leg.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,80 @@

const parseIsoDuration = require('parse-iso-duration')

const clone = obj => Object.assign({}, obj)
const parseJourneyLeg = (ctx, l) => { // l = leg
const {profile, opt} = ctx

const createParseJourneyLeg = (profile, opt, data) => {
// j = journey, l = leg
const parseJourneyLeg = (j, l) => {
const orig = l.Origin
const dest = l.Destination
const orig = l.Origin
const dest = l.Destination

const res = {
origin: profile.parseLocation(profile, opt, data, orig),
destination: profile.parseLocation(profile, opt, data, dest)
}

// todo: does `dest.rtAlighting` actually if the arrival is cancelled?
const arr = profile.parseWhen(profile, dest.date, dest.rtDate, dest.time, dest.rtTime, dest.rtTz, !dest.rtAlighting)
res.arrival = arr.when
res.plannedArrival = arr.plannedWhen
res.arrivalDelay = arr.delay
if (arr.prognosedWhen) res.prognosedArrival = arr.prognosedWhen

// todo: does `orig.rtBoarding` actually if the departure is cancelled?
const dep = profile.parseWhen(profile, orig.date, orig.rtDate, orig.time, orig.rtTime, orig.tz, !orig.rtBoarding)
res.departure = dep.when
res.plannedDeparture = dep.plannedWhen
res.departureDelay = dep.delay
if (dep.prognosedWhen) res.prognosedDeparture = dep.prognosedWhen

if (orig.rtBoarding === false || dest.rtAlighting === false) {
res.cancelled = true
Object.defineProperty(res, 'canceled', {value: true})
}
const res = {
origin: profile.parseLocation(ctx, orig),
destination: profile.parseLocation(ctx, dest)
}

if (l.type === 'WALK' || l.type === 'TRSF') {
res.public = true
res.walking = true
res.distance = l.dist || null
// if (l.type === 'TRSF') res.transfer = true
// todo: l.GisRef
res.duration = l.duration ? parseIsoDuration(l.duration) / 1000 | 0 : null
} else if (l.type === 'JNY') {
// todo: does `dest.rtAlighting` actually say if the arrival is cancelled?
const arrPl = profile.parsePlatform(profile, dest.track, dest.rtTrack, !dest.rtAlighting)
res.arrivalPlatform = arrPl.platform
res.plannedArrivalPlatform = arrPl.plannedPlatform
if (arrPl.prognosedPlatform) res.prognosedArrivalPlatform = arrPl.prognosedPlatform

// todo: does `orig.rtBoarding` actually if say the departure is cancelled?
const depPl = profile.parsePlatform(profile, orig.track, orig.rtTrack, !orig.rtBoarding)
res.departurePlatform = depPl.platform
res.plannedDeparturePlatform = depPl.plannedPlatform
if (depPl.prognosedPlatform) res.prognosedDeparturePlatform = depPl.prognosedPlatform

// todo: pull `public` value from `profile.products`
res.tripId = l.JourneyDetailRef && l.JourneyDetailRef.ref || null

const product = {name: l.name, number: l.number, ...l.Product}
res.line = profile.parseLine(profile, opt, data)(product) || null

res.direction = l.direction && profile.parseStationName(l.direction) || null

if (opt.stopovers && l.stops) {
const parse = profile.parseStopover(profile, opt, data)
res.stopovers = l.stops.map(st => parse(null, st))
}
}
// todo: does `dest.rtAlighting` actually if the arrival is cancelled?
const arr = profile.parseWhen(ctx, dest.date, dest.rtDate, dest.time, dest.rtTime, dest.rtTz, !dest.rtAlighting)
res.arrival = arr.when
res.plannedArrival = arr.plannedWhen
res.arrivalDelay = arr.delay
if (arr.prognosedWhen) res.prognosedArrival = arr.prognosedWhen

// todo: does `orig.rtBoarding` actually if the departure is cancelled?
const dep = profile.parseWhen(ctx, orig.date, orig.rtDate, orig.time, orig.rtTime, orig.tz, !orig.rtBoarding)
res.departure = dep.when
res.plannedDeparture = dep.plannedWhen
res.departureDelay = dep.delay
if (dep.prognosedWhen) res.prognosedDeparture = dep.prognosedWhen

if (orig.rtBoarding === false || dest.rtAlighting === false) {
res.cancelled = true
Object.defineProperty(res, 'canceled', {value: true})
}

if (opt.polylines && l.Polyline) {
const parse = profile.parsePolyline(profile, opt, data)
res.polyline = parse(l.Polyline) || null
if (l.type === 'WALK' || l.type === 'TRSF') {
res.public = true
res.walking = true
res.distance = l.dist || null
// if (l.type === 'TRSF') res.transfer = true
// todo: l.GisRef
res.duration = l.duration ? parseIsoDuration(l.duration) / 1000 | 0 : null
} else if (l.type === 'JNY') {
// todo: does `dest.rtAlighting` actually say if the arrival is cancelled?
const arrPl = profile.parsePlatform(ctx, dest.track, dest.rtTrack, !dest.rtAlighting)
res.arrivalPlatform = arrPl.platform
res.plannedArrivalPlatform = arrPl.plannedPlatform
if (arrPl.prognosedPlatform) res.prognosedArrivalPlatform = arrPl.prognosedPlatform

// todo: does `orig.rtBoarding` actually if say the departure is cancelled?
const depPl = profile.parsePlatform(ctx, orig.track, orig.rtTrack, !orig.rtBoarding)
res.departurePlatform = depPl.platform
res.plannedDeparturePlatform = depPl.plannedPlatform
if (depPl.prognosedPlatform) res.prognosedDeparturePlatform = depPl.prognosedPlatform

// todo: pull `public` value from `profile.products`
res.tripId = l.JourneyDetailRef && l.JourneyDetailRef.ref || null

// todo: is it num instead of number?
const product = {name: l.name, number: l.number, ...l.Product}
res.line = profile.parseLine(ctx, product) || null

res.direction = l.direction && profile.parseStationName(ctx, l.direction) || null

if (opt.stopovers && l.stops) {
const parse = profile.parseStopover
res.stopovers = l.stops.map(st => parse(ctx, st, date)) // todo: date
}
}

if (opt.remarks && Array.isArray(l.notes)) {
res.hints = l.notes.map(h => profile.parseHint(profile, h, data))
}
if (opt.polylines && l.Polyline) {
res.polyline = profile.parsePolyline(ctx, l.Polyline) || null
}

return res
if (opt.remarks && Array.isArray(l.notes)) {
res.hints = l.notes.map(h => profile.parseHint(ctx, h))
}

return parseJourneyLeg
return res
}

module.exports = createParseJourneyLeg
module.exports = parseJourneyLeg
34 changes: 14 additions & 20 deletions parse-rest/journey.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
'use strict'

const parseScheduledDays = require('../parse/scheduled-days')
const parseJourney = (ctx, j) => { // j = journey
const {profile, opt} = ctx

const createParseJourney = (profile, opt) => {
const parseLeg = profile.parseJourneyLeg(profile, opt)

const parseJourney = (j) => {
const legs = j.legs.map(leg => parseLeg(j, leg))
const res = {
type: 'journey',
legs,
// todo: refreshToken
// todo: cycle
// todo: remarks?
}

if (opt.scheduledDays && j.serviceDays) {
res.scheduledDays = parseScheduledDays(profile, j.serviceDays.sDaysB)
}
const res = {
type: 'journey',
legs: j.legs.map(leg => profile.parseJourneyLeg(ctx, leg)),
// todo: refreshToken
// todo: cycle
// todo: remarks?
}

return res
if (opt.scheduledDays && j.serviceDays) {
// todo: year
res.scheduledDays = profile.parseScheduledDays(ctx, j.serviceDays.sDaysB, year)
}

return parseJourney
return res
}

module.exports = createParseJourney
module.exports = parseJourney
11 changes: 6 additions & 5 deletions parse-rest/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const {parse} = require('qs')

const leadingZeros = /^0+/

const parseLocation = (profile, opt, _, l) => {
const parseLocation = (ctx, l) => {
const {profile, opt} = ctx

const id = parse(l.id, {delimiter: '@'})
const latitude = 'number' === typeof l.lat ? l.lat : (id.Y ? id.Y / 100000 : null)
const longitude = 'number' === typeof l.long ? l.long : (id.X ? id.X / 100000 : null)
Expand All @@ -19,19 +21,18 @@ const parseLocation = (profile, opt, _, l) => {
const stop = {
type: 'stop',
id: res.id,
name: l.name || id.O ? profile.parseStationName(l.name || id.O) : null,
name: l.name || id.O ? profile.parseStationName(ctx, l.name || id.O) : null,
location: 'number' === typeof res.latitude ? res : null
}

if (opt.linesOfStops && Array.isArray(l.productAtStop)) {
const parse = profile.parseLine(profile, opt, _)
stop.lines = l.productAtStop.map(p => parse({
stop.lines = l.productAtStop.map(p => profile.parseLine(ctx, {
...p, prodCtx: {...p, ...p.prodCtx}
}))
}

if (l.hasMainMast) {
stop.station = parseLocation(profile, opt, _, {
stop.station = parseLocation(ctx, {
type: 'ST',
id: l.mainMastId,
extId: l.mainMastExtId
Expand Down
35 changes: 16 additions & 19 deletions parse-rest/polyline.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
'use strict'

const createParsePolyline = (profile, opt, _) => {
const parsePolyline = (p) => {
const coordinates = []
for (let i = 0; i < p.crd.length; i += 2) {
coordinates.push({
type: 'Feature',
properties: {},
geometry: {
type: 'Point',
coordinates: [p.crd[i], p.crd[i + 1]]
}
})
}
const parsePolyline = (ctx, p) => {
const coordinates = []
for (let i = 0; i < p.crd.length; i += 2) {
coordinates.push({
type: 'Feature',
properties: {},
geometry: {
type: 'Point',
coordinates: [p.crd[i], p.crd[i + 1]]
}
})
}

return {
type: 'FeatureCollection',
features: coordinates
}
return {
type: 'FeatureCollection',
features: coordinates
}
return parsePolyline
}

module.exports = createParsePolyline
module.exports = parsePolyline
66 changes: 32 additions & 34 deletions parse-rest/trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,38 @@ const sortBy = require('lodash/sortBy')
const last = require('lodash/last')
const pick = require('lodash/pick')

const createParseTrip = (profile, opt, data) => {
const parseTrip = (t) => {
const product = t.products && t.products[0] && t.products[0].Product
const direction = t.directions && t.directions[0] && t.directions[0].value

const parseS = profile.parseStopover(profile, opt, data)
const stopovers = sortBy(t.stops, 'routeIdx').map(st => parseS(null, st))
const dep = stopovers[0]
const arr = last(stopovers)

return {
origin: dep.stop,
destination: arr.stop,
line: product ? profile.parseLine(profile, opt, data)(product) : null,
direction: direction || null,

...pick(dep, [
'departure',
'plannedDeparture',
'departureDelay',
'prognosedDeparture'
]),
...pick(arr, [
'arrival',
'plannedArrival',
'arrivalDelay',
'prognosedArrival'
]),

stopovers
}
const parseTrip = (ctx, t) => {
const {profile} = ctx

const product = t.products && t.products[0] && t.products[0].Product
const direction = t.directions && t.directions[0] && t.directions[0].value

const stopovers = sortBy(t.stops, 'routeIdx')
.map(st => profile.parseStopover(ctx, st, date)) // todo: date
const dep = stopovers[0]
const arr = last(stopovers)

return {
origin: dep.stop,
destination: arr.stop,
line: product ? profile.parseLine(ctx, product) : null,
direction: direction || null,

...pick(dep, [
'departure',
'plannedDeparture',
'departureDelay',
'prognosedDeparture'
]),
...pick(arr, [
'arrival',
'plannedArrival',
'arrivalDelay',
'prognosedArrival'
]),

stopovers
}

return parseTrip
}

module.exports = createParseTrip
module.exports = parseTrip
Loading

0 comments on commit fa97ee8

Please sign in to comment.