-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
72 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,75 @@ | ||
'use strict' | ||
|
||
const LegacyService = require('../legacy-service') | ||
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data') | ||
const Joi = require('joi') | ||
const BaseJsonService = require('../base-json') | ||
|
||
const schema = Joi.object({ | ||
currency_sign: Joi.string().required(), | ||
amount: Joi.number().required(), | ||
multiplier: Joi.string() | ||
.allow('') | ||
.required(), | ||
currency_abbreviation: Joi.string().required(), | ||
}).required() | ||
|
||
module.exports = class Codetally extends BaseJsonService { | ||
static render({ currency, amount, multiplier }) { | ||
return { | ||
message: `${currency}${amount.toFixed(2)} ${multiplier}`, | ||
} | ||
} | ||
|
||
module.exports = class Codetally extends LegacyService { | ||
static get category() { | ||
return 'funding' | ||
} | ||
|
||
static get defaultBadgeData() { | ||
return { | ||
label: 'codetally', | ||
color: '#2E8B57', | ||
} | ||
} | ||
|
||
static get route() { | ||
return { | ||
base: 'codetally', | ||
pattern: ':owner/:repo', | ||
} | ||
} | ||
|
||
static get examples() { | ||
return [ | ||
{ | ||
title: 'Codetally', | ||
previewUrl: 'triggerman722/colorstrap', | ||
pattern: ':owner/:repo', | ||
namedParams: { | ||
owner: 'triggerman722', | ||
repo: 'colorstrap', | ||
}, | ||
staticExample: this.render({ | ||
currency: '$', | ||
amount: 4.68, | ||
multiplier: 'K', | ||
}), | ||
}, | ||
] | ||
} | ||
|
||
static registerLegacyRouteHandler({ camp, cache }) { | ||
camp.route( | ||
/^\/codetally\/(.*)\/(.*)\.(svg|png|gif|jpg|json)$/, | ||
cache((data, match, sendBadge, request) => { | ||
const owner = match[1] // eg, triggerman722. | ||
const repo = match[2] // eg, colorstrap | ||
const format = match[3] | ||
const apiUrl = `http://www.codetally.com/formattedshield/${owner}/${repo}` | ||
const badgeData = getBadgeData('codetally', data) | ||
request(apiUrl, (err, res, buffer) => { | ||
if (err != null) { | ||
badgeData.text[1] = 'inaccessible' | ||
sendBadge(format, badgeData) | ||
return | ||
} | ||
try { | ||
const 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) | ||
} | ||
}) | ||
}) | ||
) | ||
async handle({ owner, repo }) { | ||
const url = `http://www.codetally.com/formattedshield/${owner}/${repo}` | ||
const json = await this._requestJson({ | ||
url, | ||
schema, | ||
errorMessages: { | ||
404: 'repo not found', | ||
503: 'repo not found', | ||
}, | ||
}) | ||
|
||
return this.constructor.render({ | ||
currency: json.currency_sign, | ||
amount: json.amount, | ||
multiplier: json.multiplier, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters