-
-
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
1 parent
6e793f1
commit f13cd98
Showing
3 changed files
with
79 additions
and
0 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
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 }) | ||
} | ||
} |
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 |
---|---|---|
@@ -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' }) |
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