-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
SpiralArm Consulting Ltd
committed
Nov 10, 2019
0 parents
commit f18804e
Showing
7 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2019 SpiralArm Consulting Ltd, Artonezero Ltd | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Live Departure Board Service (ldbs-json) | ||
This is a node module that will interface to the National Rail Enquiries **Live Departure Boards Service** which powers the Live Departure Boards and the **Live Departure Boards Staff Service** which powers the Staff Version of the Live Departure Boards. More details can be found here: | ||
|
||
* [user service OpenLDBWS](https://lite.realtime.nationalrail.co.uk/OpenLDBWS/) | ||
* [staff service OpenLDBSVWS](https://lite.realtime.nationalrail.co.uk/OpenLDBSVWS/) | ||
|
||
It is designed to deliver the XML data returned by both API's in JSON format. | ||
|
||
|
||
# Using the module | ||
The first step is to install the module into your node project | ||
- **npm install -s ldbs-json** | ||
|
||
Then to test just do the following | ||
``` | ||
const useStaffVersion = false; | ||
const LiveDepartureBoardService = require('ldbs-json'); | ||
const token = "0000-0000-0000-0000"; // put a valid token here | ||
// Set up the options for the call | ||
const options = { | ||
numRows: 2, | ||
crs:"KGX", | ||
}; | ||
// Now do the required query | ||
// note that if in an async function you can use await rather that .then | ||
const api = new LiveDepartureBoardService(token, useStaffVersion); | ||
api.call("GetDepBoardWithDetails", options).then(board => { | ||
console.log(board); | ||
}); | ||
``` | ||
|
||
There are 2 examples ( one STAFF version and one USER version that can be found in the included *examples* directory of the project). | ||
|
||
This module is a continuation from my other module https://www.npmjs.com/package/ldbws-json which was only for use with the USER version of the OpenLDBWS web service. | ||
|
||
|
||
# Acknowledgements | ||
- The LDBS_JSON is powered by National Rail Enquiries. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Simple example code for running the staff version of the DepartureBoard Info | ||
* NB: You will need a valid Token to access the SV version of the api | ||
*/ | ||
const LiveDepartureBoardService = require('../index'); | ||
const token = "0000-0000-0000-0000"; // put a valid token here | ||
|
||
// Set up the options for the call | ||
const options = { | ||
numRows: 2, | ||
crs:"CHI", | ||
time: "2019-11-10T12:15:00", | ||
timeWindow:120, | ||
filterType: "to" | ||
}; | ||
|
||
const api = new LiveDepartureBoardService(token, true); | ||
api.call("GetArrivalBoardByCRS", options).then(board => { | ||
console.log(board); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Simple example code for running the user version of the DepartureBoard Info | ||
* NB: You will need a valid Token to access the normal user version of the api | ||
*/ | ||
const LiveDepartureBoardService = require('../index'); | ||
const token = "0000-0000-0000-0000"; // put a valid token here | ||
|
||
// Set up the options for the call | ||
const options = { | ||
numRows: 2, | ||
crs:"KGX", | ||
}; | ||
|
||
const api = new LiveDepartureBoardService(token, false); | ||
api.call("GetDepBoardWithDetails", options).then(board => { | ||
console.log(board); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict'; | ||
/** | ||
* index.js | ||
* Package main entry point | ||
*/ | ||
|
||
const request = require('request-promise-native'), | ||
parseString = require('xml2js').parseString, | ||
stripNS = require('xml2js').processors.stripPrefix, | ||
DepartureBoardSoap = require('./soap'); | ||
|
||
|
||
|
||
/** | ||
* This is the main LiveDepartureBoardService class | ||
* It will allow access to both the user and staff versions of the Live Departureboard Services | ||
* This access depend on the type of token that you have and which service it is valid for. | ||
* | ||
* For more details on each service see: | ||
* user service - https://lite.realtime.nationalrail.co.uk/OpenLDBWS/ | ||
* staff service - https://lite.realtime.nationalrail.co.uk/OpenLDBSVWS/ | ||
*/ | ||
class LiveDepartureBoardService { | ||
|
||
/** | ||
* Create an instance of the service to allow access to the operations | ||
* @param {*} accessToken this MUST be a valid registered token | ||
* @param {*} staffVersion true to access the staff version (token should be a staff token) or false to access the user version. Defaults to the user version. | ||
*/ | ||
constructor(accessToken = "0000-0000-0000-0000", staffVersion = false) { | ||
this.staffVersion = staffVersion; | ||
this.baseURL = (staffVersion) ? "https://lite.realtime.nationalrail.co.uk/OpenLDBSVWS/ldbsv12.asmx": "https://lite.realtime.nationalrail.co.uk/OpenLDBWS/ldb11.asmx"; | ||
this.targetNamespace = (staffVersion) ? "http://thalesgroup.com/RTTI/2017-10-01/ldbsv/" : "http://thalesgroup.com/RTTI/2017-10-01/ldb/"; | ||
this.accessToken = accessToken; | ||
} | ||
|
||
/** | ||
* Query the LiveDepartureBoardService for the requested data | ||
* @param {String} method - the LDBWS or LDBSVWS to perform | ||
* @param {Object} options - a JSON object consisting of the key/value pairs for the requested method | ||
*/ | ||
async call(method, options) { | ||
const soapCall = new DepartureBoardSoap(this.accessToken, this.targetNamespace, method, options).generateCall(); | ||
const body = await request({ | ||
method: 'POST', | ||
url: this.baseURL, | ||
headers: { | ||
'content-type' : "text/xml" | ||
}, | ||
body: soapCall | ||
}); | ||
return await this._parseResult(body, method); | ||
} | ||
|
||
// Private method to parse result to JSON | ||
_parseResult(body, method) { | ||
return new Promise((resolve, reject) => { | ||
parseString(body, { | ||
tagNameProcessors: [stripNS], | ||
explicitArray : false, | ||
ignoreAttrs : true | ||
}, function(err, result){ | ||
if(!err){ | ||
const data = result.Envelope.Body[`${method}Response`]; | ||
resolve(data); | ||
} else { | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
} | ||
|
||
module.exports = LiveDepartureBoardService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "ldbsvws-json", | ||
"version": "1.0.0", | ||
"engines": { | ||
"node": ">=7.6" | ||
}, | ||
"description": "JSON Interface for the National Rail Enquiries Darwin OpenLDBWS and OpenLBSVWS SOAP service. Requires a valid token to access services.", | ||
"author": { | ||
"name": "SpiralArm Consulting ltd & Artonezero ltd", | ||
"email": "[email protected]" | ||
}, | ||
"license": "MIT", | ||
"homepage": "https://github.com/magnatronus/ldbs-json", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/magnatronus/ldbs-json" | ||
}, | ||
"main": "index.js", | ||
"scripts": { | ||
}, | ||
"keywords": [ | ||
"ldbws", | ||
"ldbs", | ||
"openldbws", | ||
"openldbsvws", | ||
"darwin", | ||
"json" | ||
], | ||
"dependencies": { | ||
"request": "^2.88.0", | ||
"request-promise-native": "^1.0.8", | ||
"xml2js": "^0.4.22" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
/** | ||
* index.js | ||
* Class used to provide the SOAP functionality for the Darwin OpenLDBWS/OpenLDBSVWS calls | ||
* It takes a Token, Namespace, Request and a set of JSON objects and generates the required SOAP call for use with the LDBWS endpoint | ||
*/ | ||
|
||
class DepartureBoardSoap { | ||
|
||
/** | ||
* Create a new SOAP call | ||
* @param {*} token - a valid access token | ||
* @param {*} namespace - the target namespace - depends on staff or user version | ||
* @param {*} request - the operation to perform | ||
* @param {*} options - a JSON object of values to pass to the request | ||
*/ | ||
constructor(token, namespace, request, options){ | ||
this.token = token; | ||
this.namespace = namespace; | ||
this.request = request; | ||
this.options = options; | ||
} | ||
|
||
/** | ||
* Generate the complete SOAP call based on the params passed to the class constructor | ||
*/ | ||
generateCall() { | ||
return `<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:typ="http://thalesgroup.com/RTTI/2013-11-28/Token/types" xmlns:ldb="${this.namespace}">` + | ||
`${this._header()}` + | ||
`${this._payload(this.options)}` + | ||
`</soap:Envelope>`; | ||
} | ||
|
||
// Generate the SOAP payload | ||
_payload(options) { | ||
let body = ``; | ||
Object.keys(options).forEach(function(key){ | ||
let val=""; | ||
if(key == "filterList"){ | ||
options[key].forEach((crs) => { | ||
val += `<crs>${crs}</crs>`; | ||
}); | ||
} else { | ||
val = `${options[key]}` | ||
} | ||
body += `<ldb:${key}>${val}</ldb:${key}>`; | ||
}); | ||
return `<soap:Body><ldb:${this.request}Request>${body}</ldb:${this.request}Request></soap:Body>`; | ||
} | ||
|
||
// generate the SOAP header | ||
_header() { | ||
return `<soap:Header>` + | ||
`<typ:AccessToken>` + | ||
`<typ:TokenValue>${this.token}</typ:TokenValue>` + | ||
`</typ:AccessToken>` + | ||
`</soap:Header>`; | ||
} | ||
} | ||
|
||
module.exports = DepartureBoardSoap; |