From ade62fc9811ffaa3237f448752055f3c21b5c639 Mon Sep 17 00:00:00 2001 From: Paul Melnikow Date: Fri, 19 Jan 2018 11:03:21 -0500 Subject: [PATCH] BaseService: make serviceData and badgeData explicit and declarative (#1451) --- lib/badge-data.js | 1 + services/base.js | 77 +++++++++++++++++++++++++++++++------------ services/base.spec.js | 25 ++++++++++++-- 3 files changed, 80 insertions(+), 23 deletions(-) diff --git a/lib/badge-data.js b/lib/badge-data.js index 706554abea113..376003d9ad194 100644 --- a/lib/badge-data.js +++ b/lib/badge-data.js @@ -104,6 +104,7 @@ function makeBadgeData(defaultLabel, overrides) { } module.exports = { + toArray, prependPrefix, isDataUri, isValidStyle, diff --git a/services/base.js b/services/base.js index 0ad3fe78c8d28..53a60fbcbd12e 100644 --- a/services/base.js +++ b/services/base.js @@ -1,7 +1,11 @@ 'use strict'; const { - makeBadgeData: getBadgeData, + isValidStyle, + makeLogo, + toArray, + makeColor, + setBadgeColor, } = require('../lib/badge-data'); module.exports = class BaseService { @@ -84,40 +88,71 @@ module.exports = class BaseService { return result; } + static _makeBadgeData(overrides, serviceData) { + const { + style, + label: overrideLabel, + logo: overrideLogo, + logoWidth: overrideLogoWidth, + link: overrideLink, + colorA: overrideColorA, + colorB: overrideColorB, + } = overrides; + + const { + label: serviceLabel, + message: serviceMessage, + color: serviceColor, + link: serviceLink, + } = serviceData; + + const defaultLabel = this.category; + const { + color: defaultColor, + logo: defaultLogo, + } = this.defaultBadgeData; + + const badgeData = { + text: [ + overrideLabel || serviceLabel || defaultLabel, + serviceMessage || 'n/a', + ], + template: isValidStyle(style) ? style : 'default', + logo: makeLogo(style === 'social' ? defaultLogo : undefined, { logo: overrideLogo }), + logoWidth: +overrideLogoWidth, + links: toArray(overrideLink || serviceLink), + colorA: makeColor(overrideColorA), + }; + const color = makeColor(overrideColorB || serviceColor || defaultColor || 'lightgrey'); + setBadgeColor(badgeData, color); + + return badgeData; + } + static register(camp, handleRequest) { const serviceClass = this; // In a static context, "this" is the class. camp.route(this._regex, handleRequest(async (queryParams, match, sendBadge, request) => { - // Assumes the final capture group is the extension - const format = match.slice(-1)[0]; - - const badgeData = getBadgeData( - serviceClass.category, - Object.assign({}, serviceClass.defaultBadgeData, queryParams) - ); + let serviceData; try { const namedParams = this._namedParamsForMatch(match); const serviceInstance = new serviceClass({ sendAndCacheRequest: request.asPromise, }); - const serviceData = await serviceInstance.handle(namedParams); - const text = badgeData.text; - if (serviceData.message) { - text[1] = serviceData.message; - } - Object.assign(badgeData, serviceData); - badgeData.text = text; - sendBadge(format, badgeData); - + serviceData = await serviceInstance.handle(namedParams); } catch (error) { + serviceData = { message: 'error' }; console.log(error); - const text = badgeData.text; - text[1] = 'error'; - badgeData.text = text; - sendBadge(format, badgeData); } + + // Assumes the final capture group is the extension + const format = match.slice(-1)[0]; + + const badgeData = this._makeBadgeData(queryParams, serviceData); + + sendBadge(format, badgeData); })); } }; diff --git a/services/base.spec.js b/services/base.spec.js index d02efbdbacdc8..ee4d3de3ce409 100644 --- a/services/base.spec.js +++ b/services/base.spec.js @@ -24,6 +24,29 @@ class DummyService extends BaseService { } describe('BaseService', () => { + describe('_makeBadgeData', function () { + describe('Overrides', function () { + it('overrides the label', function () { + const badgeData = DummyService._makeBadgeData({ label: 'purr count' }, { label: 'purrs' }); + expect(badgeData.text).to.deep.equal(['purr count', 'n/a']); + }); + }); + + describe('Service data', function () { + it('applies the service message', function () { + const badgeData = DummyService._makeBadgeData({}, { message: '10k' }); + expect(badgeData.text).to.deep.equal(['cat', '10k']); + }); + }); + + describe('Defaults', function () { + it('uses the default label', function () { + const badgeData = DummyService._makeBadgeData({}, {}); + expect(badgeData.text).to.deep.equal(['cat', 'n/a']); + }); + }); + }); + describe('ScoutCamp integration', function () { const expectedRouteRegex = /^\/foo\/([^/]+).(svg|png|gif|jpg|json)$/; @@ -62,7 +85,6 @@ describe('BaseService', () => { expect(mockSendBadge).to.have.been.calledWith( /*format*/ 'svg', { - message: 'Hello bar', text: ['cat', 'Hello bar'], colorscheme: 'lightgrey', template: 'default', @@ -70,7 +92,6 @@ describe('BaseService', () => { logoWidth: NaN, links: [], colorA: undefined, - colorB: undefined, } ); });