-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetJson.js
70 lines (60 loc) · 2.29 KB
/
getJson.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
(function() {
'use strict';
var rp = require('request-promise');
var fs = require('fs');
var _und = require("./lib/underscore/underscore-min");
var FetchedJsonData = {
'QuestionTypesSpreadsheet': null,
'QuestionsSpreadsheet': null,
'ChoicesSpreadsheet': null,
'AnswersSpreadsheet': null
}
var spreadsheets = [
{name: 'QuestionTypesSpreadsheet', url: 'https://spreadsheets.google.com/feeds/cells/1to58wg_r4R_oie7IqYPzsqMglIKkDYCh4peNhCZ6xm4/1/public/values?alt=json-in-script'},
{name: 'AnswersSpreadsheet', url: 'https://spreadsheets.google.com/feeds/cells/1to58wg_r4R_oie7IqYPzsqMglIKkDYCh4peNhCZ6xm4/2/public/values?alt=json-in-script'},
{name: 'QuestionsSpreadsheet', url: 'https://spreadsheets.google.com/feeds/cells/1to58wg_r4R_oie7IqYPzsqMglIKkDYCh4peNhCZ6xm4/3/public/values?alt=json-in-script'},
{name: 'ChoicesSpreadsheet', url: 'https://spreadsheets.google.com/feeds/cells/1to58wg_r4R_oie7IqYPzsqMglIKkDYCh4peNhCZ6xm4/4/public/values?alt=json-in-script'}
];
var jsontoString = function() {
var returnString = "var FetchedJsonData = {\n";
for (var key in FetchedJsonData){
returnString += " " + key + ": ";
returnString += FetchedJsonData[key] + ",\n";
}
returnString += "};";
return returnString;
}
var file = __dirname + '/src/fetchedJsonData.js';
var getDataForQuestionnaire = function(body){
var startPosition = body.indexOf('entry') + 7;
var endPosition = body.length - 4;
return body.substring(startPosition, endPosition);
};
var resetFile = function(file){
fs.writeFile(file, '', function(err) {
if (err) console.log(err);
});
};
var appendToFile = function(file, data){
fs.appendFile(file, data, function(err) {
if(err) console.log(err);
});
};
var getJsonAndUpdate = function(file, spreadsheet){
rp(spreadsheet.url)
.then(function(resp) {
var data = getDataForQuestionnaire(resp);
FetchedJsonData[spreadsheet.name] = data;
})
.then(function(){
if(_und.contains(_und.values(FetchedJsonData), null) == false){
resetFile(file);
appendToFile(file, jsontoString());
}
})
.catch(console.error);
};
spreadsheets.forEach(function(spreadsheet){
getJsonAndUpdate(file, spreadsheet);
});
})();