Files
shields/services/itunes/itunes.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

68 lines
1.9 KiB
JavaScript

'use strict'
const LegacyService = require('../legacy-service')
const { makeBadgeData: getBadgeData } = require('../../lib/badge-data')
const { addv: versionText } = require('../../lib/text-formatters')
const { version: versionColor } = require('../../lib/color-formatters')
module.exports = class Itunes extends LegacyService {
static get category() {
return 'version'
}
static get route() {
return {
base: 'itunes/v',
}
}
static get examples() {
return [
{
title: 'iTunes App Store',
previewUrl: '803453959',
urlPattern: ':bundle-id',
exampleUrl: '803453959',
},
]
}
static registerLegacyRouteHandler({ camp, cache }) {
camp.route(
/^\/itunes\/v\/(.+)\.(svg|png|gif|jpg|json)$/,
cache((data, match, sendBadge, request) => {
const bundleId = match[1] // eg, `324684580`
const format = match[2]
const apiUrl = `https://itunes.apple.com/lookup?id=${bundleId}`
const badgeData = getBadgeData('itunes app store', data)
request(apiUrl, (err, res, buffer) => {
if (err !== null) {
badgeData.text[1] = 'inaccessible'
sendBadge(format, badgeData)
return
}
try {
const data = JSON.parse(buffer)
if (data.resultCount === 0) {
/* Note the 'not found' response from iTunes is:
status code = 200,
body = { "resultCount":0, "results": [] }
*/
badgeData.text[1] = 'not found'
sendBadge(format, badgeData)
return
}
const version = data.results[0].version
badgeData.text[1] = versionText(version)
badgeData.colorscheme = versionColor(version)
sendBadge(format, badgeData)
} catch (e) {
badgeData.text[1] = 'invalid'
sendBadge(format, badgeData)
}
})
})
)
}
}