-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #942 from beaugunderson/zeo
Adding the Zeo connector (with 1 synclet: sleep)
- Loading branch information
Showing
7 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module.exports = { | ||
handler: function (host, apiKeys, done, req, res) { | ||
if (req.method === 'POST') { | ||
done(null, { | ||
appKey: apiKeys.appKey, | ||
username: req.body.username, | ||
password: req.body.password | ||
}); | ||
|
||
return; | ||
} | ||
|
||
res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
|
||
res.end('Please enter your Zeo credentials:' + | ||
'<form method="post">' + | ||
'<p><label>Username: <input name="username" type="textbox" size="32" /></label></p>' + | ||
'<p><label>Password: <input name="password" type="password" size="32" /></label></p>' + | ||
'<input type="submit" value="Save!">' + | ||
'</form>'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
exports.callApi = function(pi, cb, pather, querier, arrayer) { | ||
var https = require('https'); | ||
|
||
var authorization = 'Basic ' + new Buffer(pi.auth.username + ':' + | ||
pi.auth.password).toString('base64'); | ||
|
||
var options = { | ||
host: 'api.myzeo.com', | ||
port: 8443, | ||
path: '/zeows/api/v1/json' + pather(pi) + querier(pi), | ||
headers: { | ||
'Accept': '*/*', | ||
'Connection': 'close', | ||
'Referer': pi.uri, | ||
'Authorization': authorization | ||
} | ||
}; | ||
|
||
// Send the request | ||
https.get(options, function(res) { | ||
res.setEncoding('utf8'); | ||
|
||
var data = ''; | ||
|
||
res.on('data', function(chunk) { | ||
data += chunk; | ||
}); | ||
|
||
// Once we've got the full response... | ||
res.on('end', function() { | ||
cb(data); | ||
}); | ||
}); | ||
}; | ||
|
||
exports.genericSync = function(type, pather, querier, arrayer) { | ||
return function(pi, cb) { | ||
exports.callApi(pi, function(data) { | ||
var js; | ||
|
||
try { | ||
js = JSON.parse(data); | ||
} catch(E) { | ||
return cb(err); | ||
} | ||
|
||
arrayer(pi, js, function(arrayData) { | ||
var array = {}; | ||
|
||
array[type] = arrayData; | ||
|
||
// And return it using the passed in callback, | ||
// with (optionally) updated auth and config data | ||
cb(null, { | ||
auth: pi.auth, | ||
config: pi.config, | ||
data: array | ||
}); | ||
}); | ||
}, pather, querier, arrayer); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"author": "Beau Gunderson <beau@beaugunderson.com>", | ||
"name": "zeo", | ||
"description": "Zeo", | ||
"version": "0.1.2", | ||
"repository": { | ||
"title": "Zeo", | ||
"handle": "zeo", | ||
"author": "Beau Gunderson <beau@beaugunderson.com>", | ||
"update": "auto", | ||
"github": "https://github.com/LockerProject/Locker", | ||
"type": "connector", | ||
"static": "false", | ||
"url": "" | ||
}, | ||
"engines": { | ||
"node": ">=0.4.9" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
var async = require('async'), | ||
lib = require('./lib'); | ||
|
||
exports.sync = lib.genericSync('sleep', function(pi) { | ||
return '/sleeperService/getAllDatesWithSleepData'; | ||
}, function(pi) { | ||
return '?key=' + pi.auth.appKey; | ||
}, function(pi, js, cb) { | ||
var dates = js.response.dateList.date; | ||
|
||
if (!js || !dates) { | ||
return []; | ||
} | ||
|
||
// For each date with a sleep record... | ||
async.mapSeries(dates, function(date, mapped) { | ||
// Retrieve the record... | ||
lib.callApi(pi, function(data) { | ||
var js; | ||
|
||
try { | ||
js = JSON.parse(data); | ||
} catch(E) { | ||
mapped(E); | ||
} | ||
|
||
var record = js.response.sleepRecord; | ||
|
||
if (!js || !record) { | ||
mapped(); | ||
} | ||
|
||
record.id = record.startDate.year + '-' + | ||
record.startDate.month + '-' + | ||
record.startDate.day; | ||
|
||
mapped(null, record); | ||
}, function(pi) { | ||
return '/sleeperService/getSleepRecordForDate'; | ||
}, function(pi) { | ||
return '?key=' + pi.auth.appKey + '&date=' + date.year + '-' + date.month + '-' + date.day; | ||
}); | ||
}, function(err, results){ | ||
// And return them all up the chain. | ||
cb(results); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"synclets": [ | ||
{ "name": "sleep", "frequency": 86400, "threshold": 0 } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var fs = require('fs'); | ||
var pi = JSON.parse(fs.readFileSync("../../Me/zeo/me.json")); | ||
|
||
var sync = require(process.argv[2]); | ||
|
||
sync.sync(pi, function(e, js) { | ||
console.error('error', e); | ||
|
||
console.error("got js", JSON.stringify(js)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters