Files
shields/services/github/github-stars.service.js
Paul Melnikow bedba47d77 Move legacy services from server.js into services/ (#1958)
This builds on the work of #1931 by moving the legacy services into `services/`.
2018-08-27 13:29:54 -04:00

49 lines
1.6 KiB
JavaScript

'use strict'
const LegacyService = require('../legacy-service')
const {
makeBadgeData: getBadgeData,
makeLogo: getLogo,
} = require('../../lib/badge-data')
const { metric } = require('../../lib/text-formatters')
const {
checkErrorResponse: githubCheckErrorResponse,
} = require('./github-helpers')
module.exports = class GithubStars extends LegacyService {
static registerLegacyRouteHandler({ camp, cache, githubApiProvider }) {
camp.route(
/^\/github\/stars\/([^/]+)\/([^/]+)\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const user = match[1] // eg, qubyte/rubidium
const repo = match[2]
const format = match[3]
const apiUrl = `/repos/${user}/${repo}`
const badgeData = getBadgeData('stars', data)
if (badgeData.template === 'social') {
badgeData.logo = getLogo('github', data)
badgeData.links = [
'https://github.com/' + user + '/' + repo,
'https://github.com/' + user + '/' + repo + '/stargazers',
]
}
githubApiProvider.request(request, apiUrl, {}, (err, res, buffer) => {
if (githubCheckErrorResponse(badgeData, err, res)) {
sendBadge(format, badgeData)
return
}
try {
badgeData.text[1] = metric(JSON.parse(buffer).stargazers_count)
badgeData.colorscheme = null
badgeData.colorB = '#4183C4'
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
}
}