Skip to content

Instantly share code, notes, and snippets.

@nbqx
Created December 28, 2010 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nbqx/756886 to your computer and use it in GitHub Desktop.
Save nbqx/756886 to your computer and use it in GitHub Desktop.
SimpleNote API via node.js
var request = require('request'),
querystring = require('querystring');
var SimpleNote = function(email, passwd){
this.email = email;
this.passwd = passwd;
this.encoded_body = (new Buffer(this.toQuery({email:this.email,password:this.passwd}))).toString("base64");
this.token = undefined;
this.baseURL = "https://simple-note.appspot.com/api";
var Auth = function(){};
Auth.prototype = new process.EventEmitter();
this.auth = new Auth();
this.auth.on('login', function(){
request({uri:this.baseURL+'/login', method:'POST', body:this.encoded_body}, function(err,res,bdy){
if(!err && res.statusCode==200){
this.token = bdy;
}
}.bind(this));
}.bind(this));
this.auth.on('connected', function(fn){
var timer = setInterval(function(){
if(this.token!=undefined){
fn();
clearInterval(timer);
}
}.bind(this), 1000);
}.bind(this));
this.auth.emit('login');
this.auth.emit('connected', function(){return true}.bind(this));
return this
}
SimpleNote.prototype.list = function(cbk){
var self = this;
self.auth.emit('connected', function(){
request({uri:this.baseURL+'/index?'+this.toQuery({email:this.email, auth:this.token}), method:'GET'}, function(err,res,bdy){
if(!err && res.statusCode==200){
var jsn = JSON.parse(bdy);
if(cbk) cbk(jsn);
}else{ throw err }
});
}.bind(self));
}
SimpleNote.prototype.search = function(q, cbk){
var self = this;
var results = 10;
self.auth.emit('connected', function(){
request({uri:this.baseURL+'/search?'+this.toQuery({email:this.email, auth:this.token, query:q, results:results}), method:'GET'}, function(err,res,bdy){
if(!err && res.statusCode==200){
var jsn = JSON.parse(bdy);
if(cbk) cbk(jsn);
}else{ throw err }
});
}.bind(self));
}
SimpleNote.prototype.get = function(key,cbk){
var self = this;
self.auth.emit('connected', function(){
request({uri:this.baseURL+'/note?'+this.toQuery({email:this.email, auth:this.token, key:key}), method:'GET'}, function(err,res,bdy){
if(!err && res.statusCode==200){
if(cbk) cbk(bdy);
}else{ throw err }
});
}.bind(self));
};
SimpleNote.prototype.create = function(content,cbk){
var self = this;
self.auth.emit('connected', function(){
request({uri:this.baseURL+'/note?'+this.toQuery({email:this.email, auth:this.token}), method:'POST', body:this.toBase64(content)}, function(err,res,bdy){
if(!err && res.statusCode==200){
if(cbk) cbk(bdy);
}else{ throw err }
});
}.bind(self));
};
SimpleNote.prototype.update = function(key, content, cbk){
var self = this;
self.auth.emit('connected', function(){
request({uri:this.baseURL+'/note?'+this.toQuery({email:this.email, auth:this.token, key:key}), method:'POST', body:this.toBase64(content)}, function(err,res,bdy){
if(!err && res.statusCode==200){
if(cbk) cbk(bdy);
}else{ throw err }
});
}.bind(self));
}
SimpleNote.prototype.delete = function(key,cbk){
var self = this;
self.auth.emit('connected', function(){
request({uri:this.baseURL+'/delete?'+this.toQuery({email:this.email, auth:this.token, key:key}), method:'GET'}, function(err,res,bdy){
if(!err && res.statusCode==200){
if(cbk) cbk(bdy);
}else{ throw err }
});
}.bind(self));
}
SimpleNote.prototype.toQuery = function(obj){
return querystring.stringify(obj)
}
SimpleNote.prototype.toBase64 = function(str){
return (new Buffer(str)).toString('base64')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment