Skip to content

Commit

Permalink
Code cleanup. First set of doc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanmik committed Mar 28, 2013
1 parent db186cc commit f290fdd
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 128 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ endpoints/phone.js
endpoints/phone2.js
endpoints/phone
endpoints/sms
lib/phantom_daemon.js
endpoints/closed_endpoints
lib/phantom_daemon.js
index.php
docs
test
19 changes: 9 additions & 10 deletions endpoints/bus/realtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
exports.setup = function(){
server.post({path: '/bus/realtime', version: '1.0.0'}, realtime);
server.post({path: '/bus/realtime', version: '1.0.0'}, realtime); //Old

server.get({path: '/bus/realtime', version: '1.0.0'}, realtime);
}

realtime = function(req, res, next){
Expand All @@ -8,12 +10,12 @@ realtime = function(req, res, next){
request('http://straeto.is/bitar/bus/livemap/json.jsp', function (error, response, body) {
if(error) {
h.logError(error,error.stack);
res.json(500,{error:'Something went wrong',code:3});
res.json(500,{error:'Something went wrong on the server'});
return next();
}
if(response.statusCode !== 200){
h.logError('Wrong status code in /bus/realtime:'+res.statusCode,'');
res.json(500,{error:"Something went wrong code 1",code:4});
res.json(500,{error:"Something went wrong on the server"});
return next();
}

Expand Down Expand Up @@ -56,14 +58,14 @@ realtime = function(req, res, next){
}
if(response.statusCode !== 200){
h.logError('Wrong status code in /bus/realtime:'+res.statusCode,'');
res.json(500,{error:"Something went wrong code 1",code:4});
res.json(500,{error:"Something went wrong on the server"});
return next();
}

try{
var data = JSON.parse(body);
}catch(e){
res.json(500,{error:"Something went wrong"});
res.json(500,{error:"Something went wrong on the server"});
return next();
}

Expand Down Expand Up @@ -97,11 +99,8 @@ realtime = function(req, res, next){
objRoutes.results.push(objRoute);
});

if(req.header('Uptime-Test') == 'true'){
h.logVisit('/bus/search', objRoutes,true);
}else{
h.logVisit('/bus/search', objRoutes,false);
}
h.logVisit('/bus/search', objRoutes,false);

res.json(200,objRoutes)
return next();
});
Expand Down
21 changes: 11 additions & 10 deletions endpoints/car/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
exports.setup = function(){
server.post({path: '/car', version: '1.0.0'}, lookup);
server.post({path: '/car', version: '1.0.0'}, lookup); //Old

server.get({path: '/car', version: '1.0.0'}, lookup);
}

lookup = function(req, res, next){
Expand All @@ -16,7 +18,7 @@ lookup = function(req, res, next){
url: 'http://www.us.is/upplysingar_um_bil?vehinumber='+data.number
}, function(error, response, body){
if(error){
res.json(500,{error:"Something went wrong"});
res.json(500,{error:"Something went wrong on the server"});
return next();
}
var data = $(body),
Expand All @@ -35,9 +37,11 @@ lookup = function(req, res, next){
'weight': 0
}

var fields = ['registryNumber','number','factoryNumber','type','subType','color','registeredAt','status','nextCheck','pollution','weight'];
var nothingFound = data.find('table tr td').html();
if( nothingFound.indexOf('Ekkert ökutæki fannst') == -1){
var fields = ['registryNumber','number','factoryNumber','type','subType','color','registeredAt','status','nextCheck','pollution','weight'],

nothingFound = data.find('table tr td').html();

if(nothingFound.indexOf('Ekkert ökutæki fannst') == -1){
//Found something
data.find('table tr').each(function(key){
var val = $(this).find('b').html();
Expand All @@ -48,11 +52,8 @@ lookup = function(req, res, next){
obj.results.push(car);
}

if(req.header('Uptime-Test') == 'true'){
h.logVisit('/car', obj,true);
}else{
h.logVisit('/car', obj,false);
}
h.logVisit('/car', obj,false);

res.json(200,obj)
return next();
});
Expand Down
11 changes: 5 additions & 6 deletions endpoints/company/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
exports.setup = function(){
server.post({path: '/company', version: '1.0.0'}, lookup);
server.post({path: '/company', version: '1.0.0'}, lookup); //Old

server.get({path: '/company', version: '1.0.0'}, lookup);
}
lookup = function(req, res, next){
res.charSet = 'utf8';
Expand Down Expand Up @@ -94,11 +96,8 @@ lookup = function(req, res, next){
});
}

if(req.header('Uptime-Test') == 'true'){
h.logVisit('/company', obj,true);
}else{
h.logVisit('/company', obj,false);
}
h.logVisit('/company', obj,false);

res.json(200,obj)
return next();

Expand Down
12 changes: 5 additions & 7 deletions endpoints/currency/arion.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ slash = function(req, res, next){
body: toSend
}, function(error, response, body){
if(error){
res.json(500,{error:"Something went wrong"});
res.json(500,{error:"Something went wrong on the server"});
return next();
}

var jsonObject = JSON.parse(body),
obj = { results: []};
obj = { results: [] };

jsonObject.forEach(function(object,i){
var changePer = parseFloat(object.LastValueChange)/parseFloat(object.MidValue),
Expand All @@ -37,11 +37,9 @@ slash = function(req, res, next){

obj.results.push(currency)
});
if(req.header('Uptime-Test') == 'true'){
h.logVisit('/currency', obj,true);
}else{
h.logVisit('/currency', obj,false);
}

h.logVisit('/currency', obj,false);

res.json(200,obj);
return next();
});
Expand Down
19 changes: 7 additions & 12 deletions endpoints/currency/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ slash = function(req, res, next){
url: 'http://www.m5.is/?gluggi=forsida'
}, function(error, response, body){
if(error){
res.json(500,{error:"Something went wrong"});
res.json(500,{error:"Something went wrong on the server"});
return next();
}

Expand Down Expand Up @@ -86,11 +86,8 @@ slash = function(req, res, next){

});

if(req.header('Uptime-Test') == 'true'){
h.logVisit('/currency', obj,true);
}else{
h.logVisit('/currency', obj,false);
}
h.logVisit('/currency', obj,false);

res.json(200,obj);
return next();

Expand All @@ -105,7 +102,7 @@ slash = function(req, res, next){
body: toSend
}, function(error, response, body){
if(error){
res.json(500,{error:"Something went wrong"});
res.json(500,{error:"Something went wrong on the server"});
return next();
}

Expand All @@ -129,11 +126,9 @@ slash = function(req, res, next){

obj.results.push(currency)
});
if(req.header('Uptime-Test') == 'true'){
h.logVisit('/currency', obj,true);
}else{
h.logVisit('/currency', obj,false);
}

h.logVisit('/currency', obj,false);

res.json(200,obj);
return next();

Expand Down
65 changes: 32 additions & 33 deletions endpoints/currency/lb.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
var parseString = require('xml2js').parseString;
exports.setup = function(){
server.get({path: '/currency/lb', version: '1.0.0'}, getCurrencies);
};

var getCurrencies = function(req, res, next){
res.charSet = 'utf8';
res.charSet = 'utf8';

// A = Almennt gengi, S = Seðlagengi
var type = req.params['type'] || 'A';
// A = Almennt gengi, S = Seðlagengi
var type = req.params['type'] || 'A';

request.get({
url: 'http://www.landsbankinn.is/modules/markets/services/XMLGengi.asmx/NyjastaGengiByType?strTegund=' + type
}, function(err, response, xml) {
if (err) {
res.json(500, { error: 'Something went wrong' });
return next();
}
request.get({
url: 'http://www.landsbankinn.is/modules/markets/services/XMLGengi.asmx/NyjastaGengiByType?strTegund=' + type
}, function(err, response, xml) {
if (err) {
res.json(500, { error: 'Something went wrong' });
return next();
}

var currencies = [];
parseString(xml, { explicitRoot: false }, function(err, result) {
var arr = result.GjaldmidillRow;
for (var i = 0, currency; currency = arr[i]; i++) {
currencies.push({
shortName: currency.Mynt[0],
longName: currency.Heiti[0],
value: parseFloat(currency.Midgengi),
askValue: parseFloat(currency.Kaup),
bidValue: parseFloat(currency.Sala),
changeCur: parseFloat(currency.Breyting[0]),
changePer: parseFloat((parseFloat(currency.Breyting) / parseFloat(currency.Midgengi)).toFixed(2))
});
}
res.json(200, { results: currencies });
next();
});
});
};

exports.setup = function(){
server.get({path: '/currency/lb', version: '1.0.0'}, getCurrencies);
var currencies = [];
parseString(xml, { explicitRoot: false }, function(err, result) {
var arr = result.GjaldmidillRow;
for (var i = 0, currency; currency = arr[i]; i++) {
currencies.push({
shortName: currency.Mynt[0],
longName: currency.Heiti[0],
value: parseFloat(currency.Midgengi),
askValue: parseFloat(currency.Kaup),
bidValue: parseFloat(currency.Sala),
changeCur: parseFloat(currency.Breyting[0]),
changePer: parseFloat((parseFloat(currency.Breyting) / parseFloat(currency.Midgengi)).toFixed(2))
});
}
res.json(200, { results: currencies });
return next();
});
}
);
};
16 changes: 8 additions & 8 deletions endpoints/currency/m5.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
exports.setup = function() {
server.get({path: '/currency/m5', version: '1.0.0'}, getCurrencies);
};

var getCurrencies = function (req, res, next) {
res.charSet = 'utf8';
Expand All @@ -12,17 +15,18 @@ var getCurrencies = function (req, res, next) {
url: 'http://www.m5.is/?gluggi=gjaldmidlar'
}, function(err, response, body) {
if (err) {
res.json(500, { error:"Something went wrong" });
res.json(500, { error:"Something went wrong on the server" });
return next();
}

var data = $(body),
currencies = [];
currencies = [];

data.find('.table-striped tr').each(function () {
var tds = $(this).find('td'),
name = tds.eq(0).text();
name && currencies.push({
name = tds.eq(0).text();

name && currencies.push({
shortName: name,
longName: h.currency[name].long,
value: parseFloat(tds.eq(2).text().replace(',', '.')),
Expand All @@ -36,8 +40,4 @@ var getCurrencies = function (req, res, next) {
res.json(200, { results: currencies });
return next();
});
};

exports.setup = function() {
server.get({path: '/currency/m5', version: '1.0.0'}, getCurrencies);
};
18 changes: 8 additions & 10 deletions endpoints/flight/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
exports.setup = function(){
server.post({path: '/flight', version: '1.0.0'}, slash); //Old

server.post({path: '/flight', version: '1.0.0'}, slash);
}

slash = function(req, res, next){

var data = req.params;
var data = req.params,
url = '';

if(!data.type) data.type = '';
if(!data.language) data.language = '';

var url = '';

if(data.type == 'departures' && data.language == 'is'){
url = 'http://www.kefairport.is/Flugaaetlun/Brottfarir/';
}else if(data.type == 'departures' && data.language == 'en'){
Expand All @@ -20,7 +21,7 @@ slash = function(req, res, next){
}else if(data.type == 'arrivals' && data.language == 'en'){
url = 'http://www.kefairport.is/English/Timetables/Arrivals/';
}else{
res.json(200,{'error':'Wrong parameters given!'});
res.json(431,{'error':'Wrong parameters given!'});
return next();
}

Expand All @@ -29,7 +30,7 @@ slash = function(req, res, next){
url: url
}, function(error, response, body){
if(error){
res.json(500,{error:"Something went wrong"});
res.json(500,{error:"Something went wrong on the server"});
return next();
}
var data = $(body),
Expand Down Expand Up @@ -58,11 +59,8 @@ slash = function(req, res, next){
}
});

if(req.header('Uptime-Test') == 'true'){
h.logVisit('/flight', obj,true);
}else{
h.logVisit('/flight', obj,false);
}
h.logVisit('/flight', obj,false);

res.json(200,obj)
return next();
});
Expand Down
Loading

0 comments on commit f290fdd

Please sign in to comment.