-
-
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.
* add PUB points badge * update PUB points badge * update PUB points badge Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
- Loading branch information
1 parent
3e2c9b2
commit 377a036
Showing
2 changed files
with
78 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,55 @@ | ||
import Joi from 'joi' | ||
import { floorCount } from '../color-formatters.js' | ||
import { BaseJsonService } from '../index.js' | ||
import { nonNegativeInteger } from '../validators.js' | ||
|
||
const documentation = `<p>A measure of quality. This includes several dimensions of quality such as code style, platform support, and maintainability.</p>` | ||
|
||
const keywords = ['dart', 'flutter'] | ||
|
||
const schema = Joi.object({ | ||
grantedPoints: nonNegativeInteger, | ||
maxPoints: nonNegativeInteger, | ||
}).required() | ||
|
||
const title = 'Pub Points' | ||
|
||
export default class PubPoints extends BaseJsonService { | ||
static category = 'rating' | ||
|
||
static route = { base: 'pub/points', pattern: ':packageName' } | ||
|
||
static examples = [ | ||
{ | ||
title, | ||
keywords, | ||
documentation, | ||
namedParams: { packageName: 'analysis_options' }, | ||
staticPreview: this.render({ grantedPoints: 120, maxPoints: 130 }), | ||
}, | ||
] | ||
|
||
static defaultBadgeData = { label: 'points' } | ||
|
||
static render({ grantedPoints, maxPoints }) { | ||
return { | ||
label: 'points', | ||
message: `${grantedPoints}/${maxPoints}`, | ||
color: floorCount((grantedPoints / maxPoints) * 100, 40, 60, 80), | ||
} | ||
} | ||
|
||
async fetch({ packageName }) { | ||
return this._requestJson({ | ||
schema, | ||
url: `https://pub.dev/api/packages/${packageName}/score`, | ||
}) | ||
} | ||
|
||
async handle({ packageName }) { | ||
const score = await this.fetch({ packageName }) | ||
const grantedPoints = score.grantedPoints | ||
const maxPoints = score.maxPoints | ||
return this.constructor.render({ grantedPoints, maxPoints }) | ||
} | ||
} |
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,23 @@ | ||
import Joi from 'joi' | ||
import { createServiceTester } from '../tester.js' | ||
|
||
export const t = await createServiceTester() | ||
|
||
t.create('pub points (valid)') | ||
.get('/analysis_options.json') | ||
.expectBadge({ | ||
label: 'points', | ||
message: Joi.string().regex(/^\d+\/130$/), | ||
}) | ||
|
||
t.create('pub points (not found)').get('/analysisoptions.json').expectBadge({ | ||
label: 'points', | ||
message: 'not found', | ||
color: 'red', | ||
}) | ||
|
||
t.create('pub points (invalid)').get('/analysis-options.json').expectBadge({ | ||
label: 'points', | ||
message: 'invalid', | ||
color: 'lightgrey', | ||
}) |