Last active
October 22, 2015 23:39
-
-
Save martinsson/043992ed9be13d83458f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function LangBuilder(submitToQueue, pdfUtility) { | |
function _sendOffSplitJob(parentEntityKey, langId, faces, pdfAbsolutePath) { | |
//... | |
submitToQueue(jobPayload); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (queue, splitJobConfig) { | |
return function submitToQueue(jobPayload) { | |
queue.create(splitJobConfig.queueName, jobPayload) | |
.attempts(splitJobConfig.attempts) | |
.save(); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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