Skip to content

Commit

Permalink
Merge pull request #2 from magnatronus/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
magnatronus authored May 5, 2020
2 parents b6560b8 + 59863a3 commit 9f1dc38
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 96 deletions.
121 changes: 121 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# test settings
settings.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.pnp.*
downloads
eslint_out.html
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ldbs-json changelog

## 1.2.0 (May 2020)
Replaced request with node-fetch and added some basic error trapping. Examples updated

## 1.1.2 (Apr 2020)
Add missing namespace attribute for filterList (PR from SimonRice)

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ There are 3 examples ( two STAFF versions and one USER version that can be found

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.


## Updates May 2020
This update has replaced the user of the **request** module and it now uses **node-fetch**. Some simple error checks on the return status of the API are now made and the examples have also been updated.

## New Options for the api.call() method (Dec 2019)
The API will still work as it did but there are 2 new (optional) flags that can be past in when making an API call. The call now :

Expand Down
12 changes: 7 additions & 5 deletions examples/getArrivalBoardByCRS.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* 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 settings = require('../settings.json');
const LiveDepartureBoardService = require('../index');
const token = "0000-0000-0000-0000"; // put a valid token here

// Set up the options for the call
const options = {
Expand All @@ -14,7 +14,9 @@ const options = {
filterType: "to"
};

const api = new LiveDepartureBoardService(token, true);
api.call("GetArrivalBoardByCRS", options).then(board => {
console.log(board);
});
const api = new LiveDepartureBoardService(settings.tokens.staff, true);
api.call("GetArrivalBoardByCRS", options)
.then(board => {
console.log(board);
})
.catch(error => console.error(error));
13 changes: 8 additions & 5 deletions examples/getDepartureBoardWithDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
* 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 settings = require('../settings.json');
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);
});
const api = new LiveDepartureBoardService(settings.tokens.user, false);
api.call("GetDepBoardWithDetails", options)
.then(board => {
console.log(board);
})
.catch(error => console.error(error));
17 changes: 9 additions & 8 deletions examples/getNextDeparturesStaff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,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 settings = require('../settings.json');
const LiveDepartureBoardService = require('../index');
const token = "0000-0000-0000-0000"; // put a valid token here


// Set up the options for the call
const options = {
crs:"SVG",
filterList: ["KGX"],
time: "2020-04-14T08:30:00",
timeWindow:120
time: "2020-05-05T08:00:00",
timeWindow: 120
};

const api = new LiveDepartureBoardService(token, true);
api.call("GetNextDepartures", options).then(board => {
console.log(board);
});
const api = new LiveDepartureBoardService(settings.tokens.staff, true);
api.call("GetNextDepartures", options)
.then(board => {
console.log(board);
})
.catch(error => console.error(error));
13 changes: 8 additions & 5 deletions examples/getNextDeparturesUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* Simple example code for running the user version of the DepartureBoard Info
* NB: You will need a valid Token to access the SV version of the api
*/
const settings = require('../settings.json');
const LiveDepartureBoardService = require('../index');
const token = "0000-0000-0000-0000"; // put a valid token here


// Set up the options for the call
const options = {
Expand All @@ -14,7 +15,9 @@ const options = {
timeOffset: 0
};

const api = new LiveDepartureBoardService(token, false);
api.call("GetNextDepartures", options).then(board => {
console.log(board);
});
const api = new LiveDepartureBoardService(settings.tokens.user, false);
api.call("GetNextDepartures", options)
.then(board => {
console.log(board);
})
.catch(error => console.error(error));
12 changes: 7 additions & 5 deletions examples/getStationList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
* Simple example code for running the staff version of the DepartureBoard Info to access one of the Reference Data Enpoint API calls "GetStationList"
* NB: You will need a valid Token to access the SV version of the api
*/
const settings = require('../settings.json');
const LiveDepartureBoardService = require('../index');
const token = "0000-0000-0000-0000"; // put a valid token here

// Set up the options for the call
const options ={
currentVersion: ''
};

// create a staff version of the API
const api = new LiveDepartureBoardService(token, true);
const api = new LiveDepartureBoardService(settings.tokens.staff, true);

// make sure we set userRef to true
api.call("GetStationList", options, true).then(list => {
console.log(list);
});
api.call("GetStationList", options, true)
.then(list => {
console.log(list);
})
.catch(error => console.error(error));
45 changes: 27 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
* index.js
* Package main entry point
*/

const request = require('request-promise-native'),
parseString = require('xml2js').parseString,
stripNS = require('xml2js').processors.stripPrefix,
DepartureBoardSoap = require('./soap');
const fetch = require('node-fetch');
const parseString = require('xml2js').parseString;
const stripNS = require('xml2js').processors.stripPrefix;
const DepartureBoardSoap = require('./soap');



Expand Down Expand Up @@ -44,19 +43,29 @@ class LiveDepartureBoardService {
* @param {Bool} withAttributes - don't filter the attributes out from the result - default is false
*/
async call(method, options, useRef = false, withAttributes = false, ) {
const soapCall = new DepartureBoardSoap(this.accessToken, (useRef) ? this.refTargetNamespace : this.targetNamespace, method, options).generateCall();
const body = await request({
method: 'POST',
url: (useRef) ? this.refUrl: this.baseURL,
headers: {
'content-type' : "text/xml"
},
body: soapCall
});
if(withAttributes) {
return await this._parseResultWithAttributes(body, method);

try{
const soapCall = new DepartureBoardSoap(this.accessToken, (useRef) ? this.refTargetNamespace : this.targetNamespace, method, options).generateCall();
const response = await fetch((useRef) ? this.refUrl: this.baseURL, {
method: 'post',
body: soapCall,
headers: {'content-type' : "text/xml"}
});
const body = await response.text();
if(response.status !== 200){
throw Error(`API Error ${response.status}: ${response.statusText}`)
}

if(withAttributes) {
return await this._parseResultWithAttributes(body, method);
}
return await this._parseResult(body, method);

} catch(error) {
console.error(error);
return false;
}
return await this._parseResult(body, method);

}

// Private method to parse result to JSON
Expand Down Expand Up @@ -99,4 +108,4 @@ class LiveDepartureBoardService {

}

module.exports = LiveDepartureBoardService;
module.exports = LiveDepartureBoardService;
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ldbs-json",
"version": "1.1.2",
"version": "1.2.0",
"engines": {
"node": ">=7.6"
},
Expand All @@ -16,7 +16,10 @@
"url": "https://github.com/magnatronus/ldbs-json"
},
"main": "index.js",
"scripts": {},
"scripts": {
"lint": "./node_modules/eslint/bin/eslint.js *.js; exit 0",
"lint_report": "./node_modules/eslint/bin/eslint.js -f html -o eslint_out.html .;exit 0"
},
"keywords": [
"ldbws",
"ldbs",
Expand All @@ -26,8 +29,10 @@
"json"
],
"dependencies": {
"request": "^2.88.0",
"request-promise-native": "^1.0.8",
"node-fetch": "^2.6.0",
"xml2js": "^0.4.22"
},
"devDependencies": {
"eslint": "^6.8.0"
}
}
Loading

0 comments on commit 9f1dc38

Please sign in to comment.