From befd4196e01752c922ed4b4bf2ded7a5a32197d0 Mon Sep 17 00:00:00 2001 From: Mark Stenglein Date: Fri, 30 Dec 2016 23:10:30 -0500 Subject: [PATCH] Should fix the issue...also get a bit carried away - Also added a couple extra things that should be in a different branch. --- app.js | 6 ++--- helpers/index.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ routes/api/v1.js | 24 ++++++++++++----- 3 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 helpers/index.js diff --git a/app.js b/app.js index af78b1d..8814dd0 100644 --- a/app.js +++ b/app.js @@ -16,9 +16,9 @@ var bodyParser = require('body-parser') // Load in Routes // TODO: Make this a dynamic loading system that simply scans the directory -var routes = require('./routes/index') -var docs = require('./routes/docs') -var apiV1 = require('./routes/api/v1') +var routes = require(path.join(__dirname, 'routes')) +var docs = require(path.join(__dirname, 'routes', 'docs')) +var apiV1 = require(path.join(__dirname, 'routes', 'api', 'v1')) // Instantiate the application var app = express() diff --git a/helpers/index.js b/helpers/index.js new file mode 100644 index 0000000..f3523eb --- /dev/null +++ b/helpers/index.js @@ -0,0 +1,70 @@ +/* + * Mason SRCT: Schedules || Helpers index.js + * + * - Main file for the supporting functions used in the schedules application. + * - This file aggregates all of the other functions and exports them to the + * calling file's scope. + * - The main idea here is to make it so that any function that might be used + * in multiple places can be put here and easily unit tested, debugged, and + * changed in all places that use the feature. + * + * tl;dr: Forget staying thirsty, "Stay DRY, my friends." + */ + +var ical = require('ical-generator') // ical-generator library +var config = require('config') // Site wide configs +var db = require('../models') // Database Object + +/* + * A brief note...since this is quite new and I don't know exactly how I want to + * set this up, I'll be making it pretty simple for now. So basically: + * + * TODO: Make this not suck (dynamic loading of other files maybe?) + */ + +/** + * Strip all non alpha numeric characters from input. + * + * @param inputString String to be cleaned + * @since 0.0.0 + * @returns {String} + */ +var strClean = function (inputString) { + return inputString.replace(/[^0-9a-z]/gi, '') +} +module.exports.strClean = strClean + +/** + * Separate and strip all non alpha numeric characters. + * + * @param rawString String to be split and cleaned + * @param separator Character to be used as the separator (defaults to ',') + * @since 0.0.0 + * @returns {Array|String} + */ +var strSplitClean = function (rawString, separator) { + + // Validate input for separator + if (separator && separator.length !== 1) { + throw 'separator must be a single char' + } + if (!separator) { + separator = ',' + } + if (!rawString) { + return null + } + + var output = [] + + rawString.split(separator).forEach(function (rawSection) { + var nextOutput = strClean(rawSection) + + if (nextOutput) { + output.push(nextOutput) + } + }) + + return output +} +module.exports.strSplitClean = strSplitClean diff --git a/routes/api/v1.js b/routes/api/v1.js index 4c36985..1e6461a 100644 --- a/routes/api/v1.js +++ b/routes/api/v1.js @@ -12,11 +12,13 @@ // Load Environment var express = require('express') +var path = require('path') var router = express.Router() var ical = require('ical-generator') // ical-generator library var config = require('config') // Site wide configs var schoolSlugs = config.get('schoolSlugs') // Configured School Slugs var db = require('models') // Database Object +var helpers = require(path.join(__dirname, '..', '..', 'helpers')) //////////////////////////////////////////////////////////////////////////////// // JSON API Definitions @@ -164,12 +166,7 @@ router.get('/ical/:SCHOOL/:SEMSLUG/:SECTIONS', function (req, res, next) { // Get Section Information var sectionArgs = req.params['SECTIONS'] - var semesterSlugs = [] - - // Parse Section Args - sectionArgs.split(',').forEach(function (sectionArg) { - semesterSlugs.push(db.sequelize.escape(sectionArg)) - }) + var semesterSlugs = helpers.strSplitClean(sectionArgs) var sections = db.Section .findAll({ @@ -191,8 +188,21 @@ router.get('/ical/:SCHOOL/:SEMSLUG/:SECTIONS', function (req, res, next) { 'name' : school.get('slug') + ' Class Schedule Fall 2016' } ) - // Build the rest of the calendar + sections.forEach(function (section) { + console.log(section.dataValues.name) + event = cal.createEvent({ + uid: semester.dataValues.slug + '-' + section.dataValues.crn, + start: new Date(new Date().getTime() + 3600000), + end: new Date(new Date().getTime() + 7200000), + summary: 'Example Event', + description: 'It works ;)', + organizer: 'Organizer\'s Name ', + url: 'http://sebbo.net/' + }); + }) + + // TODO: build the calendar events -- GitLab