-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
75 lines (64 loc) · 1.84 KB
/
index.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
var request = require('superagent'),
_ = require('underscore');
//info about the db
request
.get('http://127.0.0.1:5984')
.end(function (res) {
if (res.ok) {
console.log(JSON.parse(res.text));
// console.log('yay got ' + JSON.stringify(res.text, null, 4));
} else {
console.log('Oh no! error ' + res.text);
}
});
//delete db
request
.del('http://127.0.0.1:5984/toggl')
.end(function (res) {
if (res.ok) {
console.log(JSON.parse(res.text));
// console.log('yay got ' + JSON.stringify(res.text, null, 4));
} else {
console.log('Oh no! error ' + res.text);
}
});
//create db
request
.put('http://127.0.0.1:5984/toggl')
.end(function (res) {
if (res.ok) {
console.log(JSON.parse(res.text));
// console.log('yay got ' + JSON.stringify(res.text, null, 4));
} else {
console.log('Oh no! error ' + res.text);
}
});
var getTimeRecords = function (callback) {
request
.get('https://www.toggl.com/api/v8/time_entries')
.auth('024ef3632096fd0b27b0b0081fc5975e', 'api_token')
.end(function(res){
callback(res);
});
};
var persistTimeRecord = function (record) {
//skip time record if tag 'tagged' is present
if (Array.isArray(record.tags) && typeof _.contains(record.tags, 'tagged')) {
console.log('logged tag found, skipping...');
return;
}
request
.put('http://127.0.0.1:5984/toggl/' + record.guid)
.set('Content-Type', 'application/json')
.send(record)
.end(function (res) {
// console.log(JSON.parse(res.text));
});
};
getTimeRecords(function (res) {
var timeRecords = JSON.parse(res.text);
var i;
for (i = 0; i < timeRecords.length; i++) {
persistTimeRecord(timeRecords[i]);
}
});