Skip to content

Commit

Permalink
BaseService: make serviceData and badgeData explicit and declarative (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmelnikow authored Jan 19, 2018
1 parent b6045b9 commit ade62fc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/badge-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function makeBadgeData(defaultLabel, overrides) {
}

module.exports = {
toArray,
prependPrefix,
isDataUri,
isValidStyle,
Expand Down
77 changes: 56 additions & 21 deletions services/base.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use strict';

const {
makeBadgeData: getBadgeData,
isValidStyle,
makeLogo,
toArray,
makeColor,
setBadgeColor,
} = require('../lib/badge-data');

module.exports = class BaseService {
Expand Down Expand Up @@ -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);
}));
}
};
25 changes: 23 additions & 2 deletions services/base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)$/;

Expand Down Expand Up @@ -62,15 +85,13 @@ describe('BaseService', () => {
expect(mockSendBadge).to.have.been.calledWith(
/*format*/ 'svg',
{
message: 'Hello bar',
text: ['cat', 'Hello bar'],
colorscheme: 'lightgrey',
template: 'default',
logo: undefined,
logoWidth: NaN,
links: [],
colorA: undefined,
colorB: undefined,
}
);
});
Expand Down

0 comments on commit ade62fc

Please sign in to comment.