-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User defined XML source badge [dynamic-xml dynamic-json] #1494
Conversation
9ace157
to
7188309
Compare
7188309
to
948e6be
Compare
Generated by 🚫 dangerJS |
server.js
Outdated
} catch (e) { | ||
switch (e.toString()){ | ||
case "TypeError: Cannot read property 'value' of undefined": | ||
case "TypeError: Cannot read property 'firstChild' of undefined": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is not test for this case (TypeError: Cannot read property 'firstChild' of undefined
). Would you like to add it?
server.js
Outdated
case "TypeError: Cannot read property 'firstChild' of undefined": | ||
throw 'no result'; | ||
default: | ||
throw e; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we able to test this part of the code?
server.js
Outdated
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively we could check xpathdata
value and remove try-catch block. What do you think about it?
let value;
if (xpathdata) {
value = (prefix || '') + (pathExpression.indexOf('@') + 1 ? xpathdata.value : xpathdata.firstChild.data) + (suffix || '');
} else {
value = 'no result';
setBadgeColor(badgeData, 'lightgrey');
}
badgeData.text[1] = value;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, a lot more simple!
service-tests/xml.js
Outdated
|
||
const colorsB = mapValues(colorscheme, 'colorB'); | ||
|
||
const t = new ServiceTester({ id: 'badge/dynamic/xml', title: 'User Defined XML Source Data' }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test for querying an attribute (/addon/@id
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Knew i forgot something! Glad you caught this.
server.js
Outdated
|
||
if (err != null || !res || res.statusCode !== 200) | ||
throw 'inaccessible'; | ||
if (checkErrorResponse(badgeData, err, res, 'uri not found')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use checkErrorResponse()
but not sure if uri not found || not found
is the best message for a 404 error on this badge?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe file not found
or resource not found
?
server.js
Outdated
|
||
if (err != null || !res || res.statusCode !== 200) | ||
throw 'inaccessible'; | ||
if (checkErrorResponse(badgeData, err, res, 'uri not found')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe file not found
or resource not found
?
service-tests/xml.js
Outdated
.get('.json?uri=https://services.addons.mozilla.org/en-US/firefox/api/1.5/addon/707078&query=//name') | ||
.expectJSONTypes(Joi.object().keys({ | ||
name: 'custom badge', | ||
value: Joi.string().regex(/^.+,\s.+$/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently //name
finds IndieGala Helper, Danial Nickford, GNU General Public License, version 3.0, Firefox, Firefox for Android
. In .+,\s.+
:
- the first part
.+
(greedy) matchesIndieGala Helper, Danial Nickford, GNU General Public License, version 3.0, Firefox
- the last part
,\s.+
matches, Firefox for Android
So this regular expression does not check if all elements are separated by a comma.
If we change query to query=/addon/compatible_applications/application/name
we could compare result with exact value Firefox, Firefox for Android
. What do you think about it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, That makes more sense.
Wasn't quite sure how to test it, so i just tried to test for the separator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know XPath 1.0 (xpath
is an XPath 1.0 implementation) uses node-sets to store results, which is an unordered collection of nodes. XPath 2.0 uses node sequences, which are an ordered collections (https://www.w3.org/TR/xpath/, https://www.w3.org/TR/xpath20/).
It would be great to use XPath 2.0 implementation, but I haven't found any good one.
still alot of room for improvement to this test
0774385
to
28dc58e
Compare
fc71cbd
to
495f691
Compare
495f691
to
78bc48a
Compare
Thanks, Have updated the front end dynamic badge generator, |
In the example at the bottom, |
Thanks for bringing that up @tooomm. Unfortunately, the dynamic badge already supports a @RedSparr0w Could you clarify which part of the UI you're not liking? |
kept support for uri, incase of any already existing badges.
Ah, I see, because the query format is different. How about providing two examples, one with DATATYPE filled in with json, and the other filled in with xml? The different query will help reinforce the difference. |
This looks great @RedSparr0w! |
@platan Good idea! that makes a lot more sense. |
I published a package to npm. How do I dynamically get the number of weekly/monthly downloads from there using an XML/JSON source ? |
Continuation of #820
Description:
This PR Adds XML support to the dynamic badges.
Other:
added a test for query param specified
changed
uri
param tourl
(uri
still supported)To do:
Examples:
All examples based on this XML
?query=
param/addon/name
/addon/@id
//version
/addon/reviews/@num
//daily_users
Usage:
?a=b
paramsuri
(required)uri=http://path.to.json
query
(required)query=$.version
label
label=ProjectX
colorB
colorB=10ADED
prefix
prefix=v
suffix
suffix=%20dev
logo
logo=twitter
style
style=for-the-badge