Skip to content

Commit

Permalink
Rewrite done
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanmik committed Sep 5, 2013
1 parent deb5950 commit 1d122db
Show file tree
Hide file tree
Showing 25 changed files with 226 additions and 465 deletions.
12 changes: 1 addition & 11 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
.DS_Store
.gitignore
node_modules
endpoints/sms.js
endpoints/phone.js
endpoints/phone2.js
endpoints/phone
endpoints/sms
endpoints/closed_endpoints
index.php
docs
config/*.config.json
repos
config/*.config.json
4 changes: 4 additions & 0 deletions config/dev.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"host": "localhost",
"port": 3100
}
109 changes: 6 additions & 103 deletions endpoints/bus/index.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,7 @@
var request = require('request');
var h = require('../../lib/helpers.js');
var request = require('request'),
h = require('../../lib/helpers.js'),
app = require('../../server');

exports.setup = function(server){
server.post({path: '/bus/search', version: '1.0.0'}, search);
}

var search = function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.json(404,{error:"This api endpoint has been closed temporarily, because Bus.is changed it's markup."});
return next();
}

realtime = function(req, res, next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");

var data = req.params;

request('http://straeto.is/bitar/bus/livemap/json.jsp', function (error, response, body) {
if(error) {
res.json(500,{error:'Something went wrong',code:3});
return next();
}
if(response.statusCode !== 200){
res.json(500,{error:"Something went wrong code 1",code:4});
return next();
}

try{
obj = JSON.parse(body);
}catch(error){
res.json(500,{error:"Something is wrong with the data provided from the data source"});
return next();
}

var activeBusses = [],
requestedBusses = [];

obj.routes.forEach(function(object, key){
activeBusses.push(object.id);
});

if(data.busses){ //Post busses = 1,2,3,4,5
requestedBusses = data.busses.split(",");

for(var i in requestedBusses){ //Prevent requested to busses that are not available
if(activeBusses.indexOf(requestedBusses[i]) == -1){
requestedBusses.splice(requestedBusses.indexOf(requestedBusses[i]),1);
}
}
}else{
//No bus was posted, use all active busses
requestedBusses = activeBusses;
}

objString = requestedBusses.join('%2C');

request('http://straeto.is/bitar/bus/livemap/json.jsp?routes='+objString, function (error, response, body) {

if(error) {
res.json(500,{error:'Something went wrong',code:3});
return next();
}
if(response.statusCode !== 200){
res.json(500,{error:"Something went wrong code 1",code:4});
return next();
}

var data = JSON.parse(body),
routes = data.routes;

var objRoutes = {
results: []
};
routes.forEach(function(route, key){

var objRoute = {
busNr: route.id,
busses: []
};
route.busses.forEach(function(bus, key){

var location = h.ISN93_To_WGS84(bus.X,bus.Y),
oneRoute = {
'unixTime': Date.parse(bus.TIMESTAMPREAL)/1000,
'x': location.lat,
'y': location.lng,
'from': bus.FROMSTOP,
'to': bus.TOSTOP
};
objRoute.busses.push(oneRoute);

});

objRoutes.results.push(objRoute);
});

res.json(200,objRoutes)
return next();
});
});
}
app.post('/bus/search', function(req, res) {
return res.json(404,{error:"This api endpoint has been closed, because Bus.is changed it's markup."});
});
33 changes: 13 additions & 20 deletions endpoints/bus/realtime.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
var h = require('../../lib/helpers.js'),
request = require('request');
request = require('request'),
app = require('../../server');

exports.setup = function(server){
server.post({path: '/bus/realtime', version: '1.0.0'}, realtime); //Old
server.get({path: '/bus/realtime', version: '1.0.0'}, realtime);
}

var realtime = function(req, res, next){
var data = req.params;
app.get('/bus/realtime', function(req, res){
var data = req.query;

request('http://straeto.is/bitar/bus/livemap/json.jsp', function (error, response, body) {
if(error || response.statusCode !== 200) {
throw new Error("The bus api is down or refuses to respond");
}
if(error || response.statusCode !== 200)
return res.json(500,{error:'The bus api is down or refuses to respond'});

var obj;
try{
obj = JSON.parse(body);
}catch(error){
throw new Error("Something is wrong with the data provided from the data source");
return res.json(500,{error:'Something is wrong with the data provided from the data source'});
}

var activeBusses = [],
Expand All @@ -44,14 +40,13 @@ var realtime = function(req, res, next){

request('http://straeto.is/bitar/bus/livemap/json.jsp?routes='+objString, function (error, response, body) {

if(error || response.statusCode !== 200) {
throw new Error("The bus api is down or refuses to respond");
}
if(error || response.statusCode !== 200)
return res.json(500,{error:'The bus api is down or refuses to respond'});

try{
var data = JSON.parse(body);
}catch(e){
throw new Error("Something is wrong with the data provided from the data source");
return res.json(500,{error:'Something is wrong with the data provided from the data source'});
}

var routes = data.routes;
Expand Down Expand Up @@ -84,9 +79,7 @@ var realtime = function(req, res, next){
});

});

res.json(200,objRoutes);
return next();
return res.json(objRoutes);
});
});
}
});
17 changes: 7 additions & 10 deletions endpoints/bus/search.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
app.post({path: '/bus/search', version: '1.0.0'}, function(req,res){
res.inactive(error: 'This api endpoint has been closed temporarily, because Bus.is changed it's markup.');
}); //Old
app.get({path: '/bus/search', version: '1.0.0'}, function(req,res){
res.inactive(error: 'This api endpoint has been closed temporarily, because Bus.is changed it's markup.');
});
var app = require('../../server');

var 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();
}
app.post('/bus/search', function(req,res){
res.json(404,{error: 'This api endpoint has been closed temporarily, because Bus.is changed it\'s markup.'});
}); //Old
app.get('/bus/search', function(req,res){
res.json(404,{error: 'This api endpoint has been closed temporarily, because Bus.is changed it\'s markup.'});
});
6 changes: 3 additions & 3 deletions endpoints/bus/tests/integration_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var request = require('request');
var assert = require('assert');
var helpers = require('../../../lib/test_helpers.js');
var request = require('request'),
assert = require('assert'),
helpers = require('../../../lib/test_helpers.js');

describe('bus', function() {
this.timeout(0); // this endpoint is slow
Expand Down
34 changes: 12 additions & 22 deletions endpoints/car/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
var request = require('request');
var $ = require('jquery');
var h = require('../../lib/helpers.js');
var request = require('request'),
$ = require('jquery'),
h = require('../../lib/helpers.js'),
app = require('../../server');

exports.setup = function(server){
server.get({path: '/car', version: '1.0.0'}, lookup);
server.post({path: '/car', version: '1.0.0'}, lookup);
}

var lookup = function(req, res, next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");

var data = req.params;
app.get('/car', function(req, res){
var carPlate = req.query.number || req.query.carPlate || '';

if(!data.number){
res.json(431,{error:'Please provide a valid number to lookup'});
return next();
}
if(!carPlate)
return res.json(431,{error:'Please provide a valid carPlate to lookup'});

request.get({
headers: {'User-Agent': h.browser()},
url: 'http://ww2.us.is/upplysingar_um_bil?vehinumber='+data.number
url: 'http://ww2.us.is/upplysingar_um_bil?vehinumber='+carPlate
}, function(error, response, body){
if(error || response.statusCode !== 200) {
throw new Error("www.us.is refuses to respond or give back data");
return res.json(500,{error:'www.us.is refuses to respond or give back data'});
}
var data = $(body),
obj = { results: []},
Expand Down Expand Up @@ -53,7 +44,6 @@ var lookup = function(req, res, next){
});
obj.results.push(car);
}
res.json(200,obj)
return next();
return res.json(obj);
});
}
});
6 changes: 3 additions & 3 deletions endpoints/car/tests/integration_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ var request = require('request');
var assert = require('assert');
var helpers = require('../../../lib/test_helpers.js');

describe('cars', function() {
describe('flight', function() {
it("should return an array of objects containing correct fields", function(done) {
var fieldsToCheckFor = ["registryNumber","number","factoryNumber","type","subType","color","registeredAt","status","nextCheck","pollution","weight"];
var params = helpers.testRequestParams("/car", { number: "AA031" });
var params = helpers.testRequestParams("/car", { carPlate: "AA031" });
var resultHandler = helpers.testRequestHandlerForFields(done, fieldsToCheckFor);
request.post(params, resultHandler);
request.get(params, resultHandler);
});
});
37 changes: 13 additions & 24 deletions endpoints/cinema/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
var request = require('request');
var cheerio = require('cheerio');
var h = require('../../lib/helpers.js');

exports.setup = function ( server ) {
server.get({path: '/cinema', version: '1.0.0'}, getMovies);
server.get({path: '/cinema/theaters', version: '1.0.0'}, getTheaters);
};
var request = require('request'),
cheerio = require('cheerio'),
app = require('../../server');

/**
* Fetches movies for show today in Icelandic cinemas.
* response - JSON: Movie data within an 'results' array.
*/
var getMovies = function (req, res, next) {
res.charSet = 'utf8';

app.get('/cinema', function (req, res, next) {
var url = 'http://kvikmyndir.is/bio/syningatimar/';

request(url, function (error, response, body) {
if (error) throw new Error( url + ' not responding correctly...' );
if (error) return res.json(500,{error: url + ' not responding correctly...' });

// Cheerio declared and then attemted to load.
var $;

try {
$ = cheerio.load( body );
} catch (e) {
throw new Error( 'Could not load the body with cherrio.' );
return res.json(500,{error:'Could not load the body with cherrio.'});
}

// Base object to be added to
Expand Down Expand Up @@ -79,29 +71,27 @@ var getMovies = function (req, res, next) {
});
});

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

/**
* Fetches theaters that are showing movies today.
* response - JSON: Theater data within an 'results' array.
*/
var getTheaters = function (req, res, next) {
res.charSet = 'utf8';
app.get('/cinema/theaters', function (req, res, next) {
var url = 'http://kvikmyndir.is/bio/syningatimar_bio/';

request(url, function (error, response, body) {
if (error) throw new Error( url + ' not responding correctly...' );
if (error) return res.json(500,{error: url + ' not responding correctly...' });

// Cheerio declared and then attemted to load.
var $;

try {
$ = cheerio.load( body );
} catch (e) {
throw new Error( 'Could not load the body with cherrio.' );
return res.json(500,{error:'Could not load the body with cherrio.'});
}

// Base object to be added to
Expand Down Expand Up @@ -152,7 +142,6 @@ var getTheaters = function (req, res, next) {
});
});

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

0 comments on commit 1d122db

Please sign in to comment.