From 2aa9dfdd4568985b9d60d8182acbac04f7f7009d Mon Sep 17 00:00:00 2001 From: Julius Tens Date: Thu, 15 Mar 2018 09:33:31 +0100 Subject: [PATCH] support pricing information --- p/nahsh/example.js | 2 +- p/nahsh/index.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/p/nahsh/example.js b/p/nahsh/example.js index 0eb94f15..ada76608 100644 --- a/p/nahsh/example.js +++ b/p/nahsh/example.js @@ -6,7 +6,7 @@ const nahshProfile = require('.') const client = createClient(nahshProfile) // Flensburg Hbf to Kiel Hbf -client.journeys('8000103', '8000199', {results: 10}) +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}) diff --git a/p/nahsh/index.js b/p/nahsh/index.js index 7388c701..b8f7a5ef 100644 --- a/p/nahsh/index.js +++ b/p/nahsh/index.js @@ -4,6 +4,7 @@ const createParseBitmask = require('../../parse/products-bitmask') const createFormatBitmask = require('../../format/products-bitmask') const _createParseLine = require('../../parse/line') const _parseLocation = require('../../parse/location') +const _createParseJourney = require('../../parse/journey') const products = require('./products') @@ -62,6 +63,49 @@ const createParseLine = (profile, operators) => { return parseLineWithMode } +const createParseJourney = (profile, stations, lines, remarks) => { + const parseJourney = _createParseJourney(profile, stations, lines, remarks) + + const parseJourneyWithTickets = (j) => { + const res = parseJourney(j) + + if ( + j.trfRes && + Array.isArray(j.trfRes.fareSetL) && + j.trfRes.fareSetL.length > 0 + ) { + res.tickets = [] + + for(let t of j.trfRes.fareSetL){ + const tariff = t.desc + if(!tariff || !Array.isArray(t.fareL)) continue + for(let v of t.fareL){ + const variant = v.name + if(!variant) continue + const ticket = { + name: [tariff, variant].join(' - '), + tariff, + variant + } + if(v.prc && Number.isInteger(v.prc) && v.cur){ + ticket.amount = v.prc/100 + ticket.currency = v.cur + } + else{ + ticket.amount = null + ticket.hint = 'No pricing information available.' + } + res.tickets.push(ticket) + } + } + } + + return res + } + + return parseJourneyWithTickets +} + const defaultProducts = { nationalExp: true, national: true, @@ -95,6 +139,7 @@ const nahshProfile = { parseProducts: createParseBitmask(products.allProducts, defaultProducts), parseLine: createParseLine, parseLocation, + parseJourney: createParseJourney, formatProducts,