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 5 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
4 changes: 4 additions & 0 deletions lib/all-badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,10 @@ const allBadgeExamples = [
title: 'Gratipay',
previewUri: '/gratipay/project/shields.svg'
},
{
title: 'Liberapay',
previewUri: '/liberapay/Changaco.svg'
},
{
title: 'Bountysource',
previewUri: '/bountysource/team/mozilla-core/activity.svg'
Expand Down
52 changes: 52 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,58 @@ cache(function(data, match, sendBadge, request) {
});
}));

// Liberapay integration.
camp.route(/^\/liberapay\/(.*)\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var entity = match[1]; // eg, 'Changaco'
var type = match[2]
var format = match[3];
var apiUrl = 'https://liberapay.com/' + entity + '/public.json';
if (type == 'gives') {
var badgeData = getBadgeData('gives', data);
} else {
var badgeData = getBadgeData('receives', 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);
// Avoid falsey checks because amounts may be 0
if (type == 'gives') {
var value = data.giving.amount;
} else {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess there is an indentation problem here. And maybe you could make this part simple with a ternary expression, like: var value = type === 'gives' ? data.giving.amount : data.receiving.amount.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Although I may go with a switch since I intend to add more options.

var value = data.receiving.amount;
}
if (value != null) {
badgeData.text[1] = '$' + metric(value) + '/week';
if (value === 0) {
badgeData.colorscheme = 'red';
} else if (value < 10) {
badgeData.colorscheme = 'yellow';
} else if (value < 100) {
badgeData.colorscheme = 'green';
} else {
badgeData.colorscheme = 'brightgreen';
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to accomplish this using colorScale.

colorScale([0, 10, 100])(receiving)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to implement that, is there an example somewhere?

sendBadge(format, badgeData);
} else {
badgeData.text[1] = 'anonymous';
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