db-vendo-client/index.js

49 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-06-22 01:39:04 +02:00
'use strict'
const defaultProfile = require('./lib/default-profile')
2017-11-11 22:49:04 +01:00
const request = require('./lib/request')
2016-06-22 02:09:02 +02:00
2017-11-11 22:49:04 +01:00
const createClient = (profile) => {
profile = Object.assign({}, defaultProfile, profile)
2017-11-12 01:23:34 +01:00
if ('string' !== typeof profile.timezone) {
throw new Error('profile.timezone must be a string.')
2016-06-22 01:39:59 +02:00
}
2017-10-03 17:36:42 +02:00
2017-11-12 18:06:16 +01:00
const departures = (station, opt = {}) => {
if ('string' !== typeof station) throw new Error('station must be a string.')
opt = Object.assign({
direction: null, // only show departures heading to this station
duration: 10 // show departures for the next n minutes
}, opt)
opt.when = opt.when || new Date()
const products = profile.formatProducts(opt.products || {})
const dir = opt.direction ? profile.formatStation(opt.direction) : null
return request(profile, {
meth: 'StationBoard',
req: {
type: 'DEP',
date: profile.formatDate(profile, opt.when),
time: profile.formatTime(profile, opt.when),
stbLoc: profile.formatStation(station),
dirLoc: dir,
jnyFltrL: [
profile.formatProducts(opt.products) // todo
],
dur: opt.duration,
getPasslist: false
}
})
.then((d) => {
if (!Array.isArray(d.jnyL)) return [] // todo: throw err?
const parse = profile.parseDeparture(profile, d.locations, d.lines, d.remarks)
return d.jnyL.map(parse)
})
}
return {departures}
2016-06-22 01:39:59 +02:00
}
module.exports = createClient