diff --git a/README.md b/README.md index 40e1edf..ba364fd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ GistDB ====== +Install: + npm install gistdb + This project was started just to learn the gist api. This project is [Unlicensed](http://unlicense.org/ "Title") diff --git a/lib/GistDB.js b/lib/GistDB.js index 5985955..d463ef7 100644 --- a/lib/GistDB.js +++ b/lib/GistDB.js @@ -25,6 +25,7 @@ this.emit(listener,obj); } else { console.log("ERROR: Missing event listener(s) for '"+listener+"'."); + return false; } }; diff --git a/lib/http_get.js b/lib/http_get.js index 4e36037..904d49c 100644 --- a/lib/http_get.js +++ b/lib/http_get.js @@ -1,6 +1,6 @@ (function(){ http_get = function (url, options, cb) { - var opts = {}, p, timeout; + var opts = {}, p; if (!cb) { cb = options; options = {}; @@ -24,7 +24,17 @@ if (opts.username && opts.password) { opts.auth = opts.username+":"+opts.password; } - opts.method = (opts.postData?"POST":"GET"); + if (opts.type) { + opts.type = opts.type.toLowerCase(); + if (opts.type == 'head') { + opts.method = 'HEAD'; + } + } else { + opts.type = 'default'; + } + if (!opts.method) { + opts.method = (opts.postData?"POST":"GET"); + } if (opts.method == 'POST' || opts.method == 'PUT') { opts.headers["Content-type"] = "application/x-www-form-urlencoded"; opts.headers["Content-length"] = opts.postData.length; @@ -35,26 +45,27 @@ res.on('data', function(chunk) { body += chunk; }).on('end', function() { - if (timeout) { - clearTimeout(timeout); + if (this.timeout) { + clearTimeout(this.timeout); } if (opts.type) { - if (opts.type.toLowerCase() != 'auto') { - return cb(returnType(opts.type, body),null,res); + if (opts.type != 'default') { + var parsed = returnType(opts.type, body, res); + return cb((parsed?parsed:"Could not parse content."),!parsed,res); } } cb(body,null,res); - }); + }.bind(this)); }); if (opts.timeout) { - timeout = setTimeout(function(){ + req.timeout = setTimeout(function(){ this.abort(); this.emit('error',{message:'request reached timeout'}); - }.bind(req),opts.timeout); + }.bind(req),(!isNaN(opts.timeout)?opts.timeout:30000)); } req.on('error', function(e) { - if (timeout) { - clearTimeout(timeout); + if (this.timeout) { + clearTimeout(this.timeout); } if (!this.haderr) { cb(e.message,1,null); @@ -65,17 +76,23 @@ req.write(opts.postData); } req.end(); - function returnType (type, body) { - switch (type.toLowerCase()) { - case 'jsdom': - var jsdom = require("jsdom") - var _jsdom = new jsdom.jsdom(body,null,{features:{QuerySelector: true}}); - return _jsdom.createWindow(); + function returnType (type, body, res) { + switch (type) { + case 'head': + return res.headers||{}; case 'json': - return JSON.parse(body); + try { + return JSON.parse(body); + } catch (e) { + return false; + } case 'xml': - var parser = require('xml2json'); - return JSON.parse(parser.toJson(body)); + try { + var parser = require('xml2json'); + return JSON.parse(parser.toJson(body)); + } catch (e) { + return false; + } default: return body; } diff --git a/package.json b/package.json index d97052b..266a1e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gistdb", - "version": "0.0.3", + "version": "0.0.4", "author": { "name": "Louis T.", "email": "louist@ltdev.im", @@ -21,7 +21,10 @@ "database", "github" ], + "scripts": { + "test": "node test.js && test2.js" + }, "description": "Use gist's from GitHub as database files.", - "engine": "node => 0.6.6", + "engine": "node => 0.6", "main": "index.js" } diff --git a/test.js b/test.js new file mode 100644 index 0000000..3de5dc0 --- /dev/null +++ b/test.js @@ -0,0 +1,29 @@ +/* + Yes, I realize these tests are super simple and need to be improved. + + Test 1, read example database. +*/ +console.log("Test 1: print from an example database."); +var GistDB = require('./'); +var gdb = new GistDB({id:'9879133ed9dd415d7199'}); +gdb.load(); +gdb.on('loaded',function (data) { + if (this.content.Example0 == "3100.205226801336") { + console.log('Sucessful load.'); + } else { + console.log('Example0 failed.'); + } + console.log('Current data: '+JSON.stringify(this.content)); + runTest2(); +}); +/* + Test 2, no error handler. +*/ +function runTest2 () { + console.log("\nTest 2: No error handler.\n"); + var gdb = new GistDB({user:'USERNAME',pass:'PASSWORD',id:'9879133ed9dd415d7199',timeout:5000}); + gdb.load(); + gdb.on('loaded',function (data) { + console.log('Current data: '+JSON.stringify(data)); + }); +}