Migrate [Codetally] to new service model (#2548)
This commit is contained in:
committed by
Paul Melnikow
parent
b4b29ab0f7
commit
6c9fc0de79
@@ -1,16 +1,39 @@
|
||||
'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',
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,40 +41,35 @@ module.exports = class Codetally extends LegacyService {
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const Joi = require('joi')
|
||||
const ServiceTester = require('../service-tester')
|
||||
|
||||
const t = (module.exports = new ServiceTester({
|
||||
id: 'codetally',
|
||||
title: 'Codetally',
|
||||
}))
|
||||
const t = (module.exports = require('../create-service-tester')())
|
||||
|
||||
// This test will extract the currency value from the
|
||||
// string value response from the server.
|
||||
@@ -37,3 +32,8 @@ t.create('Empty')
|
||||
})
|
||||
)
|
||||
.expectJSON({ name: 'codetally', value: '$0.00' })
|
||||
|
||||
t.create('Non existent')
|
||||
.get('/not/real.json')
|
||||
.timeout(10000)
|
||||
.expectJSON({ name: 'codetally', value: 'repo not found' })
|
||||
|
||||
Reference in New Issue
Block a user