-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.js
More file actions
59 lines (53 loc) · 1.62 KB
/
api.js
File metadata and controls
59 lines (53 loc) · 1.62 KB
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
var http = require("spas-http")
var async = require("async")
exports["getEventsMultiple"] = function (params, credentials, callback) {
var cals = params.calendars;
async.concat(cals, getSingleCalendar(params,credentials), function(err, items) {
if (err) {
callback(err, items);
} else {
var dates = {
convert:function(d) {
return (
d.constructor === Date ? d :
d.constructor === Array ? new Date(d[0],d[1],d[2]) :
d.constructor === Number ? new Date(d) :
d.constructor === String ? new Date(d) :
typeof d === "object" ? new Date(d.year,d.month,d.date) :
NaN
);
},
compare:function(a,b) {
return (
isFinite(a=this.convert(a).valueOf()) &&
isFinite(b=this.convert(b).valueOf()) ?
(a>b)-(a<b) :
NaN
);
}
}
items.sort(function(a,b){
return dates.compare(a.start.date || a.start.dateTime,b.start.date || b.start.dateTime);
});
callback(null, items);
}
});
};
var getSingleCalendar = function(params,credentials) {
return function(cal,cb){
params.url = "https://www.googleapis.com/calendar/v3/calendars/" + cal + "/events";
if (params.futureevents) {
params.timeMin = new Date().toISOString();
}
http.request(params, credentials, function(err,results){
if (err) {
return cb(err);
}
if (results.items) {
cb(null,results.items);
} else {
cb(null, []);
}
});
}
};