responseparser.js | |
---|---|
var xml2js = require('../vendor/node-xml2js/lib/xml2js'); | |
Tidies up and optionally parses the XML API response The
| function ResponseParser(options) {
this.format = options.format;
this.logger = options.logger;
} |
Callback for parsing the XML response return from the API and converting it to JSON and handing control back to the caller.
| ResponseParser.prototype.parse = function (callback, err, response) {
var parser;
if (err) {
callback(err);
return;
}
console.dir(arguments);
if (this.format === 'XML') {
callback(null, response);
return;
}
parser = new xml2js.Parser(); |
Check the request was authorised | if (response.statusCode === 401) {
callback(response);
return;
}
console.log(response);
this.logger.info('Parsing XML response from API');
parser.on('end', function (result) { |
Manually remove the xml namespace bits | delete result['xmlns:xsi'];
delete result['xsi:noNamespaceSchemaLocation'];
if (result.status === 'error') {
callback(result.error);
}
else {
callback(null, result);
}
});
parser.parseString(response + '');
};
module.exports = ResponseParser;
|