Files
shields/services/docker/docker-pulls.service.js
Paul Melnikow 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

65 lines
1.3 KiB
JavaScript

'use strict'
const Joi = require('joi')
const { metric } = require('../../lib/text-formatters')
const { BaseJsonService } = require('..')
const { nonNegativeInteger } = require('../validators')
const {
dockerBlue,
buildDockerUrl,
getDockerHubUser,
} = require('./docker-helpers')
const pullsSchema = Joi.object({
pull_count: nonNegativeInteger,
}).required()
module.exports = class DockerPulls extends BaseJsonService {
async fetch({ user, repo }) {
return this._requestJson({
schema: pullsSchema,
url: `https://hub.docker.com/v2/repositories/${getDockerHubUser(
user
)}/${repo}`,
errorMessages: { 404: 'repo not found' },
})
}
static render({ count }) {
return {
message: metric(count),
color: dockerBlue,
}
}
async handle({ user, repo }) {
const data = await this.fetch({ user, repo })
return this.constructor.render({ count: data.pull_count })
}
static get defaultBadgeData() {
return { label: 'docker pulls' }
}
static get category() {
return 'downloads'
}
static get route() {
return buildDockerUrl('pulls')
}
static get examples() {
return [
{
title: 'Docker Pulls',
namedParams: {
user: '_',
repo: 'ubuntu',
},
staticPreview: this.render({ count: 765400000 }),
},
]
}
}