* allow service classes to define a static example * define static example for some services (apm, appveyor, cdnjs, clojars, gem, librariesio, npm, uptimerobot) * add/update tests This allows us to show an example without making an API call to a live service for better performance. We can now specify 3 fields in the example definition: * urlPattern for the version with placeholders e.g: /npm/dw/:package.svg * ExampleUrl/Uri for the concrete example e.g: /npm/dw/localeval.svg * PreviewUrl/Uri for the static (or live) image we will actually show
54 lines
1.0 KiB
JavaScript
54 lines
1.0 KiB
JavaScript
'use strict'
|
|
|
|
const { colorScale } = require('../../lib/color-formatters')
|
|
const LibrariesIoBase = require('./librariesio-base')
|
|
|
|
const sourceRankColor = colorScale([10, 15, 20, 25, 30])
|
|
|
|
class LibrariesIoSourcerank extends LibrariesIoBase {
|
|
static get category() {
|
|
return 'rating'
|
|
}
|
|
|
|
static get defaultBadgeData() {
|
|
return {
|
|
label: 'sourcerank',
|
|
}
|
|
}
|
|
|
|
static get url() {
|
|
return this.buildUrl('librariesio/sourcerank')
|
|
}
|
|
|
|
static get examples() {
|
|
return [
|
|
{
|
|
title: 'Libraries.io SourceRank',
|
|
exampleUrl: 'npm/got',
|
|
urlPattern: ':platform/:library',
|
|
staticExample: this.render({ rank: 25 }),
|
|
},
|
|
]
|
|
}
|
|
|
|
static render({ rank }) {
|
|
return {
|
|
message: rank,
|
|
color: sourceRankColor(rank),
|
|
}
|
|
}
|
|
|
|
async handle({ platform, packageName }) {
|
|
const { rank } = await this.fetch(
|
|
{
|
|
platform,
|
|
packageName,
|
|
},
|
|
{ allowPackages: true }
|
|
)
|
|
return this.constructor.render({ rank })
|
|
}
|
|
}
|
|
|
|
module.exports = LibrariesIoSourcerank
|