Skip to content

Commit

Permalink
[0.0.4] Commit for NPM update. - Small fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
louis committed Aug 1, 2012
1 parent fb787d6 commit 2683468
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 22 deletions.
3 changes: 3 additions & 0 deletions 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")
Expand Down
1 change: 1 addition & 0 deletions lib/GistDB.js
Expand Up @@ -25,6 +25,7 @@
this.emit(listener,obj);
} else {
console.log("ERROR: Missing event listener(s) for '"+listener+"'.");
return false;
}
};

Expand Down
57 changes: 37 additions & 20 deletions 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 = {};
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "gistdb",
"version": "0.0.3",
"version": "0.0.4",
"author": {
"name": "Louis T.",
"email": "louist@ltdev.im",
Expand All @@ -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"
}
29 changes: 29 additions & 0 deletions 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));
});
}

0 comments on commit 2683468

Please sign in to comment.