parseLine: move mode & products parsing from profiles into main code

This commit is contained in:
Jannis R 2018-03-16 17:00:06 +01:00
parent 83248785e5
commit 08357dfa9d
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
5 changed files with 30 additions and 83 deletions

View file

@ -1,6 +1,5 @@
'use strict'
const _createParseLine = require('../../parse/line')
const _createParseJourney = require('../../parse/journey')
const _formatStation = require('../../format/station')
const {bike} = require('../../format/filters')
@ -35,26 +34,6 @@ const transformJourneysQuery = (query, opt) => {
return query
}
const createParseLine = (profile, operators) => {
const parseLine = _createParseLine(profile, operators)
const parseLineWithMode = (l) => {
const res = parseLine(l)
res.mode = res.product = null
if ('class' in res) {
const data = products.bitmasks[parseInt(res.class)]
if (data) {
res.mode = data.mode
res.product = data.product
}
}
return res
}
return parseLineWithMode
}
const createParseJourney = (profile, stations, lines, remarks) => {
const parseJourney = _createParseJourney(profile, stations, lines, remarks)
@ -112,10 +91,9 @@ const dbProfile = {
transformReqBody,
transformJourneysQuery,
products: products.allProducts,
products: products,
// todo: parseLocation
parseLine: createParseLine,
parseJourney: createParseJourney,
formatStation

View file

@ -1,6 +1,5 @@
'use strict'
const _createParseLine = require('../../parse/line')
const products = require('./products')
const transformReqBody = (body) => {
@ -18,33 +17,13 @@ const transformReqBody = (body) => {
return body
}
const createParseLine = (profile, operators) => {
const parseLine = _createParseLine(profile, operators)
const parseLineWithMode = (l) => {
const res = parseLine(l)
res.mode = res.product = null
if ('class' in res) {
const data = products.bitmasks[parseInt(res.class)]
if (data) {
res.mode = data.mode
res.product = data.product
}
}
return res
}
return parseLineWithMode
}
const insaProfile = {
locale: 'de-DE',
timezone: 'Europe/Berlin',
endpoint: 'http://reiseauskunft.insa.de/bin/mgate.exe',
transformReqBody,
products: products.allProducts,
products: products,
journeyLeg: true,
radar: true

View file

@ -3,7 +3,6 @@
// todo: https://gist.github.com/anonymous/a5fc856bc80ae7364721943243f934f4#file-haf_config_base-properties-L5
// todo: https://gist.github.com/anonymous/a5fc856bc80ae7364721943243f934f4#file-haf_config_base-properties-L47-L234
const _createParseLine = require('../../parse/line')
const _parseLocation = require('../../parse/location')
const _createParseMovement = require('../../parse/movement')
@ -26,26 +25,6 @@ const transformReqBody = (body) => {
return body
}
const createParseLine = (profile, operators) => {
const parseLine = _createParseLine(profile, operators)
const parseLineWithMode = (l) => {
const res = parseLine(l)
res.mode = res.product = null
if ('class' in res) {
const data = products.bitmasks[parseInt(res.class)]
if (data) {
res.mode = data.mode
res.product = data.product
}
}
return res
}
return parseLineWithMode
}
const parseLocation = (profile, l, lines) => {
// ÖBB has some 'stations' **in austria** with no departures/products,
// like station entrances, that are actually POIs.
@ -87,9 +66,8 @@ const oebbProfile = {
endpoint: 'http://fahrplan.oebb.at/bin/mgate.exe',
transformReqBody,
products: products.allProducts,
products: products,
parseLine: createParseLine,
parseLocation,
parseMovement: createParseMovement,

View file

@ -27,18 +27,9 @@ const transformReqBody = (body) => {
const createParseLine = (profile, operators) => {
const parseLine = _createParseLine(profile, operators)
const parseLineWithMode = (l) => {
const parseLineWithMoreDetails = (l) => {
const res = parseLine(l)
res.mode = res.product = null
if ('class' in res) {
const data = products.bitmasks[parseInt(res.class)]
if (data) {
res.mode = data.mode
res.product = data.product
}
}
const details = parseLineName(l.name)
res.symbol = details.symbol
res.nr = details.nr
@ -48,7 +39,7 @@ const createParseLine = (profile, operators) => {
return res
}
return parseLineWithMode
return parseLineWithMoreDetails
}
const parseLocation = (profile, l, lines) => {
@ -154,7 +145,7 @@ const vbbProfile = {
transformReqBody,
products: products.allProducts,
products: products,
parseStationName: shorten,
parseLocation,

View file

@ -2,8 +2,23 @@
const slugg = require('slugg')
// todo: are p.number and p.line ever different?
const createParseLine = (profile, operators) => {
const byBitmask = []
for (let product of profile.products) {
if ('string' !== typeof product.product) {
throw new Error('profile.products[].product must be a string.')
}
if (!Array.isArray(product.bitmasks)) {
throw new Error(product.product + '.bitmasks must be an array.')
}
for (let bitmask of product.bitmasks) {
if ('number' !== typeof bitmask) {
throw new Error(product.product + '.bitmasks[] must be a number.')
}
byBitmask[bitmask] = product
}
}
const parseLine = (p) => {
if (!p) return null // todo: handle this upstream
const res = {
@ -13,8 +28,9 @@ const createParseLine = (profile, operators) => {
public: true
}
// todo: what is p.prodCtx && p.prodCtx.num?
// todo: what is p.number?
// This is terrible, but FPTF demands an ID. Let's pray for VBB to expose an ID.
// This is terrible, but FPTF demands an ID. Let's pray for HaCon to expose an ID.
// todo: find a better way
if (p.line) res.id = slugg(p.line.trim())
else if (p.name) res.id = slugg(p.name.trim())
@ -24,7 +40,12 @@ const createParseLine = (profile, operators) => {
res.productCode = +p.prodCtx.catCode
}
// todo: parse mode, remove from profiles
if ('class' in res) {
// todo: what if `res.class` is the sum of two bitmasks?
const data = byBitmask[parseInt(res.class)]
res.mode = data && data.mode || null
res.product = data && data.product || null
}
if ('number' === typeof p.oprX) {
res.operator = operators[p.oprX] || null