Skip to content

Commit

Permalink
added ability to stop attribute removal from result data
Browse files Browse the repository at this point in the history
  • Loading branch information
magnatronus committed Dec 14, 2019
1 parent 90f91b8 commit 7b6b805
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
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.1.1 (Dec 2019)
By default attributes are removed from the returned data Now they can be included.

## 1.1.0 (Dec 2019)
Updated to allow for access to Staff Reference Data Endpoint calls

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ 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.

## 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 :

- api.call(*method*, *options*, *useRef*, *withAttributes*)

The parameters **method** and **options** are the same as they were but there are 2 new options **useRef** and **withAttributes** (both default to false so have no impact if not specified).

- **useRef** can be set to true so that a Staff API call can access the Reference Data Endpoint
- **withAttributes** can be set to true to stop the attributes being stripped from the result. **NB: setting this to true will change the JSON data structure returned**.



# Acknowledgements
- The LDBS-JSON module is powered by National Rail Enquiries.
28 changes: 26 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ class LiveDepartureBoardService {
* 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
* @param {Bool} userRef - only applies for the staff version of the API and allows access to those calls that use the reference data endpoint
* @param {Bool} useRef - only applies for the staff version of the API and allows access to those calls that use the reference data endpoint - default is false
* @param {Bool} withAttributes - don't filter the attributes out from the result - default is false
*/
async call(method, options, useRef = 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',
Expand All @@ -52,6 +53,9 @@ class LiveDepartureBoardService {
},
body: soapCall
});
if(withAttributes) {
return await this._parseResultWithAttributes(body, method);
}
return await this._parseResult(body, method);
}

Expand All @@ -73,6 +77,26 @@ class LiveDepartureBoardService {
});
}

// Private method to parse result to JSON and return all data including attributes
// this version will return the the content text as "_" and all associated attribute in "$"
// for more information see https://www.npmjs.com/package/xml2js
_parseResultWithAttributes(body, method) {
return new Promise((resolve, reject) => {
parseString(body, {
tagNameProcessors: [stripNS],
explicitArray : false,
ignoreAttrs : false
}, function(err, result){
if(!err){
const data = result.Envelope.Body[`${method}Response`];
resolve(data);
} else {
reject(err);
}
});
});
}

}

module.exports = LiveDepartureBoardService;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ldbs-json",
"version": "1.1.0",
"version": "1.1.1",
"engines": {
"node": ">=7.6"
},
Expand Down

0 comments on commit 7b6b805

Please sign in to comment.