Skip to content

Commit

Permalink
add dynamic xml badge support
Browse files Browse the repository at this point in the history
  • Loading branch information
RedSparr0w committed Feb 12, 2018
1 parent 8ad176d commit 948e6be
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"request": "~2.83.0",
"semver": "~5.5.0",
"svgo": "~0.7.1",
"xml2js": "~0.4.16"
"xml2js": "~0.4.16",
"xmldom": "~0.1.27",
"xpath": "~0.0.27"
},
"scripts": {
"coverage:test:js": "nyc node_modules/mocha/bin/_mocha '*.spec.js' 'lib/**/*.spec.js' 'service-tests/**/*.spec.js'",
Expand Down
20 changes: 18 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use strict';

const countBy = require('lodash.countby');
const dom = require('xmldom').DOMParser;
const jp = require('jsonpath');
const path = require('path');
const prettyBytes = require('pretty-bytes');
const queryString = require('query-string');
const semver = require('semver');
const xml2js = require('xml2js');
const xpath = require('xpath');

const { checkErrorResponse } = require('./lib/error-helper');
const analytics = require('./lib/analytics');
Expand Down Expand Up @@ -7464,7 +7466,7 @@ camp.route(/^\/maven-metadata\/v\/(https?)\/(.+\.xml)\.(svg|png|gif|jpg|json)$/,
}));

// User defined sources - JSON response
camp.route(/^\/badge\/dynamic\/(json)\.(svg|png|gif|jpg|json)$/,
camp.route(/^\/badge\/dynamic\/(json|xml)\.(svg|png|gif|jpg|json)$/,
cache({
queryParams: ['uri', 'query', 'prefix', 'suffix'],
handler: function(query, match, sendBadge, request) {
Expand Down Expand Up @@ -7503,8 +7505,22 @@ cache({
var innerText = jsonpath.join(', ');
badgeData.text[1] = (prefix || '') + innerText + (suffix || '');
break;
case 'xml':
data = new dom().parseFromString(data);
var xpathdata = xpath.select(pathExpression, data, true);
try {
badgeData.text[1] = (prefix || '') + (pathExpression.indexOf('@') + 1 ? xpathdata.value : xpathdata.firstChild.data) + (suffix || '');
} catch (e) {
switch (e.toString()){
case "TypeError: Cannot read property 'value' of undefined":
case "TypeError: Cannot read property 'firstChild' of undefined":
throw 'no result';
default:
throw e;
}
}
}
} catch(e) {
} catch (e) {
setBadgeColor(badgeData, 'lightgrey');
badgeData.text[1] = e;
} finally {
Expand Down

0 comments on commit 948e6be

Please sign in to comment.