Skip to content

Commit

Permalink
✨ added wrapper for get(C|K)lasses
Browse files Browse the repository at this point in the history
  • Loading branch information
stoez committed Feb 12, 2017
1 parent 1fa1362 commit b5fe033
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});


Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"°
}
}
70 changes: 43 additions & 27 deletions webuntisWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit b5fe033

Please sign in to comment.