Skip to content

Commit

Permalink
Merge 275c27b into 5eee549
Browse files Browse the repository at this point in the history
  • Loading branch information
williambelle committed May 13, 2019
2 parents 5eee549 + 275c27b commit d4e6e29
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
24 changes: 21 additions & 3 deletions src/index.js
Expand Up @@ -73,7 +73,7 @@ let checkTags = (listTags) => {
return isValid;
};

exports.findMenu = (opts = DEFAULT_MENUS_OPTIONS) => {
let findMenu = (opts = DEFAULT_MENUS_OPTIONS) => {
opts.language = opts.language || DEFAULT_MENUS_OPTIONS.language;
opts.partOfDay = opts.partOfDay || DEFAULT_MENUS_OPTIONS.partOfDay;

Expand All @@ -97,7 +97,22 @@ exports.findMenu = (opts = DEFAULT_MENUS_OPTIONS) => {
opts.tags = listTags.join(',');
}

const url = buildMenuUrl(opts);
return getMenu(opts);
};

let getMenu = async function (opts) {
if (opts.restoId && isNaN(opts.restoId)) {
return Promise.reject(new TypeError('Not a valid restoId'));
}
let resto = await findResto(opts.restoId);
if (resto.length > 0) {
const url = buildMenuUrl(opts);
return promGetMenu(url);
}
return Promise.reject(new TypeError('Not a valid restoId'));
};

let promGetMenu = (url) => {
return new Promise((resolve, reject) => {
got(url).then((response) => {
let jsonString = escapeTab(response.body);
Expand All @@ -107,7 +122,7 @@ exports.findMenu = (opts = DEFAULT_MENUS_OPTIONS) => {
});
};

exports.findResto = (id) => {
let findResto = (id) => {
const url = buildRestoUrl(id);
return new Promise((resolve, reject) => {
got(url).then((response) => {
Expand All @@ -133,3 +148,6 @@ exports.translateTags = (strTags) => {
}
return translatedList.join(',');
};

exports.findMenu = findMenu;
exports.findResto = findResto;
28 changes: 23 additions & 5 deletions test/test-findMenu.js
Expand Up @@ -11,21 +11,21 @@ const epflMenuApi = require('../src/index.js');
describe('epfl-menu-api findMenu', function () {
this.timeout(10000);

it('should throw an exception with an invalid date', () => {
it('should throw an exception with the date "30/02/2019"', () => {
return epflMenuApi.findMenu({
date: '30/02/2019'
}).then(() => {
}).catch((err) => err.message.should.equal('Not a valid date'));
});

it('should throw an exception with a language not supported', () => {
it('should throw an exception with the language "de"', () => {
return epflMenuApi.findMenu({
language: 'de'
}).then(() => {
}).catch((err) => err.message.should.equal('Language not supported'));
});

it('should throw an exception with a part of the day not supported', () => {
it('should throw an exception with the part of the day "morning"', () => {
return epflMenuApi.findMenu({
'partOfDay': 'morning'
}).then(() => {
Expand All @@ -34,7 +34,7 @@ describe('epfl-menu-api findMenu', function () {
);
});

it('should throw an exception with a tag not supported', () => {
it('should throw an exception with the tag "Cheese"', () => {
return epflMenuApi.findMenu({
'tags': 'Cheese'
}).then(() => {
Expand All @@ -43,7 +43,25 @@ describe('epfl-menu-api findMenu', function () {
);
});

it('should contains at least 5 menus', function () {
it('should throw an exception with the restoId "Cafétéria BC"', () => {
return epflMenuApi.findMenu({
'restoId': 'Cafétéria BC'
}).then(() => {
}).catch(
(err) => err.message.should.equal('Not a valid restoId')
);
});

it('should throw an exception with the restoId "999999"', () => {
return epflMenuApi.findMenu({
'restoId': 999999
}).then(() => {
}).catch(
(err) => err.message.should.equal('Not a valid restoId')
);
});

it('should contains at least 5 menus on 18/04/2019', function () {
return epflMenuApi.findMenu({
date: '18/04/2019',
restoId: '22',
Expand Down

0 comments on commit d4e6e29

Please sign in to comment.