Files
shields/services/clojars/clojars-downloads.service.js
Paul Melnikow fafb22efee Move "good" badge helpers from lib/ to services/ (#3101)
This moves a few helpers from `lib/` to `services/`:

build-status.js
build-status.spec.js
color-formatters.js
color-formatters.spec.js
contributor-count.js
licenses.js
licenses.spec.js
php-version.js
php-version.spec.js
text-formatters.js
text-formatters.spec.js
version.js
version.spec.js

And one from `lib/` to `core/`:

unhandled-rejection.spec.js

The diff is long, but the changes are straightforward.

Ref #2832
2019-02-27 20:47:46 -05:00

60 lines
1.3 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { metric } = require('../text-formatters')
const { downloadCount: downloadsColor } = require('../color-formatters')
const { nonNegativeInteger } = require('../validators')
const { BaseJsonService } = require('..')
const clojarsSchema = Joi.object({
downloads: nonNegativeInteger,
}).required()
module.exports = class ClojarsDownloads extends BaseJsonService {
async fetch({ clojar }) {
const url = `https://clojars.org/api/artifacts/${clojar}`
return this._requestJson({
url,
schema: clojarsSchema,
})
}
static render({ downloads }) {
return {
label: 'downloads',
message: metric(downloads),
color: downloadsColor(downloads),
}
}
async handle({ clojar }) {
const json = await this.fetch({ clojar })
return this.constructor.render({ downloads: json.downloads })
}
// Metadata
static get defaultBadgeData() {
return { label: 'downloads' }
}
static get category() {
return 'downloads'
}
static get route() {
return {
base: 'clojars/dt',
pattern: ':clojar+',
}
}
static get examples() {
return [
{
namedParams: { clojar: 'prismic' },
staticPreview: this.render({ downloads: 117 }),
},
]
}
}