-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
executable file
·125 lines (102 loc) · 3.75 KB
/
logic.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const urlGetter = require('./utils/UrlGetter');
const futureDate = new Date(new Date().getFullYear()+5,0,0,0,0,0,0);
function getParasha(cb, city='') {
getCalLuach('parashat', cb, city);
}
function encodeQueryParams(baseUrl, params) {
var paramsStr = Object.keys(params).map(function(key) {
return [key, params[key]].map(encodeURIComponent).join('=');
});
return [baseUrl, paramsStr.join('&')].join("&");
}
/**
*
* @param {string} title What category in the response we are after
* @param {Function} emitHandler emit fucntion to return to Alexa
* @param {string} city What is our city of interest
*/
function getCalLuach(title, emitHandler, city) {
return getCalLuachEx(title, emitHandler, city, new Date().getMonth()+1);
}
function getCalLuachEx(title, emitHandler, city, month) {
var params = {
'city': 'CA-Vancouver',
'month': month
};
if (!city) {
}
var baseUrlStr='http://www.hebcal.com/hebcal/?v=1&cfg=json&maj=on&min=on&nx=on&year=now&ss=on&mf=on&c=on&geo=city&m=50&s=on';
if (city) {
params.city = city;
}
var urlStr = encodeQueryParams(baseUrlStr, params);
console.log("Calling URL: ", urlStr);
urlGetter.getFromUrl(urlStr,
(body) => calLuachResponseCallback(body, title, emitHandler, month, city));
}
/**
* Get the body from the response togethre with the title, month and city
* Find the right answer to emit by:
* First, filter the response by city and title
* from the filtered results choose the first one which is after today
* If there's no results after the above logic, it means
* we're in the end of teh month and we should try with the next one
* @param {Object} body response body
* @param {string} title category of items we're into
* @param {Function} emitHandler emit skill reponse to Alexa
* @param {number} month - month of the year we're trying to get
* @param {string} city - city
*/
function calLuachResponseCallback(body, title, emitHandler, month, city) {
var items = body.items;
var selectedTitle="";
var selectedDate=futureDate;
var today=new Date();
if (items) {
for (var item of items) {
if (item.category == title) {
console.log("p:", item.title, "d:", item.date);
var parasha=item.title;
var parashaDate=new Date(item.date);
console.log("pDate: ", parashaDate);
if (parashaDate>today && parashaDate<selectedDate) {
selectedTitle=parasha;
selectedDate=parashaDate;
}
console.log(parashaDate);
}
}
}
console.log("month type: ", typeof month, "Category:", title, "Selected:", selectedTitle, "SelectedDate:", selectedDate);
if (!selectedTitle) {
if (new Date().getMonth()+1<month) {
console.log("Couldn't find return value");
} else{
getCalLuachEx(title, emitHandler, city, month+1);
}
return;
}
var location = body.location.title;
var response = "In " + location + ". " + selectedTitle;
emitHandler(':tell', response);
}
function getShabbatTime(emitCb, city='') {
console.log('getShabbatTime called with city: ', city);
getCalLuach('candles', emitCb, city);
}
function testerCb(cmd, title) {
console.log("TesterCB called with: cmd=", cmd, ", title=", title);
}
function tester() {
console.log("hi", process.argv, process.argv[2]);
eval(process.argv[2]+"(testerCb)");
}
module.exports = {
getParasha: getParasha,
getShabbatTime: getShabbatTime,
tester: tester,
calLuachResponseCallback: calLuachResponseCallback // only here to enable unit test
};
if (require.main == module) {
tester();
}