Skip to content

Commit

Permalink
Merge pull request #942 from beaugunderson/zeo
Browse files Browse the repository at this point in the history
Adding the Zeo connector (with 1 synclet: sleep)
  • Loading branch information
quartzjer committed Mar 21, 2012
2 parents a5f3872 + 4780a1e commit 7612b49
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Connectors/Zeo/auth.js
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>');
}
};
62 changes: 62 additions & 0 deletions Connectors/Zeo/lib.js
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);
};
};
21 changes: 21 additions & 0 deletions Connectors/Zeo/package.json
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": {}
}
47 changes: 47 additions & 0 deletions Connectors/Zeo/sleep.js
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);
});
});
5 changes: 5 additions & 0 deletions Connectors/Zeo/synclets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"synclets": [
{ "name": "sleep", "frequency": 86400, "threshold": 0 }
]
}
10 changes: 10 additions & 0 deletions Connectors/Zeo/test.js
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));
});
1 change: 1 addition & 0 deletions Ops/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ exports.app = function (app) {

app.get('/auth/:id', authIsAwesome);
app.get('/auth/:id/auth', authIsAuth);
app.post('/auth/:id/auth', express.bodyParser(), authIsAuth);

app.get('/deauth/:id', deauthIsAwesomer);
};
Expand Down

0 comments on commit 7612b49

Please sign in to comment.