Skip to content

Commit

Permalink
add dub score badge service
Browse files Browse the repository at this point in the history
  • Loading branch information
pr7prashant committed Sep 4, 2023
1 parent 6e793f1 commit f13cd98
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
42 changes: 42 additions & 0 deletions services/dub/dub-score.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Joi from 'joi'
import { BaseJsonService } from '../index.js'
import { colorScale } from '../color-formatters.js'

const schema = Joi.object({
score: Joi.number().required(),
})

export default class DubScore extends BaseJsonService {
static category = 'rating'

static route = { base: 'dub/score', pattern: ':packageName' }

static examples = [
{
title: 'DUB SCORE',
namedParams: { packageName: 'vibe-d' },
staticPreview: this.render({ score: 4.5 }),
},
]

static defaultBadgeData = { label: 'SCORE' }

static render({ score }) {
return {
label: 'SCORE',
color: colorScale([1, 2, 3, 4, 5])(score),
message: score,
}
}

async fetch({ packageName }) {
const url = `https://code.dlang.org/api/packages/${packageName}/stats`
return this._requestJson({ schema, url })
}

async handle({ packageName }) {
let { score } = await this.fetch({ packageName })
score = score.toFixed(1)
return this.constructor.render({ score })
}
}
26 changes: 26 additions & 0 deletions services/dub/dub-score.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Joi from 'joi'
import { isWithinRange } from '../test-validators.js'
import { createServiceTester } from '../tester.js'

export const t = await createServiceTester()

const isScoreColor = Joi.equal(
'red',
'orange',
'yellow',
'yellowgreen',
'green',
'brightgreen',
)

t.create('version (valid)')
.get('/vibe-d.json')
.expectBadge({
label: 'SCORE',
message: isWithinRange(0, 5),
color: isScoreColor,
})

t.create('version (not found)')
.get('/not-a-package.json')
.expectBadge({ label: 'SCORE', message: 'not found' })
11 changes: 11 additions & 0 deletions services/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ const isCurrency = withRegex(
/(?=.*\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|0)?(\.\d{1,2})?$/,
)

/**
* Returns a Joi schema that validates strings that represent numbers within the range [start, end].
*
* @param {number} start - The lower bound of the range (inclusive)
* @param {number} end - The upper bound of the range (inclusive)
* @returns {Joi.StringSchema} A Joi schema that uses a regular expression to check if the string is a number in the range
*/
const isWithinRange = (start, end) =>
withRegex(new RegExp(`^([${start}-${end - 1}](.[0-9])?)|(${end}(.0)?)$`))

export {
isSemver,
isVPlusTripleDottedVersion,
Expand Down Expand Up @@ -214,4 +224,5 @@ export {
isOrdinalNumberDaily,
isHumanized,
isCurrency,
isWithinRange,
}

0 comments on commit f13cd98

Please sign in to comment.