From b5fe033203acea2f03e12cf2255ff03ba12f0a78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Stelzm=C3=BCller?= Date: Sun, 12 Feb 2017 17:57:46 +0100 Subject: [PATCH] :sparkles: added wrapper for get(C|K)lasses --- index.js | 8 ++++-- package.json | 3 +- webuntisWrapper.js | 70 ++++++++++++++++++++++++++++------------------ 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/index.js b/index.js index 0309ffd..0718f44 100644 --- a/index.js +++ b/index.js @@ -11,9 +11,11 @@ const w = require("./webuntisWrapper"); const e = require("./entities.js"); w.connectPromise.then(() => { - w.getTimetable(new e.TimeTableEntity(w.info.personId, w.info.personType), undefined, res => { - console.log(res); - }); + w.getTimetable(new e.TimeTableEntity(w.info.personId, w.info.personType)) + .then(data => console.log(data)); + + w.getClasses() + .then(data => console.log(data)); }); diff --git a/package.json b/package.json index a5c9d77..a2c3beb 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": { "enumify": "^1.0.4", "lodash": "^4.17.4", - "moment": "^2.17.1" + "moment": "^2.17.1", + "rxjs": "^5.1.0"° } } diff --git a/webuntisWrapper.js b/webuntisWrapper.js index 6ec07e5..7aad113 100644 --- a/webuntisWrapper.js +++ b/webuntisWrapper.js @@ -13,40 +13,56 @@ const isDate = require("util").isDate, moment = require("moment"), webuntis = require("./webuntisRaw"); -const dateFormat = "YYYYMMDD"; +const DATE_FORMAT = "YYYYMMDD"; +const TIME_FORMAT = "hhmm"; // webuntis.setup("username", "password", "schoolName"); webuntis.setupWithObject(require('./credentials.json')); let connectPromise = webuntis.connect(); -function getTimeTable(timeTableEntity, dateInWeek, cb) { - if(isUndefined(timeTableEntity)) { - throw Error("please pass a timeTableEntity"); - } - - if(isUndefined(dateInWeek) || !isDate(dateInWeek)){ - dateInWeek = moment(); - } - - let startDate = dateInWeek.subtract(dateInWeek.day(), 'days').add(1, 'days'); - - let endDate = startDate.clone().add(5, 'days'); - - return connectPromise.then(webuntis.rpc("getTimetable", { - type: timeTableEntity.type, - id: timeTableEntity.id, - startDate: parseInt(startDate.format(dateFormat)), - endDate: parseInt(endDate.format(dateFormat)), - }, data => { - if(data.error) { - throw Error(data.error); - } else if(data.result) { - cb(data.result); +exports.getTimetable = function(timeTableEntity, dateInWeek) { + return new Promise((resolve, reject) => { + if (isUndefined(timeTableEntity)) { + reject("please pass a timeTableEntity"); } - })) -} -exports.getTimetable = getTimeTable; + if (isUndefined(dateInWeek) || !isDate(dateInWeek)) { + dateInWeek = moment(); + } + + let startDate = dateInWeek.subtract(dateInWeek.day(), 'days').add(1, 'days'); + + let endDate = startDate.clone().add(5, 'days'); + + connectPromise.then(webuntis.rpc("getTimetable", { + type: timeTableEntity.type, + id: timeTableEntity.id, + startDate: parseInt(startDate.format(DATE_FORMAT)), + endDate: parseInt(endDate.format(DATE_FORMAT)), + }, data => { + if (data.error) { + reject(data.error); + } else if (data.result) { + resolve(data.result); + } + })) + }); +}; + +exports.getClasses = function() { + + return new Promise((resolve,reject) => { + connectPromise.then(webuntis.rpc("getKlassen", {}, data => { + if (data.error) { + reject(data.error); + } else if (data.result) { + resolve(data.result); + } + })); + }); +}; + +exports.getKlassen = exports.getClasses; exports.info = webuntis.info; exports.connectPromise = connectPromise;