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

Add [Liberapay] #1251

Merged
merged 27 commits into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
16 changes: 16 additions & 0 deletions lib/all-badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,22 @@ const allBadgeExamples = [
title: 'Gratipay',
previewUri: '/gratipay/project/shields.svg'
},
{
title: 'Liberapay receiving',
previewUri: '/liberapay/receives/Changaco.svg'
},
{
title: 'Liberapay giving',
previewUri: '/liberapay/gives/Changaco.svg'
},
{
title: 'Liberapay patrons',
previewUri: '/liberapay/patrons/Changaco.svg'
},
{
title: 'Liberapay goal progress',
previewUri: '/liberapay/goal/Changaco.svg'
},
{
title: 'Bountysource',
previewUri: '/bountysource/team/mozilla-core/activity.svg'
Expand Down
72 changes: 71 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const {
downloadCount: downloadCountColor,
floorCount: floorCountColor,
version: versionColor,
age: ageColor
age: ageColor,
colorScale
} = require('./lib/color-formatters');
const {
makeColorB,
Expand Down Expand Up @@ -1072,6 +1073,75 @@ cache(function(data, match, sendBadge, request) {
});
}));

// Liberapay integration.
camp.route(/^\/liberapay\/(receives|gives|patrons|goal)\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var type = match[1]; // e.g., 'gives'
var entity = match[2]; // e.g., 'Changaco'
var format = match[3];
var apiUrl = 'https://liberapay.com/' + entity + '/public.json';
// Lock down type
const label = {
'receives': 'receives',
'gives': 'gives',
'patrons': 'patrons',
'goal': 'goal progress',
}[type];
const badgeData = getBadgeData(label, data);
if (badgeData.template === 'social') {
badgeData.logo = getLogo('liberapay', 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);
var value;
var currency;
switch(type) {
case 'receives':
if (data.receiving) {
value = data.receiving.amount;
currency = data.receiving.currency;
badgeData.text[1] = `${metric(value)} ${currency}/week`;
}
break;
case 'gives':
if (data.giving) {
value = data.giving.amount;
currency = data.giving.currency;
badgeData.text[1] = `${metric(value)} ${currency}/week`;
}
break;
case 'patrons':
value = data.npatrons;
badgeData.text[1] = metric(value);
break;
case 'goal':
if (data.goal) {
value = Math.round(data.receiving.amount/data.goal.amount*100);
badgeData.text[1] = `${value}%`;
}
break;
}
if (value != null) {
badgeData.colorscheme = colorScale([0, 10, 100])(value);
sendBadge(format, badgeData);
} else {
badgeData.text[1] = 'anonymous';
badgeData.colorscheme = 'blue';
sendBadge(format, badgeData);
}
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));

// Libscore integration.
camp.route(/^\/libscore\/s\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
Expand Down
53 changes: 53 additions & 0 deletions service-tests/liberapay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

const Joi = require('joi');
const ServiceTester = require('./runner/service-tester');
const isLiberapayTestValues =
Joi.string().regex(/^([0-9]*[1-9][0-9]*(\.[0-9]+)?|[0]+\.[0-9]*[1-9][0-9]*)[ A-Za-z]{4}\/week/); //values must be greater than zero
const {isMetric} = require('./helpers/validators');
const t = new ServiceTester({ id: 'liberapay', title: 'Liberapay' });
module.exports = t;

t.create('Receiving')
.get('/receives/Liberapay.json')
.expectJSONTypes(Joi.object().keys({
name: 'receives',
value: isLiberapayTestValues
}));

t.create('Giving')
.get('/gives/Changaco.json')
.expectJSONTypes(Joi.object().keys({
name: 'gives',
value: isLiberapayTestValues
}));

t.create('Patrons')
.get('/patrons/Liberapay.json')
.expectJSONTypes(Joi.object().keys({
name: 'patrons',
value: isMetric
}));

t.create('Goal Progress')
.get('/goal/Liberapay.json')
.expectJSONTypes(Joi.object().keys({
name: 'goal progress',
value: Joi.string().regex(/^[0-9]+%/)
}));

t.create('No Goal')
.get('/goal/Liberapay.json')
.intercept(nock => nock('https://liberapay.com')
.get('/Liberapay/public.json')
.reply(200, { goal: null })
)
.expectJSON({ name: 'goal progress', value: 'anonymous'});

t.create('Empty')
.get('/receives/Liberapay.json')
.intercept(nock => nock('https://liberapay.com')
.get('/Liberapay/public.json')
.reply(200, { receiving: 0.00 })
)
.expectJSON({ name: 'receives', value: 'anonymous'});