-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipes.js
102 lines (97 loc) · 3.29 KB
/
recipes.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict';
const AWS = require('aws-sdk');
AWS.config.update({accessKeyId: '', secretAccessKey: ''});
const getClosest = require('get-closest');
const Levenshtein = require('levenshtein');
const Promise = require('bluebird');
module.exports = {
compareLevenshteinDistance: function(compareTo, baseItem) {
return new Levenshtein(compareTo, baseItem).distance;
},
validateResponse: function(response) {
var firstCharCode = response.charCodeAt(0);
if (firstCharCode == 65279) {
return response.substring(1);
}
return response;
},
parseIngredients: function(ingredients) {
var ingredientsList = [];
var ingredientsString = '';
ingredients.forEach(function(ingredient) {
if (ingredientsString != '') {
ingredientsString += ', ';
}
ingredientsList.push(ingredient);
ingredientsString += ingredient;
});
return ingredientsString;
},
getAllRecipeData: function(callback) {
var s3 = new AWS.S3();
var params = {
Bucket: '',
Prefix: 'recipes/'
}
var recipes = [];
var readRecipe = (recipe) => {
return new Promise((resolve, reject) => {
s3.getObject({Bucket: '', Key: recipe}, function(err, data) {
if (err) reject(err);
var fileText = data.Body.toString();
fileText = module.exports.validateResponse(fileText);
var jsonObject = JSON.parse(fileText);
resolve(jsonObject);
});
})
}
s3.listObjects(params, function(err, data) {
if (err) throw err;
var keys = [];
data.Contents.forEach(function(item) {
if (item.Key != 'recipes/') {
keys = keys.concat(item.Key);
}
});
Promise.map(keys, readRecipe).then(recipes => {
callback(recipes);
})
});
},
randomNumber: function() {
return Math.random();
},
getRandomRecipe: function(recipes, callback) {
var i = Math.floor(module.exports.randomNumber() * recipes.length);
callback(recipes[i]);
},
matchRecipe: function(spokenRecipe, recipes, callback) {
var recipeDict = {};
var recipeNames = [];
recipes.forEach(function(recipe) {
var key = recipe.key;
recipeDict[recipe.title] = recipe;
recipeNames.push(recipe.title);
});
var match = getClosest.custom(spokenRecipe, recipeNames, module.exports.compareLevenshteinDistance);
var recipe = recipeDict[recipeNames[match]];
callback(recipe);
},
listRecipes: function(recipes) {
var speechOutput = '';
recipes.forEach(function(recipe) {
if (speechOutput != '') {
speechOutput += ', ';
}
speechOutput += recipe.title.replace('&', ' and ');
});
return speechOutput;
},
getSpokenRecipe: function(recipeSlot) {
let recipeName;
if (recipeSlot && recipeSlot.value) {
recipeName = recipeSlot.value.toLowerCase();
}
return recipeName;
}
}