From c2ccc462ce5197dabdf349345de16f2cccc95d87 Mon Sep 17 00:00:00 2001 From: Adam Anderly Date: Mon, 3 Nov 2014 11:28:43 -0600 Subject: [PATCH 1/7] Added support for wordpress plugin version, total downloads and rating. --- server.js | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/server.js b/server.js index b53c41b37df59..feeca549cf653 100644 --- a/server.js +++ b/server.js @@ -2102,6 +2102,108 @@ cache(function(data, match, sendBadge, request) { }); })); +// wordpress plugin version integration. +camp.route(/^\/wp\/v\/(.*)\.(svg|png|gif|jpg|json)$/, +cache(function(data, match, sendBadge, request) { + var plugin = match[1]; // eg, `localeval`. + var format = match[2]; + var apiUrl = 'http://api.wordpress.org/plugins/info/1.0/' + plugin + '.json'; + var badgeData = getBadgeData('plugin', data); + request(apiUrl, function(err, res, buffer) { + if (err != null) { + badgeData.text[1] = 'inaccessible'; + sendBadge(format, badgeData); + } + try { + var data = JSON.parse(buffer); + var version = data.version; + badgeData.text[1] = 'v' + version; + if (version[0] === '0') { + badgeData.colorscheme = 'orange'; + } else { + badgeData.colorscheme = 'blue'; + } + sendBadge(format, badgeData); + } catch(e) { + badgeData.text[1] = 'invalid'; + sendBadge(format, badgeData); + } + }); +})); + +// wordpress plugin downloads integration. +camp.route(/^\/wp\/dt\/(.*)\.(svg|png|gif|jpg|json)$/, +cache(function(data, match, sendBadge, request) { + var plugin = match[1]; // eg, `localeval`. + var format = match[2]; + var apiUrl = 'http://api.wordpress.org/plugins/info/1.0/' + plugin + '.json'; + var badgeData = getBadgeData('downloads', data); + request(apiUrl, function(err, res, buffer) { + if (err != null) { + badgeData.text[1] = 'inaccessible'; + sendBadge(format, badgeData); + return; + } + try { + var total = JSON.parse(buffer).downloaded; + } catch(e) { + badgeData.text[1] = 'invalid'; + sendBadge(format, badgeData); + return; + } + badgeData.text[1] = metric(total); + if (monthly === 0) { + badgeData.colorscheme = 'red'; + } else if (monthly < 10) { + badgeData.colorscheme = 'yellow'; + } else if (monthly < 100) { + badgeData.colorscheme = 'yellowgreen'; + } else if (monthly < 1000) { + badgeData.colorscheme = 'green'; + } else { + badgeData.colorscheme = 'brightgreen'; + } + sendBadge(format, badgeData); + }); +})); + +// wordpress plugin rating integration. +camp.route(/^\/wp\/r\/(.*)\.(svg|png|gif|jpg|json)$/, +cache(function(data, match, sendBadge, request) { + var plugin = match[1]; // eg, `localeval`. + var format = match[2]; + var apiUrl = 'http://api.wordpress.org/plugins/info/1.0/' + plugin + '.json'; + var badgeData = getBadgeData('rating', data); + request(apiUrl, function(err, res, buffer) { + if (err != null) { + badgeData.text[1] = 'inaccessible'; + sendBadge(format, badgeData); + return; + } + try { + var rating = JSON.parse(buffer).rating; + rating = (rating/100)*5; + } catch(e) { + badgeData.text[1] = 'invalid'; + sendBadge(format, badgeData); + return; + } + badgeData.text[1] = metric(rating) + ' stars'; + if (rating === 0) { + badgeData.colorscheme = 'red'; + } else if (rating < 2) { + badgeData.colorscheme = 'yellow'; + } else if (rating < 3) { + badgeData.colorscheme = 'yellowgreen'; + } else if (rating < 4) { + badgeData.colorscheme = 'green'; + } else { + badgeData.colorscheme = 'brightgreen'; + } + sendBadge(format, badgeData); + }); +})); + // Any badge. camp.route(/^\/(:|badge\/)(([^-]|--)+)-(([^-]|--)+)-(([^-]|--)+)\.(svg|png|gif|jpg)$/, function(data, match, end, ask) { From dbbb86b03e728f7a2d3adef7a77c049d3f3d3d38 Mon Sep 17 00:00:00 2001 From: Adam Anderly Date: Mon, 3 Nov 2014 18:02:54 -0600 Subject: [PATCH 2/7] Fix for wp dt. --- server.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server.js b/server.js index feeca549cf653..0fa22fbea695a 100644 --- a/server.js +++ b/server.js @@ -2152,13 +2152,13 @@ cache(function(data, match, sendBadge, request) { return; } badgeData.text[1] = metric(total); - if (monthly === 0) { + if (total === 0) { badgeData.colorscheme = 'red'; - } else if (monthly < 10) { + } else if (total < 10) { badgeData.colorscheme = 'yellow'; - } else if (monthly < 100) { + } else if (total < 100) { badgeData.colorscheme = 'yellowgreen'; - } else if (monthly < 1000) { + } else if (total < 1000) { badgeData.colorscheme = 'green'; } else { badgeData.colorscheme = 'brightgreen'; From 4aa562b3c8847537f910aea749c7370257d745d0 Mon Sep 17 00:00:00 2001 From: Adam Anderly Date: Tue, 4 Nov 2014 00:12:12 -0600 Subject: [PATCH 3/7] Made route names more explicit and nested plugin under wordpress. Added support for wordpress version compatibility. --- server.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 0fa22fbea695a..3239f0208967c 100644 --- a/server.js +++ b/server.js @@ -2103,7 +2103,7 @@ cache(function(data, match, sendBadge, request) { })); // wordpress plugin version integration. -camp.route(/^\/wp\/v\/(.*)\.(svg|png|gif|jpg|json)$/, +camp.route(/^\/wordpress\/plugin\/v\/(.*)\.(svg|png|gif|jpg|json)$/, cache(function(data, match, sendBadge, request) { var plugin = match[1]; // eg, `localeval`. var format = match[2]; @@ -2132,7 +2132,7 @@ cache(function(data, match, sendBadge, request) { })); // wordpress plugin downloads integration. -camp.route(/^\/wp\/dt\/(.*)\.(svg|png|gif|jpg|json)$/, +camp.route(/^\/wordpress\/plugin\/dt\/(.*)\.(svg|png|gif|jpg|json)$/, cache(function(data, match, sendBadge, request) { var plugin = match[1]; // eg, `localeval`. var format = match[2]; @@ -2168,7 +2168,7 @@ cache(function(data, match, sendBadge, request) { })); // wordpress plugin rating integration. -camp.route(/^\/wp\/r\/(.*)\.(svg|png|gif|jpg|json)$/, +camp.route(/^\/wordpress\/plugin\/r\/(.*)\.(svg|png|gif|jpg|json)$/, cache(function(data, match, sendBadge, request) { var plugin = match[1]; // eg, `localeval`. var format = match[2]; @@ -2204,6 +2204,51 @@ cache(function(data, match, sendBadge, request) { }); })); +// wordpress version support integration. +camp.route(/^\/wordpress\/v\/(.*)\.(svg|png|gif|jpg|json)$/, +cache(function(data, match, sendBadge, request) { + var plugin = match[1]; // eg, `localeval`. + var format = match[2]; + var apiUrl = 'http://api.wordpress.org/plugins/info/1.0/' + plugin + '.json'; + var badgeData = getBadgeData('wordpress', data); + request(apiUrl, function(err, res, buffer) { + if (err != null) { + badgeData.text[1] = 'inaccessible'; + sendBadge(format, badgeData); + } + try { + var data = JSON.parse(buffer); + var pluginVersion = data.version; + if (data.tested) { + var testedVersion = data.tested; + badgeData.text[1] = testedVersion; + var coreUrl = 'https://api.wordpress.org/core/version-check/1.7/'; + request(coreUrl, function(err, res, response) { + var versions = JSON.parse(response).offers.map(function(v) { + return v.version + }); + if (err != null) { sendBadge(format, badgeData); return; } + var svTestedVersion = testedVersion.split('.').length == 2 ? testedVersion += '.0' : testedVersion; + var svVersion = versions[0].split('.').length == 2 ? versions[0] += '.0' : versions[0]; + if (testedVersion == versions[0] || semver.gtr(svTestedVersion, svVersion)) { + badgeData.colorscheme = 'brightgreen'; + } else if (versions.indexOf(testedVersion) != -1) { + badgeData.colorscheme = 'orange'; + } else { + badgeData.colorscheme = 'yellow'; + } + sendBadge(format, badgeData); + }); + } else { + sendBadge(format, badgeData); + } + } catch(e) { + badgeData.text[1] = 'invalid'; + sendBadge(format, badgeData); + } + }); +})); + // Any badge. camp.route(/^\/(:|badge\/)(([^-]|--)+)-(([^-]|--)+)-(([^-]|--)+)\.(svg|png|gif|jpg)$/, function(data, match, end, ask) { From df3554c24470fb0c0be944ff96b7c7889a472cf1 Mon Sep 17 00:00:00 2001 From: Adam Anderly Date: Tue, 4 Nov 2014 00:32:03 -0600 Subject: [PATCH 4/7] Rounding plugin rating to 1 decimal place to match wordpress plugin repository. --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 3239f0208967c..0064b9159799f 100644 --- a/server.js +++ b/server.js @@ -2188,7 +2188,7 @@ cache(function(data, match, sendBadge, request) { sendBadge(format, badgeData); return; } - badgeData.text[1] = metric(rating) + ' stars'; + badgeData.text[1] = metric(Math.round(rating * 10) / 10) + ' stars'; if (rating === 0) { badgeData.colorscheme = 'red'; } else if (rating < 2) { From 5fa80c77adb5f077af4b7249a73659f3622399fc Mon Sep 17 00:00:00 2001 From: Adam Anderly Date: Tue, 4 Nov 2014 00:39:32 -0600 Subject: [PATCH 5/7] Fix to strip alpha non-digit characters from wordpress version. --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 0064b9159799f..2373c77c846f8 100644 --- a/server.js +++ b/server.js @@ -2220,7 +2220,7 @@ cache(function(data, match, sendBadge, request) { var data = JSON.parse(buffer); var pluginVersion = data.version; if (data.tested) { - var testedVersion = data.tested; + var testedVersion = data.tested.replace(/[^0-9.]/g,''); badgeData.text[1] = testedVersion; var coreUrl = 'https://api.wordpress.org/core/version-check/1.7/'; request(coreUrl, function(err, res, response) { From f8c7960035cea161e363b5a8d4b019d6dbc1b57d Mon Sep 17 00:00:00 2001 From: Adam Anderly Date: Tue, 4 Nov 2014 01:38:46 -0600 Subject: [PATCH 6/7] Updated download color thresholds. --- server.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server.js b/server.js index 2373c77c846f8..bf38a66a4e876 100644 --- a/server.js +++ b/server.js @@ -2154,11 +2154,11 @@ cache(function(data, match, sendBadge, request) { badgeData.text[1] = metric(total); if (total === 0) { badgeData.colorscheme = 'red'; - } else if (total < 10) { - badgeData.colorscheme = 'yellow'; } else if (total < 100) { - badgeData.colorscheme = 'yellowgreen'; + badgeData.colorscheme = 'yellow'; } else if (total < 1000) { + badgeData.colorscheme = 'yellowgreen'; + } else if (total < 10000) { badgeData.colorscheme = 'green'; } else { badgeData.colorscheme = 'brightgreen'; From d3f8cdb1c2aac6076a743ddc3476e322e6f24250 Mon Sep 17 00:00:00 2001 From: Adam Anderly Date: Tue, 4 Nov 2014 15:13:59 -0600 Subject: [PATCH 7/7] Added wordpress functional examples in comments. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added ‘ total’ to end of /wordpress/plugin/dt/ badgeText Added ‘ tested’ to end of /wordpress/v/ badgeText to better reflect meaning. --- server.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index bf38a66a4e876..df9b5b3428044 100644 --- a/server.js +++ b/server.js @@ -2103,6 +2103,7 @@ cache(function(data, match, sendBadge, request) { })); // wordpress plugin version integration. +// example: https://img.shields.io/wordpress/plugin/v/akismet.svg for https://wordpress.org/plugins/akismet camp.route(/^\/wordpress\/plugin\/v\/(.*)\.(svg|png|gif|jpg|json)$/, cache(function(data, match, sendBadge, request) { var plugin = match[1]; // eg, `localeval`. @@ -2132,6 +2133,7 @@ cache(function(data, match, sendBadge, request) { })); // wordpress plugin downloads integration. +// example: https://img.shields.io/wordpress/plugin/dt/akismet.svg for https://wordpress.org/plugins/akismet camp.route(/^\/wordpress\/plugin\/dt\/(.*)\.(svg|png|gif|jpg|json)$/, cache(function(data, match, sendBadge, request) { var plugin = match[1]; // eg, `localeval`. @@ -2151,7 +2153,7 @@ cache(function(data, match, sendBadge, request) { sendBadge(format, badgeData); return; } - badgeData.text[1] = metric(total); + badgeData.text[1] = metric(total) + ' total'; if (total === 0) { badgeData.colorscheme = 'red'; } else if (total < 100) { @@ -2168,6 +2170,7 @@ cache(function(data, match, sendBadge, request) { })); // wordpress plugin rating integration. +// example: https://img.shields.io/wordpress/plugin/r/akismet.svg for https://wordpress.org/plugins/akismet camp.route(/^\/wordpress\/plugin\/r\/(.*)\.(svg|png|gif|jpg|json)$/, cache(function(data, match, sendBadge, request) { var plugin = match[1]; // eg, `localeval`. @@ -2205,6 +2208,7 @@ cache(function(data, match, sendBadge, request) { })); // wordpress version support integration. +// example: https://img.shields.io/wordpress/v/akismet.svg for https://wordpress.org/plugins/akismet camp.route(/^\/wordpress\/v\/(.*)\.(svg|png|gif|jpg|json)$/, cache(function(data, match, sendBadge, request) { var plugin = match[1]; // eg, `localeval`. @@ -2221,7 +2225,7 @@ cache(function(data, match, sendBadge, request) { var pluginVersion = data.version; if (data.tested) { var testedVersion = data.tested.replace(/[^0-9.]/g,''); - badgeData.text[1] = testedVersion; + badgeData.text[1] = testedVersion + ' tested'; var coreUrl = 'https://api.wordpress.org/core/version-check/1.7/'; request(coreUrl, function(err, res, response) { var versions = JSON.parse(response).offers.map(function(v) {