Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
it('contains the path of the pdf', function() {
var submitToQueueSpy = sinon.spy();
var langBuilder = new LangBuilder(submitToQueueSpy, pdfUtility);
langBuilder.buildLang(parentEntityKey, pdPath);
var expectedPayload = {originalFilepath: pdPath};
sinon.assert.calledWith(submitToQueueSpy, sinon.match(expectedPayload))
})
function LangBuilder(submitToQueue, pdfUtility) {
function _sendOffSplitJob(parentEntityKey, langId, faces, pdfAbsolutePath) {
//...
submitToQueue(jobPayload);
}
var _ = require('lodash');
var PdfUtility = require('./thirdParty/PdfUtility');
var nodeUuid = require('node-uuid');
var Face = require('./restOfTheCode/Face');
function LangBuilder(jobQueue, pdfUtility) {
var queue = jobQueue;
var splitJobConfig = {
queueName: 'job-split-pdf',
attempts: 5
};
function buildLang(parentEntityKey, pdfAbsolutePath, forcedLangId) {
var pdfInfo = pdfUtility.getPdfInfo(pdfAbsolutePath);
var langId = defineLang(forcedLangId, pdfInfo);
var faces = buildFaces(pdfInfo);
var lang = {
id: langId,
face: faces
};
_sendOffSplitJob(parentEntityKey, langId, faces, pdfAbsolutePath);
return lang;
}
function _sendOffSplitJob(parentEntityKey, langId, faces, pdfAbsolutePath) {
var langEntityKey = parentEntityKey.append('lang', langId);
var jobPayload = buildDataForSplitJob(langEntityKey, faces, pdfAbsolutePath);
queue.create(splitJobConfig.queueName, jobPayload)
.attempts(splitJobConfig.attempts)
.save();
}
return {
buildLang: buildLang
}
}
function buildFaces(pdfInfo) {
return _.times(pdfInfo.numberOfPages, function () {
return Face.buildFace({
id: nodeUuid.v1(),
dimensions: {
width: pdfInfo.width,
height: pdfInfo.height
}
});
});
}
function defineLang(forcedLangId, pdfInfo) {
var langId;
if (forcedLangId) {
langId = forcedLangId;
} else if (pdfInfo.lang) {
langId = pdfInfo.lang;
} else {
langId = 'fr';
}
return langId;
}
function buildDataForSplitJob(langEntityKey, faces, pdfAbsolutePath) {
var callbackRoutes = faces.map(function (face) {
return [langEntityKey.directory(), 'face', face.id].join('/');
});
var jobTitle = langEntityKey.directory();
return {
title: jobTitle,
originalFilepath: pdfAbsolutePath,
faceRoutes: callbackRoutes,
startSplit: 1
};
}
module.exports = LangBuilder;
function LangBuilder(queue, pdfUtility) {
var splitJobConfig = {
queueName: 'job-split-pdf',
attempts: 5
};
//...
function buildLang(parentEntityKey, pdfAbsolutePath, forcedLangId) {
var pdfInfo = pdfUtility.getPdfInfo(pdfAbsolutePath);
var langId = defineLang(forcedLangId, pdfInfo);
var faces = buildFaces(pdfInfo);
var lang = {
id: langId,
face: faces
};
_sendOffSplitJob(parentEntityKey, langId, faces, pdfAbsolutePath);
return lang;
}
//....
function _sendOffSplitJob(parentEntityKey, langId, faces, pdfAbsolutePath) {
var langEntityKey = parentEntityKey.append('lang', langId);
var jobPayload = buildDataForSplitJob(langEntityKey, faces, pdfAbsolutePath);
queue.create(splitJobConfig.queueName, jobPayload)
.attempts(splitJobConfig.attempts)
.save();
}
module.exports = function (queue, splitJobConfig) {
return function submitToQueue(jobPayload) {
queue.create(splitJobConfig.queueName, jobPayload)
.attempts(splitJobConfig.attempts)
.save();
}
};
describe('sends a splitJob to the message queue', function () {
it('contains the path of the pdf', function() {
var saveSpy = sinon.spy();
var attemptsStub = sinon.stub();
attemptsStub
.withArgs(5)
.returns({save: saveSpy});
var createStub = sinon.stub();
var expectedPayload = {originalFilepath: pdPath};
createStub
.withArgs('job-split-pdf', sinon.match(expectedPayload))
.returns({attempts: attemptsStub});
var jobQueue = {
create: createStub
};
var langBuilder = new LangBuilder(jobQueue, fileSystemContext, pdfUtility);
langBuilder.buildLang(parentEntityKey, pdPath);
sinon.assert.calledOnce(saveSpy)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment