diff --git a/server.js b/server.js index 38fad2c..0668b9f 100644 --- a/server.js +++ b/server.js @@ -2113,6 +2113,54 @@ cache(function(data, match, sendBadge, request) { }); })); +// GitHub license integration. +camp.route(/^\/github\/license\/([^\/]+)\/([^\/]+)\.(svg|png|gif|jpg|json)$/, +cache(function(data, match, sendBadge, request) { + var user = match[1]; // eg, mashape + var repo = match[2]; // eg, apistatus + var format = match[3]; + var apiUrl = 'https://api.github.com/repos/' + user + '/' + repo; + var badgeData = getBadgeData('license', data); + // Using our OAuth App secret grants us 5000 req/hour + // instead of the standard 60 req/hour. + if (serverSecrets) { + apiUrl += '?client_id=' + serverSecrets.gh_client_id + + '&client_secret=' + serverSecrets.gh_client_secret; + } + // Custom user-agent and accept headers are required + // http://developer.github.com/v3/#user-agent-required + // https://developer.github.com/v3/licenses/ + var customHeaders = { + 'User-Agent': 'Shields.io', + 'Accept': 'application/vnd.github.drax-preview+json' + }; + request(apiUrl, { headers: customHeaders }, function(err, res, buffer) { + if (err != null) { + badgeData.text[1] = 'inaccessible'; + sendBadge(format, badgeData); + } + try { + if ((+res.headers['x-ratelimit-remaining']) === 0) { + return; // Hope for the best in the cache. + } else if (res.statusCode === 404) { + throw new Error(); // Show invalid badge in this case + } + var body = JSON.parse(buffer); + if (body.license != null) { + badgeData.text[1] = body.license.name; + badgeData.colorscheme = 'blue'; + sendBadge(format, badgeData); + } else { + badgeData.text[1] = 'unknown'; + sendBadge(format, badgeData); + } + } catch(e) { + badgeData.text[1] = 'invalid'; + sendBadge(format, badgeData); + } + }); +})); + // Chef cookbook integration. camp.route(/^\/cookbook\/v\/(.*)\.(svg|png|gif|jpg|json)$/, cache(function(data, match, sendBadge, request) { diff --git a/try.html b/try.html index 79dfa81..96064af 100644 --- a/try.html +++ b/try.html @@ -514,6 +514,10 @@ Pixel-perfect Retina-ready Fast Consistent Hackable
https://img.shields.io/github/followers/espadrine.svg
https://img.shields.io/github/license/mashape/apistatus.svg
https://img.shields.io/wordpress/plugin/r/akismet.svg
:wq