add minimal working INSA endpoint

This commit is contained in:
Johannes Filter 2018-02-26 22:14:11 +01:00
parent eb2acaded0
commit 582e5d0ef7
4 changed files with 183 additions and 0 deletions

22
p/insa/example.js Normal file
View file

@ -0,0 +1,22 @@
'use strict'
const createClient = require('../..')
const insaProfile = require('.')
const client = createClient(insaProfile)
// from Magdeburg-Neustadt to Magdeburg-Buckau
client.journeys('008010226', '008013456', {results: 1})
// client.departures('008010226', {duration: 5})
// client.locations('Magdeburg', {results: 2})
// client.location('008010226') // Magdeburg-Neustadt
// client.nearby({
// type: 'location',
// latitude: 52.148842,
// longitude: 11.641705
// }, {distance: 200})
.then((data) => {
console.log(require('util').inspect(data, {depth: null}))
})
.catch(console.error)

62
p/insa/index.js Normal file
View file

@ -0,0 +1,62 @@
'use strict'
const products = require('./products')
const createParseBitmask = require('../../parse/products-bitmask')
const createFormatBitmask = require('../../format/products-bitmask')
const defaultProducts = {
nationalExp: true,
national: true,
regional: true,
suburban: true,
bus: true,
tram: true,
tourismTrain: true,
}
const transformReqBody = (body) => {
body.client = {
type: 'IPH',
id: 'NASA',
v: '4000200',
name: 'nasaPROD',
os: 'iPhone OS 11.2.5'
}
body.ver = '1.11'
body.auth = {aid: "nasa-apps"}
body.lang = 'en' // todo: `de`?
return body
}
const formatProducts = (products) => {
products = Object.assign(Object.create(null), defaultProducts, products)
return {
type: 'PROD',
mode: 'INC',
value: formatBitmask(products) + ''
}
}
const formatBitmask = createFormatBitmask(products)
const insaProfile = {
locale: 'de-DE',
timezone: 'Europe/Berlin',
endpoint: 'http://reiseauskunft.insa.de/bin/mgate.exe',
products: products.allProducts,
transformReqBody,
defaultProducts,
parseProducts: createParseBitmask(products.bitmasks),
formatProducts
// todo: journeyLeg?
// todo: radar?
}
module.exports = insaProfile;

81
p/insa/products.js Normal file
View file

@ -0,0 +1,81 @@
'use strict'
// TODO Jannis R.: DRY
const p = {
nationalExp: {
bitmask: 1,
name: 'InterCityExpress',
short: 'ICE',
mode: 'train',
product: 'nationalExp'
},
national: {
bitmask: 2,
name: 'InterCity & EuroCity',
short: 'IC/EC',
mode: 'train',
product: 'national'
},
regional: {
bitmask: 8,
name: 'RegionalExpress & RegionalBahn',
short: 'RE/RB',
mode: 'train',
product: 'regional'
},
suburban: {
bitmask: 16,
name: 'S-Bahn',
short: 'S',
mode: 'train',
product: 'suburban'
},
tram: {
bitmask: 32,
name: 'Tram',
short: 'T',
mode: 'train',
product: 'tram'
},
bus: {
bitmask: 64+128,
name: 'Bus',
short: 'B',
mode: 'bus',
product: 'bus'
},
tourismTrain: {
bitmask: 256,
name: 'Tourism Train',
short: 'TT',
mode: 'train',
product: 'tourismTrain'
},
unknown: {
bitmask: 0,
name: 'unknown',
short: '?',
product: 'unknown'
}
}
p.bitmasks = []
p.bitmasks[1] = p.nationalExp
p.bitmasks[2] = p.national
p.bitmasks[8] = p.regional
p.bitmasks[16] = p.suburban
p.bitmasks[32] = p.tram
p.bitmasks[64+128] = p.bus
p.bitmasks[256] = p.tourismTrain
p.allProducts = [
p.nationalExp,
p.national,
p.regional,
p.suburban,
p.tram,
p.bus,
p.tourismTrain
]
module.exports = p

18
p/insa/readme.md Normal file
View file

@ -0,0 +1,18 @@
# INSA profile for `hafas-client`
The [Nahverkehr Sachsen-Anhalt (NASA)](https://de.wikipedia.org/wiki/Nahverkehrsservice_Sachsen-Anhalt) offers [Informationssystem Nahverkehr Sachsen-Anhalt (INSA)](https://insa.de) to distribute their public transport data.
## Usage
```js
const createClient = require('hafas-client')
const insaProfile = require('hafas-client/p/insa')
// create a client with INSA profile
const client = createClient(insaProfile)
```
## Customisations
- parses *INSA*-specific products (such as *Tourism Train*)