Skip to content
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

[Gratipay] Replace with error message #1449

Merged
merged 4 commits into from
Mar 4, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 7 additions & 40 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1131,48 +1131,15 @@ cache(function(data, match, sendBadge, request) {

// Gratipay integration.
camp.route(/^\/(?:gittip|gratipay(\/user|\/team|\/project)?)\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var type = match[1]; // eg, `user`.
var user = match[2]; // eg, `dougwilson`.
var format = match[3];
if (type === '') { type = '/user'; }
if (type === '/user') { user = '~' + user; }
var apiUrl = 'https://gratipay.com/' + user + '/public.json';
var badgeData = getBadgeData('receives', data);
cache(function(queryParams, match, sendBadge, request) {
const format = match[3];
const badgeData = getBadgeData('gratipay', queryParams);
if (badgeData.template === 'social') {
badgeData.logo = getLogo('gratipay', data);
badgeData.logo = getLogo('gratipay', queryParams);
}
request(apiUrl, function dealWithData(err, res, buffer) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
var data = JSON.parse(buffer);
// Avoid falsey checks because amounts may be 0
var receiving = isNaN(data.receiving) ? data.taking : data.receiving;
if (!isNaN(receiving)) {
badgeData.text[1] = '$' + metric(receiving) + '/week';
if (receiving === 0) {
badgeData.colorscheme = 'red';
} else if (receiving < 10) {
badgeData.colorscheme = 'yellow';
} else if (receiving < 100) {
badgeData.colorscheme = 'green';
} else {
badgeData.colorscheme = 'brightgreen';
}
sendBadge(format, badgeData);
} else {
badgeData.text[1] = 'anonymous';
sendBadge(format, badgeData);
}
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
badgeData.colorscheme = 'red';
badgeData.text[1] = 'no longer available';
sendBadge(format, badgeData);
}));

// Liberapay integration.
Expand Down
18 changes: 4 additions & 14 deletions service-tests/gratipay.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
'use strict';

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');

const t = new ServiceTester({ id: 'gratipay', title: 'Gratipay' });
module.exports = t;

t.create('Receiving')
.get('/Gratipay.json')
.expectJSONTypes(Joi.object().keys({
name: 'receives',
value: Joi.string().regex(/^\$[0-9]+(\.[0-9]{2})?\/week/)
}));


t.create('Empty')
.get('/Gratipay.json')
.intercept(nock => nock('https://gratipay.com')
.get('/Gratipay/public.json')
.reply(200, { receiving: 0.00 })
)
.expectJSON({ name: 'receives', value: '$0/week'});
.expectJSON({
name: 'gratipay',
value: 'no longer available',
});