From 29ab9baed18303e5bbf3d31be00cb4975d1ac94d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Ingi=20Mikaelsson?= Date: Wed, 6 Mar 2013 18:30:23 +0000 Subject: [PATCH] New pattern for loading endpoints. More tests and fixes in various functions --- endpoints/bus/index.js | 9 ++- endpoints/car/index.js | 6 +- endpoints/company/index.js | 5 +- endpoints/currency/arion.js | 6 +- endpoints/currency/index.js | 7 ++- endpoints/currency/m5.js | 6 +- endpoints/domainlookup/index.js | 4 +- endpoints/flight/index.js | 7 ++- endpoints/frontpage/index.js | 9 ++- endpoints/house/index.js | 9 ++- endpoints/static_endpoints/help_out.js | 5 ++ endpoints/static_endpoints/phone.js | 10 ++++ endpoints/word/index.js | 6 +- lib/endpoints.js | 80 ++------------------------ lib/helpers.js | 2 + package.json | 3 +- test/helpers.js | 5 ++ test/test-server.js | 5 ++ 18 files changed, 92 insertions(+), 92 deletions(-) create mode 100644 endpoints/static_endpoints/help_out.js create mode 100644 endpoints/static_endpoints/phone.js create mode 100644 test/helpers.js create mode 100644 test/test-server.js diff --git a/endpoints/bus/index.js b/endpoints/bus/index.js index 0b3edc44..b453a338 100644 --- a/endpoints/bus/index.js +++ b/endpoints/bus/index.js @@ -1,4 +1,9 @@ -exports.search = function(req, res, next) { +exports.setup = function(){ + server.post({path: '/bus/search', version: '1.0.0'}, search); + server.post({path: '/bus/realtime', version: '1.0.0'}, realtime); +} + +search = function(req, res, next) { res.json(404,{error:"This api endpoint has been closed temporarily, because Bus.is changed it's markup."}); return next(); @@ -191,7 +196,7 @@ exports.search = function(req, res, next) { } -exports.realtime = function(req, res, next){ +realtime = function(req, res, next){ var data = req.params; request('http://straeto.is/bitar/bus/livemap/json.jsp', function (error, response, body) { diff --git a/endpoints/car/index.js b/endpoints/car/index.js index 826f85d1..09acd6ea 100644 --- a/endpoints/car/index.js +++ b/endpoints/car/index.js @@ -1,4 +1,8 @@ -exports.slash = function(req, res, next){ +exports.setup = function(){ + server.post({path: '/car', version: '1.0.0'}, slash); +} + +slash = function(req, res, next){ var data = req.params; if(!data.number){ diff --git a/endpoints/company/index.js b/endpoints/company/index.js index d5243ce5..5515f404 100644 --- a/endpoints/company/index.js +++ b/endpoints/company/index.js @@ -1,4 +1,7 @@ -exports.slash = function(req, res, next){ +exports.setup = function(){ + server.post({path: '/company', version: '1.0.0'}, slash); +} +slash = function(req, res, next){ res.charSet = 'utf8'; var data = req.params; diff --git a/endpoints/currency/arion.js b/endpoints/currency/arion.js index 1ec1936b..12330332 100644 --- a/endpoints/currency/arion.js +++ b/endpoints/currency/arion.js @@ -1,4 +1,8 @@ -exports.slash = function(req, res, next){ +exports.setup = function(){ + server.get({path: '/currency/arion', version: '1.0.0'}, slash); +} + +slash = function(req, res, next){ res.charSet = 'utf8'; diff --git a/endpoints/currency/index.js b/endpoints/currency/index.js index f10ea16f..2c6b972d 100644 --- a/endpoints/currency/index.js +++ b/endpoints/currency/index.js @@ -1,4 +1,8 @@ -exports.slash = function(req, res, next){ +exports.setup = function(){ + server.post({path: '/currency', version: '1.0.0'}, slash); +} + +slash = function(req, res, next){ var data = req.params; res.charSet = 'utf8'; @@ -138,5 +142,4 @@ exports.slash = function(req, res, next){ res.json(404,{error:'This provider does not exist',code:2}); return next(); } - } \ No newline at end of file diff --git a/endpoints/currency/m5.js b/endpoints/currency/m5.js index 53f1bc2f..b951e95b 100644 --- a/endpoints/currency/m5.js +++ b/endpoints/currency/m5.js @@ -1,4 +1,8 @@ -exports.slash = function(req, res, next){ +exports.setup = function(){ + server.get({path: '/currency/m5', version: '1.0.0'}, slash); +} + +slash = function(req, res, next){ res.charSet = 'utf8'; var currencyNames = { diff --git a/endpoints/domainlookup/index.js b/endpoints/domainlookup/index.js index c1217009..0319c3b3 100644 --- a/endpoints/domainlookup/index.js +++ b/endpoints/domainlookup/index.js @@ -1,2 +1,4 @@ //Possible endpoint: -//https://www.isnic.is/is/whois/mini.php?type=all&query=apis.is \ No newline at end of file +//https://www.isnic.is/is/whois/mini.php?type=all&query=apis.is +exports.setup = function(){ +} \ No newline at end of file diff --git a/endpoints/flight/index.js b/endpoints/flight/index.js index 6fc24268..f5b5dad0 100644 --- a/endpoints/flight/index.js +++ b/endpoints/flight/index.js @@ -1,4 +1,8 @@ -exports.slash = function(req, res, next){ +exports.setup = function(){ + server.post({path: '/flight', version: '1.0.0'}, slash); +} + +slash = function(req, res, next){ var data = req.params; @@ -17,6 +21,7 @@ exports.slash = function(req, res, next){ url = 'http://www.kefairport.is/English/Timetables/Arrivals/'; }else{ res.json(200,{'error':'Wrong parameters given!'}); + return next(); } request.get({ diff --git a/endpoints/frontpage/index.js b/endpoints/frontpage/index.js index 3f1f7d02..3d574f0d 100644 --- a/endpoints/frontpage/index.js +++ b/endpoints/frontpage/index.js @@ -1,4 +1,9 @@ -exports.getSlash = function(req, res, next) { +exports.setup = function(){ + server.get({path: '/'}, getSlash); + server.post({path: '/'}, postSlash); +} + +getSlash = function(req, res, next) { // fs.readFile('public/index.html', 'utf8', function (err,data) { // if (err) { // h.logError(err,err.stack); @@ -20,7 +25,7 @@ exports.getSlash = function(req, res, next) { return next(false); } -exports.postSlash = function(req, res, next) { +postSlash = function(req, res, next) { res.json(200,{"info":"Velkominn á apis.is! Kíktu á docs.apis.is í vafranum þínum fyrir frekari upplýsingar!"}); return next(); } \ No newline at end of file diff --git a/endpoints/house/index.js b/endpoints/house/index.js index aaaec9e0..30284a9e 100644 --- a/endpoints/house/index.js +++ b/endpoints/house/index.js @@ -1,5 +1,10 @@ -//Ókl! -exports.slash = function(req, res, next){ +//Incomplete + +exports.setup = function(){ + server.post({path: '/house', version: '1.0.0'}, slash); +} + +slash = function(req, res, next){ var data = req.params; res.charSet = 'utf8'; diff --git a/endpoints/static_endpoints/help_out.js b/endpoints/static_endpoints/help_out.js new file mode 100644 index 00000000..e11c4587 --- /dev/null +++ b/endpoints/static_endpoints/help_out.js @@ -0,0 +1,5 @@ +exports.setup = function(){ + server.post({path: '/help_out', version: '1.0.0'}, function(req, res, next){ + res.json(200,{message:'Send us mail: apis@apis.is ,thanks for your interest!'}); + }); +} \ No newline at end of file diff --git a/endpoints/static_endpoints/phone.js b/endpoints/static_endpoints/phone.js new file mode 100644 index 00000000..112fc187 --- /dev/null +++ b/endpoints/static_endpoints/phone.js @@ -0,0 +1,10 @@ +exports.setup = function(){ + server.post({path: '/phone', version: '1.0.0'}, function(req, res, next){ + res.json(410,{error:"This api endpoint has been closed and it will not be available in the foreseeable future."}); + return next(); + }); + server.post({path: '/phone', version: '2.0.0'}, function(req, res, next){ + res.json(410,{error:"This api endpoint has been closed and it will not be available in the foreseeable future."}); + return next(); + }); +} \ No newline at end of file diff --git a/endpoints/word/index.js b/endpoints/word/index.js index 01239d16..cece9455 100644 --- a/endpoints/word/index.js +++ b/endpoints/word/index.js @@ -1,5 +1,9 @@ //Very incomplete -exports.slash = function(req, res, next){ +exports.setup = function(){ + server.post({path: '/word', version: '1.0.0'}, slash); +} + +slash = function(req, res, next){ var data = req.params; diff --git a/lib/endpoints.js b/lib/endpoints.js index 3a0d0f5e..76e463b3 100644 --- a/lib/endpoints.js +++ b/lib/endpoints.js @@ -1,88 +1,16 @@ -/** - * Registers an endpoint onto the server handle - */ -function register(endpoint,data){ - console.log('- '+endpoint); - switch(endpoint){ - case 'frontpage': - server.get({path: '/'}, data.getSlash); - server.post({path: '/'}, data.postSlash); - break; - case 'bus': - server.post({path: '/bus/search', version: '1.0.0'}, data.search); - server.post({path: '/bus/realtime', version: '1.0.0'}, data.realtime); - break; - case 'sms': - server.post({path: '/sms', version: '1.0.0'}, data.slash); - break; - case 'currency': - server.post({path: '/currency', version: '1.0.0'}, data.slash); - break; - case 'currency.m5': - server.get({path: '/currency/m5', version: '1.0.0'}, data.slash); - break; - case 'currency.arion': - server.get({path: '/currency/arion', version: '1.0.0'}, data.slash); - break; - case 'company': - server.post({path: '/company', version: '1.0.0'}, data.slash); - break; - case 'car': - server.post({path: '/car', version: '1.0.0'}, data.slash); - break; - case 'flight': - server.post({path: '/flight', version: '1.0.0'}, data.slash); - break; - case 'house': - //Incomplete - server.post({path: '/house', version: '1.0.0'}, data.slash); - break; - case 'word': - //Incomplete - server.post({path: '/word', version: '1.0.0'}, data.slash); - break; - case 'phone': - server.post({path: '/phone', version: '1.0.0'}, function(req, res, next){ - res.json(410,{error:"This api endpoint has been closed and it will not be available in the foreseeable future."}); - return next(); - }); - server.post({path: '/phone', version: '2.0.0'}, function(req, res, next){ - res.json(410,{error:"This api endpoint has been closed and it will not be available in the foreseeable future."}); - return next(); - }); - break; - case 'help_out': - server.post({path: '/help_out', version: '1.0.0'}, function(req, res, next){ - res.json(200,{message:'Send us mail: apis@apis.is ,thanks for your interest!'}); - }); - break; - default: - break; - } -} - /** * Loads upp all the required endpoints */ -exports.load = function(){ - console.log('Registering endpoints:'); +exports.load = function(){ //Load all endpoints in the endpoints folder //walk is blocking on purpose because the server can't listen yet fileModule.walk('./endpoints', function(a, dirPath, dirs, files){ if(files){ files.forEach(function(file,key){ - var fileName = file.replace('/index.js','') - .replace('.js','') - .replace('endpoints/','') - .replace('/','.'), - requiredData = require('../'+file); - register(fileName,requiredData); - }) + //Setup the endpoint via the setup function + require('../'+file).setup(); + }); } }); - - //Register endpoins that do not have file associated. - register('phone',null); - register('help_out',null); } \ No newline at end of file diff --git a/lib/helpers.js b/lib/helpers.js index 5cf38dd0..4add0abc 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -164,6 +164,7 @@ exports.logError = function(error,stack){ //var data = {error:error,stack:stack,timestamp:h.mysqlTimestamp()}; console.log(error); console.log(stack) + return true; } /** @@ -171,6 +172,7 @@ exports.logError = function(error,stack){ */ exports.logVisit = function(endpoint,data,testMode){ // var data = {endpoint:endpoint,data:JSON.stringify(data),timestamp:h.mysqlTimestamp(),test:1} + return true; } /** diff --git a/package.json b/package.json index e4df2099..a5bb5b80 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "jquery": "1.8.x", "scraper" : "0.0.9", "moment": "2.0.0", - "file": "0.1.x" + "file": "0.1.x", + "nodeunit": "0.7.x" } } \ No newline at end of file diff --git a/test/helpers.js b/test/helpers.js new file mode 100644 index 00000000..2aa3abf5 --- /dev/null +++ b/test/helpers.js @@ -0,0 +1,5 @@ +exports['check-globals'] = function (test) { + var server = require('../server.js'); + test.ok(server.moment,'Moment.js module not loaded properly'); + test.done(); +}; \ No newline at end of file diff --git a/test/test-server.js b/test/test-server.js new file mode 100644 index 00000000..e26a34c1 --- /dev/null +++ b/test/test-server.js @@ -0,0 +1,5 @@ +exports['check-globals'] = function (test) { + var server = require('../server.js'); + test.ok(server.moment); + test.done(); +}; \ No newline at end of file