Files
shields/services/cran/cran.service.js
T
Paul MelnikowandGitHub 83ac6ff1b3 Enforce use of template literals (#2242)
This is consistent with what we're pretty much already doing, and saves us from making the request during code review.

These were all autofixed and most of them seem easier to read. Some in the legacy services should be rewritten in more legible forms during refactor (ie using intermediate variables, or using request’s qs option). There are some in helper functions and elsewhere that should get rewritten separately. I don't want to change them in this PR because the changes will get lost in this diff, though we could identify them here and fix them before or just after.
2018-11-02 17:11:44 -04:00

63 lines
2.0 KiB
JavaScript

'use strict'
const LegacyService = require('../legacy-service')
const {
makeBadgeData: getBadgeData,
makeLabel: getLabel,
} = require('../../lib/badge-data')
const { addv: versionText } = require('../../lib/text-formatters')
const { version: versionColor } = require('../../lib/color-formatters')
// For CRAN/METACRAN.
module.exports = class Cran extends LegacyService {
static registerLegacyRouteHandler({ camp, cache }) {
camp.route(
/^\/cran\/([vl])\/([^/]+)\.(svg|png|gif|jpg|json)$/,
cache((queryParams, match, sendBadge, request) => {
const info = match[1] // either `v` or `l`
const pkg = match[2] // eg, devtools
const format = match[3]
const url = `http://crandb.r-pkg.org/${pkg}`
const badgeData = getBadgeData('cran', queryParams)
request(url, (err, res, buffer) => {
if (err != null) {
badgeData.text[1] = 'inaccessible'
sendBadge(format, badgeData)
return
}
if (res.statusCode === 404) {
badgeData.text[1] = 'not found'
sendBadge(format, badgeData)
return
}
try {
const data = JSON.parse(buffer)
if (info === 'v') {
const version = data.Version
badgeData.text[1] = versionText(version)
badgeData.colorscheme = versionColor(version)
sendBadge(format, badgeData)
} else if (info === 'l') {
badgeData.text[0] = getLabel('license', queryParams)
const license = data.License
if (license) {
badgeData.text[1] = license
badgeData.colorscheme = 'blue'
} else {
badgeData.text[1] = 'unknown'
}
sendBadge(format, badgeData)
} else {
throw Error('Unreachable due to regex')
}
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
}
}