Files
shields/services/docker/docker-stars.service.js
T
Paul MelnikowandGitHub 226fa67a02 Create shortcut for BaseService-related imports (#2809)
Continue to implement #2698:

- Add `core/base-service/index.js` (but hold off on moving the things it imports)
- Add shortcuts in `services/index.js` for Base*Service, errors, and deprecatedService. This file will be streamlined later to avoid cluttering it with rarely used bits.
- Apply consistent ordering of imports and use of `module.exports` in testers.
- Remove some renaming of imports.
- Remove obsolete tests here and there.
2019-01-21 15:41:24 -05:00

61 lines
1.3 KiB
JavaScript

'use strict'
const { metric } = require('../../lib/text-formatters')
const { BaseService } = require('..')
const { nonNegativeInteger } = require('../validators')
const {
dockerBlue,
buildDockerUrl,
getDockerHubUser,
} = require('./docker-helpers')
module.exports = class DockerStars extends BaseService {
async fetch({ user, repo }) {
const url = `https://hub.docker.com/v2/repositories/${getDockerHubUser(
user
)}/${repo}/stars/count/`
const { buffer } = await this._request({
url,
errorMessages: { 404: 'repo not found' },
})
return this.constructor._validate(buffer, nonNegativeInteger)
}
static render({ stars }) {
return {
message: metric(stars),
color: dockerBlue,
}
}
async handle({ user, repo }) {
const stars = await this.fetch({ user, repo })
return this.constructor.render({ stars })
}
static get defaultBadgeData() {
return { label: 'docker stars' }
}
static get category() {
return 'rating'
}
static get route() {
return buildDockerUrl('stars')
}
static get examples() {
return [
{
title: 'Docker Stars',
namedParams: {
user: '_',
repo: 'ubuntu',
},
staticPreview: this.render({ stars: 9000 }),
},
]
}
}