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 1 commit
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
43 changes: 43 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,49 @@ 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 format = match[2];
var apiUrl = 'https://liberapay.com/' + entity + '/public.json';
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
var receiving = data.receiving.amount;
if (!isNaN(receiving)) {
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit odd to use isNaN on a JSON value. Can you invert this and test receiving === null instead?

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';
}
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] = apiUrl;
Copy link
Member

Choose a reason for hiding this comment

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

Why does this put the API URL on the badge?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a testing mistake, looks like I didn't push my latest change.

sendBadge(format, badgeData);
}
});
}));

// Libscore integration.
camp.route(/^\/libscore\/s\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
Expand Down