Files
shields/services/packagecontrol/packagecontrol.service.js
Paul Melnikow 02ec19fd22 BaseService terminology: Rename url to route (#2278)
The term “url” is overloaded in services, to refer to the Shields route and also the API URL. Calling the Shields URL a “route” is on the whole more descriptive, and makes it clearer and more obvious which one of these we’re talking about. It’s a small thing, though seems like an improvement.

We have a few functions called `buildUrl`. I’ve renamed them to `buildRoute` when they refer to routes, and left them as `buildUrl` when they refer to API URLs.

I included a minor style tweak and some formatting cleanup in `TUTORIAL.md`.
2018-11-09 15:11:03 -05:00

116 lines
3.7 KiB
JavaScript

'use strict'
const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const { metric } = require('../../lib/text-formatters')
const {
downloadCount: downloadCountColor,
} = require('../../lib/color-formatters')
module.exports = class PackageControl extends LegacyService {
static get category() {
return 'downloads'
}
static get route() {
return {
base: 'packagecontrol',
}
}
static get examples() {
return [
{
title: 'Package Control',
previewUrl: 'dm/GitGutter',
keywords: ['sublime'],
},
{
title: 'Package Control',
previewUrl: 'dw/GitGutter',
keywords: ['sublime'],
},
{
title: 'Package Control',
previewUrl: 'dd/GitGutter',
keywords: ['sublime'],
},
{
title: 'Package Control',
previewUrl: 'dt/GitGutter',
keywords: ['sublime'],
},
]
}
static registerLegacyRouteHandler({ camp, cache }) {
camp.route(
/^\/packagecontrol\/(dm|dw|dd|dt)\/(.*)\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const info = match[1] // either `dm`, `dw`, `dd` or dt`.
const userRepo = match[2] // eg, `Package%20Control`.
const format = match[3]
const apiUrl = `https://packagecontrol.io/packages/${userRepo}.json`
const badgeData = getBadgeData('downloads', data)
request(apiUrl, (err, res, buffer) => {
if (err != null) {
badgeData.text[1] = 'inaccessible'
sendBadge(format, badgeData)
return
}
try {
const data = JSON.parse(buffer)
let downloads = 0
let platforms
switch (info.charAt(1)) {
case 'm':
// daily downloads are separated by Operating System
platforms = data.installs.daily.data
platforms.forEach(platform => {
// loop through the first 30 days or 1 month
for (let i = 0; i < 30; i++) {
// add the downloads for that day for that platform
downloads += platform.totals[i]
}
})
badgeData.text[1] = `${metric(downloads)}/month`
break
case 'w':
// daily downloads are separated by Operating System
platforms = data.installs.daily.data
platforms.forEach(platform => {
// loop through the first 7 days or 1 week
for (let i = 0; i < 7; i++) {
// add the downloads for that day for that platform
downloads += platform.totals[i]
}
})
badgeData.text[1] = `${metric(downloads)}/week`
break
case 'd':
// daily downloads are separated by Operating System
platforms = data.installs.daily.data
platforms.forEach(platform => {
// use the downloads from yesterday
downloads += platform.totals[1]
})
badgeData.text[1] = `${metric(downloads)}/day`
break
case 't':
// all-time downloads are already compiled
downloads = data.installs.total
badgeData.text[1] = metric(downloads)
break
}
badgeData.colorscheme = downloadCountColor(downloads)
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
}
}