From d7c230187999a9f7a2fc7c95a49c44b35600209e Mon Sep 17 00:00:00 2001 From: Bram van der Veen <96aa48@gmail.com> Date: Sat, 27 Jun 2015 15:03:50 +0200 Subject: [PATCH] Added an API endpoint in the server on /api --- api.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ lookup.js | 19 +++++++++++++++--- plugins/hoewerkt | 2 +- schedule.js | 13 ++++++++----- web.js | 15 +++++++++------ 5 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 api.js diff --git a/api.js b/api.js new file mode 100644 index 0000000..e4a86a8 --- /dev/null +++ b/api.js @@ -0,0 +1,50 @@ +//api.js +var lookup = require('./lookup'); +var schedule = require('./schedule'); + +function parse(req, res, next, api) { + req.api = true; + + if (api == 'search') { + if (!req.query.name) error('You didn\'t send the needed queries: name', res); + else { + lookup.api(req, function (lookup) { + if (lookup.error) error(lookup.error, res); + else response(lookup.data, res); + }); + } + } + else if (api == 'schedule') { + if (!req.query.name) error('You didn\'t send the needed queries : name', res); + else { + lookup.api(req, function (lookup) { + if (lookup.error) error(lookup.error, res); + else { + if (lookup.data.length > 1 || lookup.data.length == 0) error('The request that you did had multiple responses or none, make sure that your query returns one.', res, lookup.data) + else { + console.log(lookup.data); + schedule.api(lookup, function (schedule_data) { + response(schedule_data, res, true); + }); + } + } + }) + } + } +} + +function error(str, res, data) { + res.set('Content-Type', 'application/json'); + var error = JSON.stringify({'error' : str, 'data' : data}, null, 2); + res.status(400).end(error); + return; +} + +function response(data, res, disable_pretty) { + res.set('Content-Type', 'application/json'); + var response = JSON.stringify({'data': data}, null, disable_pretty ? 0 : 2); + res.status(200).end(response); + return; +} + +module.exports = parse; diff --git a/lookup.js b/lookup.js index 3733a55..453cdc3 100644 --- a/lookup.js +++ b/lookup.js @@ -6,7 +6,7 @@ var database = require('mongoskin').db('mongodb://' + config().database); var school_id = config().school_id; //Function for looking through the database and finding entries related to the searchterm. -function lookup(req, res, next, search) { +function get(req, res, next, search) { var index = database.collection('index'); easter(search) ? req.easter = easter(search) : null; easter(search) ? search = easter(search).name : null; //Check if there are any eastereggs matching the search query. @@ -31,6 +31,19 @@ function lookup(req, res, next, search) { }); } +function api(req, callback) { + var index = database.collection('index'); + var query = RegExp(req.query.name, 'i'); + + index.find({$or : [{id : query}, {name : query}, {first_name : query}, {last_name : query}]}).toArray(function (err, database_entry) { + if (err) callback({'error': err}); + else { + for (entry of database_entry) {entry.url = make_url(req, entry)} + callback({'data': database_entry}); + } + }); +} + //Function for making a link out of the given database_entry. function make_url(req, database_entry) { var url = 'http://roosters5.gepro-osi.nl/roosters/rooster.php?school=' + school_id + '&type=' + database_entry.type.charAt(0).toUpperCase() + database_entry.type.slice(1) + 'rooster'; @@ -52,7 +65,7 @@ function make_url(req, database_entry) { url += '&klassen=' + database_entry.name; break; } - + if (req.query.tab) url += '&tabblad=' + req.query.tab return url; @@ -70,7 +83,7 @@ function easter(search) { return null; } -module.exports = lookup; +module.exports = {'get': get, 'api': api}; //Testing function, if test is passed in the command line will execute a test. if (process.argv[2] == "test") { diff --git a/plugins/hoewerkt b/plugins/hoewerkt index 7d2c86d..a3bb72a 160000 --- a/plugins/hoewerkt +++ b/plugins/hoewerkt @@ -1 +1 @@ -Subproject commit 7d2c86d60be054d4db7e1a36dccd3bba83ee4633 +Subproject commit a3bb72a3418a3a857e03e9cd73b9e9533a98a492 diff --git a/schedule.js b/schedule.js index ace041a..25d288c 100644 --- a/schedule.js +++ b/schedule.js @@ -5,16 +5,19 @@ var config = require('./configuration'); var url = require('url'); //Wrapper function that is being called by express. -function schedule(req, res, next) { - get(req.match.url, function (json) { +function get(req, res, next) { + get_schedule(req.match.url, function (json) { req.match.json = json; next(); }); } +function api(lookup, callback) { + get_schedule(lookup.data[0].url, callback) +} + //Function for getting the page via http. -function get(get_url, callback) { - +function get_schedule(get_url, callback) { var options = url.parse(get_url); options.socksPort = config().tor_port; options.socksHost = config().tor_host; @@ -89,4 +92,4 @@ function to_json(page) { } //Exporting the schedule function. -module.exports = schedule; +module.exports = {'get': get, 'api': api}; diff --git a/web.js b/web.js index 1820edf..7e34736 100644 --- a/web.js +++ b/web.js @@ -4,6 +4,7 @@ var less = require('express-less'); var body_parser = require('body-parser'); var fs = require('fs'); +var api = require('./api'); var config = require('./configuration'); var lookup = require('./lookup'); var schedule = require('./schedule'); @@ -25,16 +26,18 @@ app.get('/', function (req, res) { res.render('homepage', req); }); +app.get('/api/:api', function (req, res) { + +}); + +app.param('api', api); + app.get('/rooster/:search', function (req, res) { next(); }); -app.get('/over', function (req, res) { - res.send('Hier kun je lezen over werkmanrooster.'); -}); - -app.param('search', lookup); -app.param('search', schedule); +app.param('search', lookup.get); +app.param('search', schedule.get); app.param('search', function (req, res) { req.links = config().links;