Files
shields/services/gem/gem-version.service.js
Paul Melnikow 84a5be3946 Declare static examples using namedParams (#2308)
This continues the work from #2279, by allowing example badges to be specified using `namedParams`. Using an object makes it possible for us to display these in form fields down the line. (#701)

I've called this the "preferred" way, and labeled the other ways deprecated. I've also added some doc to the `examples` property in BaseService. Then I realized we had some doc in the tutorial, though I think it's fine to have a short version in the tutorial, and the gory detail in BaseService.

I've also added a `pattern` keyword, and made `urlPattern` an alias.

Closes #2050.
2018-11-17 09:47:25 -05:00

58 lines
1.2 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { renderVersionBadge } = require('../../lib/version')
const BaseJsonService = require('../base-json')
const schema = Joi.object({
// In most cases `version` will be a SemVer but the registry doesn't
// actually enforce this.
version: Joi.string().required(),
}).required()
module.exports = class GemVersion extends BaseJsonService {
async fetch({ gem }) {
return this._requestJson({
schema,
url: `https://rubygems.org/api/v1/gems/${gem}.json`,
})
}
static render({ version }) {
return renderVersionBadge({ version })
}
async handle({ gem }) {
const { version } = await this.fetch({ gem })
return this.constructor.render({ version })
}
// Metadata
static get defaultBadgeData() {
return { label: 'gem' }
}
static get category() {
return 'version'
}
static get route() {
return {
base: 'gem/v',
pattern: ':gem',
}
}
static get examples() {
return [
{
title: 'Gem',
namedParams: { gem: 'formatador' },
staticExample: this.render({ version: '2.1.0' }),
keywords: ['ruby'],
},
]
}
}