Skip to content

Commit

Permalink
New pattern for loading endpoints. More tests and fixes in various fu…
Browse files Browse the repository at this point in the history
…nctions
  • Loading branch information
kristjanmik committed Mar 6, 2013
1 parent 7fd7271 commit 29ab9ba
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 92 deletions.
9 changes: 7 additions & 2 deletions endpoints/bus/index.js
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion endpoints/car/index.js
Original file line number Diff line number Diff line change
@@ -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){
Expand Down
5 changes: 4 additions & 1 deletion endpoints/company/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 5 additions & 1 deletion endpoints/currency/arion.js
Original file line number Diff line number Diff line change
@@ -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';


Expand Down
7 changes: 5 additions & 2 deletions endpoints/currency/index.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -138,5 +142,4 @@ exports.slash = function(req, res, next){
res.json(404,{error:'This provider does not exist',code:2});
return next();
}

}
6 changes: 5 additions & 1 deletion endpoints/currency/m5.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
4 changes: 3 additions & 1 deletion endpoints/domainlookup/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
//Possible endpoint:
//https://www.isnic.is/is/whois/mini.php?type=all&query=apis.is
//https://www.isnic.is/is/whois/mini.php?type=all&query=apis.is
exports.setup = function(){
}
7 changes: 6 additions & 1 deletion endpoints/flight/index.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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({
Expand Down
9 changes: 7 additions & 2 deletions endpoints/frontpage/index.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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();
}
9 changes: 7 additions & 2 deletions endpoints/house/index.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
5 changes: 5 additions & 0 deletions endpoints/static_endpoints/help_out.js
Original file line number Diff line number Diff line change
@@ -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: [email protected] ,thanks for your interest!'});
});
}
10 changes: 10 additions & 0 deletions endpoints/static_endpoints/phone.js
Original file line number Diff line number Diff line change
@@ -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();
});
}
6 changes: 5 additions & 1 deletion endpoints/word/index.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
80 changes: 4 additions & 76 deletions lib/endpoints.js
Original file line number Diff line number Diff line change
@@ -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: [email protected] ,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);
}
2 changes: 2 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,15 @@ exports.logError = function(error,stack){
//var data = {error:error,stack:stack,timestamp:h.mysqlTimestamp()};
console.log(error);
console.log(stack)
return true;
}

/**
* Logs endpoints visits
*/
exports.logVisit = function(endpoint,data,testMode){
// var data = {endpoint:endpoint,data:JSON.stringify(data),timestamp:h.mysqlTimestamp(),test:1}
return true;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
5 changes: 5 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -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();
};
5 changes: 5 additions & 0 deletions test/test-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports['check-globals'] = function (test) {
var server = require('../server.js');
test.ok(server.moment);
test.done();
};

0 comments on commit 29ab9ba

Please sign in to comment.