From 20ab540fb2967ad81b1e641e5e493dc91cb317fd Mon Sep 17 00:00:00 2001 From: triggerman722 Date: Sat, 23 Sep 2017 21:08:50 -0400 Subject: [PATCH] Added [codetally] (#1069) --- server.js | 26 ++++++++++++++++++++++++++ service-tests/codetally.js | 30 ++++++++++++++++++++++++++++++ try.html | 4 ++++ 3 files changed, 60 insertions(+) create mode 100644 service-tests/codetally.js diff --git a/server.js b/server.js index 6d1ba10735..e4fe29f902 100644 --- a/server.js +++ b/server.js @@ -1150,6 +1150,32 @@ cache(function(data, match, sendBadge, request) { }); })); +// Codetally integration. +camp.route(/^\/codetally\/(.*)\/(.*)\.(svg|png|gif|jpg|json)$/, +cache(function(data, match, sendBadge, request) { + var owner = match[1]; // eg, triggerman722. + var repo = match[2]; // eg, colorstrap + var format = match[3]; + var apiUrl = 'http://www.codetally.com/formattedshield/' + owner + '/' + repo; + var badgeData = getBadgeData('codetally', data); + request(apiUrl, function dealWithData(err, res, buffer) { + if (err != null) { + badgeData.text[1] = 'inaccessible'; + sendBadge(format, badgeData); + return; + } + try { + var data = JSON.parse(buffer); + badgeData.text[1] = " " + data.currency_sign + data.amount + " " + data.multiplier; + badgeData.colorscheme = null; + badgeData.colorB = '#2E8B57'; + sendBadge(format, badgeData); + } catch(e) { + badgeData.text[1] = 'invalid'; + sendBadge(format, badgeData); + } + }); +})); // Bountysource integration. diff --git a/service-tests/codetally.js b/service-tests/codetally.js new file mode 100644 index 0000000000..852fe1a943 --- /dev/null +++ b/service-tests/codetally.js @@ -0,0 +1,30 @@ +'use strict'; + +const Joi = require('joi'); +const ServiceTester = require('./runner/service-tester'); + +const t = new ServiceTester({ id : 'codetally', title: 'Codetally' }); +module.exports = t; + +// This test will extract the currency value from the +// string value response from the server. +// \b = Asset position at word boundary +// \d+ = match a digit [0-9] +// credit for this regex goes to: +// https://stackoverflow.com/questions/38074000/how-to-get-float-value-from-string-using-regex + +t.create('Codetally') + .get('/triggerman722/colorstrap.json') + .expectJSONTypes(Joi.object().keys({ + name: Joi.equal('codetally'), + value: Joi.string().regex(/\b\d+(?:.\d+)?/) + })); + + +t.create('Empty') + .get('/triggerman722/colorstrap.json') + .intercept(nock => nock('http://www.codetally.com') + .get('/formattedshield/triggerman722/colorstrap') + .reply(200, { currency_sign:'$', amount:'0.00', multiplier:'', currency_abbreviation:'CAD' }) + ) + .expectJSON({ name: 'codetally', value: ' $0.00 '}); diff --git a/try.html b/try.html index 9d7ad22318..8eff2cce2f 100644 --- a/try.html +++ b/try.html @@ -702,6 +702,10 @@ Pixel-perfect   Retina-ready   Fast   Consistent   Hackable https://img.shields.io/codeclimate/issues/github/me-and/mdf.svg + Codetally: + + https://img.shields.io/codetally/triggerman722/colorstrap.svg + bitHound: https://img.shields.io/bithound/code/github/rexxars/sse-channel.svg