Skip to content

Instantly share code, notes, and snippets.

@kirel
Forked from nbqx/simplenote.js
Created January 14, 2012 21:15
Show Gist options
  • Save kirel/1612922 to your computer and use it in GitHub Desktop.
Save kirel/1612922 to your computer and use it in GitHub Desktop.
SimpleNote API via node.js
var request = require('request'),
querystring = require('querystring');
var Note = function(key){
this.key = key;
}
Note.prototype = new process.EventEmitter();
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.getNote = function(key){
var self = this;
var note = new Note(key);
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){
note.content = bdy;
note.emit('load');
}else{ throw err }
});
}.bind(self));
return note
}
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