-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildData.js
87 lines (80 loc) · 2.77 KB
/
buildData.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
const fs = require('fs');
const https = require('https');
const cheerio = require('cheerio');
const path = require('path');
// The menu is stored as an html string on a JS function call
// Looks like this:
// menuApi.defaultApiCallResponseHandler('htmlStringWithMenuInfo', 'jsonStringWithLocationInfo');
function extractMenuXmlFromJsString(jsString) {
// At the moment, we can just use the index of ' characters
const startIndex = jsString.indexOf('\'') + 1;
const endIndex = jsString.indexOf('\'', startIndex);
const xmlString = jsString.substring(startIndex, endIndex);
return xmlString;
}
function parseEntitiesInSection($, $section, sectionId) {
const entities = [];
const $menuItems = $section.find('menuItem');
$menuItems.each((i, el) => {
const $el = $(el);
const entity = {};
entity.sectionId = sectionId;
entity.id = $el.children('item_id').first().text();
entity.name = $el.children('item_title').first().text();
entity.description = $el.children('item_description').first().text();
entity.index = parseInt($el.children('itemIndex').first().text(), 10);
entities.push(entity);
});
return entities;
}
function parseMenuXml(xmlString) {
const flattenedMenuData = {
sections: [],
entities: [],
};
const $ = cheerio.load(xmlString, {
xmlMode: true,
decodeEntities: true,
});
const $sections = $('section');
$sections.each((i, el) => {
const $el = $(el);
const section = {};
section.id = $el.children('section_id').first().text();
section.name = $el.children('section_name').first().text();
section.description = $el.children('section_desc').first().text();
section.index = parseInt($el.children('sectionIndex').first().text(), 10);
const entities = parseEntitiesInSection($, $el, section.id);
entities.forEach(e => flattenedMenuData.entities.push(e));
flattenedMenuData.sections.push(section);
});
return flattenedMenuData;
}
function writeObjectToFile(data) {
return new Promise((resolve, reject) => {
const jsonString = JSON.stringify(data, null, 2);
const filePath = path.join(__dirname, 'client/src/data/menu.json');
fs.writeFile(filePath, jsonString, err => {
if (err) {
reject(err);
return;
}
resolve(data);
});
});
}
const request = new Promise((resolve, reject) => {
https.get('https://menus.singleplatform.co/storefront/menus/juice-stop-15.js', res => {
if (res.statusCode !== 200) {
reject(new Error('Request failed with status code: ' + res.statusCode));
}
let rawData = '';
res.on('data', d => rawData += d);
res.on('end', () => resolve(rawData));
}).on('error', e => reject(e));
});
request
.then(extractMenuXmlFromJsString)
.then(parseMenuXml)
.then(writeObjectToFile)
.catch(e => console.error(e));