forked from YarivGilad/serverless-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (40 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const rest = require('./utils/rest');
const readability = require('./readability/calculations');
const readabilityScore = require('./readability/saveScore');
module.exports.hello = (event, context, callback) => {
console.info('hello');
const response = rest.response(200, 'Version: v1.0', '/');
callback(null, response);
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};
module.exports.getEpoch = (event, context, callback) => {
console.info('getEpoch');
const epoch = Math.round((new Date().getTime() / 1000.0) + 30); // in seconds
console.info(`Epoch: ${epoch}`);
const response = rest.response(200, { epoch }, '/get-epoch');
callback(null, response);
};
module.exports.fkReadability = (event, context, callback) => {
console.info('fkReadability');
const data = JSON.parse(event.body);
const readingBurden = readability.fleschKincaid(data.text);
const response = rest.response(200, {
readingBurden,
text: data.text,
}, '/flesch-kincaid');
callback(null, response);
};
module.exports.saveFkReadabilityScore = (event, context, callback) => {
console.info(`saveFkReadabilityScore: ${event.jobId}`);
if (!parseInt(event.jobId, 10)) {
callback(TypeError('Invalid Job ID'), {
statusCode: 400,
message: 'Invalid Job ID. Please retry.',
});
}
const data = JSON.parse(event.body);
const readingBurden = readability.fleschKincaid(data.text);
const response = readabilityScore.saveJobScore(event.jobId, data.text, readingBurden);
callback(null, response);
};