Skip to content

Commit

Permalink
add [PUB] popularity badge (#7920)
Browse files Browse the repository at this point in the history
* add PUB popularity badge

* update PUB popularity badge

* update PUB popularity badge

Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
  • Loading branch information
G1Joshi and repo-ranger[bot] committed May 8, 2022
1 parent 4c8a724 commit 3e2c9b2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
53 changes: 53 additions & 0 deletions services/pub/pub-popularity.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import Joi from 'joi'
import { floorCount } from '../color-formatters.js'
import { BaseJsonService } from '../index.js'

const documentation = `<p>A measure of how many developers use a package, providing insight into what other developers are using.</p>`

const keywords = ['dart', 'flutter']

const schema = Joi.object({
popularityScore: Joi.number().min(0).max(1).required(),
}).required()

const title = 'Pub Popularity'

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

static route = { base: 'pub/popularity', pattern: ':packageName' }

static examples = [
{
title,
keywords,
documentation,
namedParams: { packageName: 'analysis_options' },
staticPreview: this.render({ popularityScore: 0.9 }),
},
]

static defaultBadgeData = { label: 'popularity' }

static render({ popularityScore }) {
const roundedScore = Math.round(popularityScore * 100)
return {
label: 'popularity',
message: `${roundedScore}%`,
color: floorCount(roundedScore, 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 popularityScore = score.popularityScore
return this.constructor.render({ popularityScore })
}
}
23 changes: 23 additions & 0 deletions services/pub/pub-popularity.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isIntegerPercentage } from '../test-validators.js'
import { createServiceTester } from '../tester.js'

export const t = await createServiceTester()

t.create('pub popularity (valid)').get('/analysis_options.json').expectBadge({
label: 'popularity',
message: isIntegerPercentage,
})

t.create('pub popularity (not found)')
.get('/analysisoptions.json')
.expectBadge({
label: 'popularity',
message: 'not found',
color: 'red',
})

t.create('pub popularity (invalid)').get('/analysis-options.json').expectBadge({
label: 'popularity',
message: 'invalid',
color: 'lightgrey',
})

0 comments on commit 3e2c9b2

Please sign in to comment.