diff --git a/index.js b/index.js index 20a1ea6..37501b1 100644 --- a/index.js +++ b/index.js @@ -170,3 +170,11 @@ if (process.argv[2] && process.argv[2] === '--service') { }) } + +module.exports = { + AbstractListenerRSSBuilder: require('./src/Models/Builders/AbstractListenerRSSBuilder'), + YoutubeListenerRSSBuilder: require('./src/Models/Builders/YoutubeListenerRSSBuilder'), + ListenerRssInfos: require('./src/Models/ListenerRSSInfos'), + ListenerBuildDirector: require('./src/ListenerDirector'), + ListenerRss: require('./src/ListenerRss') +} diff --git a/src/listener-director.js b/src/ListenerDirector.js similarity index 65% rename from src/listener-director.js rename to src/ListenerDirector.js index 85c526e..6f3ef10 100644 --- a/src/listener-director.js +++ b/src/ListenerDirector.js @@ -1,5 +1,3 @@ -const YoutubeListenerRSSBuilder = require('./Models/YoutubeListenerRSSBuilder'); - class ListenerBuildDirector { _builder = undefined; @@ -9,7 +7,7 @@ class ListenerBuildDirector { } getListener() { - return this._builder.listenerRSS(); + return this._builder.listenerRSS; } build(infos) { diff --git a/src/listener-rss.js b/src/ListenerRss.js similarity index 62% rename from src/listener-rss.js rename to src/ListenerRss.js index 0be964e..9cf5b98 100644 --- a/src/listener-rss.js +++ b/src/ListenerRss.js @@ -1,4 +1,3 @@ -const jsonFile = require('jsonfile'); const Parser = require('rss-parser'); @@ -14,20 +13,29 @@ class ListenerRss { loopRunning = false; constructor(info) { + if(info !== undefined) { + this.setData(info); + this.setParser(); + } + } + + setParser() { // set parser - this.parser = new Parser({ - customFields : { - item: info._customfields.map((elt) => { + this.parser = new Parser(this.customfields !== undefined ? { + customFields: { + item: this.customfields.map((elt) => { return Array.isArray(elt[1]) ? elt[1][0] : elt[1]; }) } - }); + } : undefined); // if customfield is set -> let's set the parser with, else let the option empty + } + setData(infos) { // Set data - this.name = info._name === undefined ? info._address : info._name; // if name is undefined let's take the address - this.address = info._address; - this.timeloop = info._timeloop; - this.customfields = info._customfields; + this.name = infos._name === undefined ? infos._address : infos._name; // if name is undefined let's take the address + this.address = infos._address; + this.timeloop = infos._timeloop; + this.customfields = infos._customfields; } fetchRSS() { diff --git a/src/Models/Builders/AbstractListenerRSSBuilder.js b/src/Models/Builders/AbstractListenerRSSBuilder.js index 8654619..85ebada 100644 --- a/src/Models/Builders/AbstractListenerRSSBuilder.js +++ b/src/Models/Builders/AbstractListenerRSSBuilder.js @@ -1,10 +1,10 @@ -const ListenerRSS = require('../listener-rss'); +const ListenerRSS = require('../../ListenerRss') -class AbstractListnerRSSBuilder { +class AbstractListenerRSSBuilder { _listenerRSS = undefined; constructor() { - if(this.constructor === AbstractListnerRSSBuilder) + if(this.constructor === AbstractListenerRSSBuilder) throw new Error('The Abstract class "AbstractListnerRSSBuilder" cannot be instantiated'); } @@ -17,6 +17,7 @@ class AbstractListnerRSSBuilder { this.setInfos(infos); this.setSpecificInfos(); + this.listenerRSS.setParser() } /** @@ -31,10 +32,7 @@ class AbstractListnerRSSBuilder { } setInfos(infos) { // Nominal Infos (like name, addresse, and other) - this._listenerRSS.name = infos._name; - this._listenerRSS.address = infos._address; - this._listenerRSS.timeloop = infos._timeloop; - this._listenerRSS.name = infos._name; + this._listenerRSS.setData(infos); } setSpecificInfos() { // More generic infos who's depend of platforms @@ -44,4 +42,4 @@ class AbstractListnerRSSBuilder { } -module.exports = AbstractListnerRSSBuilder \ No newline at end of file +module.exports = AbstractListenerRSSBuilder \ No newline at end of file diff --git a/src/Models/Builders/YoutubeListenerRSSBuilder.js b/src/Models/Builders/YoutubeListenerRSSBuilder.js index a3377e9..b3ccab0 100644 --- a/src/Models/Builders/YoutubeListenerRSSBuilder.js +++ b/src/Models/Builders/YoutubeListenerRSSBuilder.js @@ -1,4 +1,4 @@ -const AbstractListenerRSSBuilder = require('AbstractListenerRSSBuilder') +const AbstractListenerRSSBuilder = require('./AbstractListenerRSSBuilder'); class YoutubeListenerRSSBuilder extends AbstractListenerRSSBuilder { constructor() { @@ -6,7 +6,7 @@ class YoutubeListenerRSSBuilder extends AbstractListenerRSSBuilder { } setSpecificInfos() { - this.listenerRSS._customfields = [ + this.listenerRSS.customfields = [ ['description', ['media:group', 'media:description']], ['icon', ['media:group', 'media:thumbnail']] ] diff --git a/src/Models/listenerRSSInfos.js b/src/Models/ListenerRSSInfos.js similarity index 95% rename from src/Models/listenerRSSInfos.js rename to src/Models/ListenerRSSInfos.js index b7ec472..1c5a5be 100644 --- a/src/Models/listenerRSSInfos.js +++ b/src/Models/ListenerRSSInfos.js @@ -39,4 +39,4 @@ class ListenerRSSInfos { } } -module.exports = ListenerRSSInfos \ No newline at end of file +module.exports = ListenerRSSInfos; \ No newline at end of file diff --git a/tests/rss-youtube-spec.js b/tests/rss-youtube-spec.js index bba6032..5922f95 100644 --- a/tests/rss-youtube-spec.js +++ b/tests/rss-youtube-spec.js @@ -1,13 +1,20 @@ + // external lib +const Parser = require("rss-parser"); + + // tested class + +const ListenerRssPackage = require("../index"); + +const Listeners = ListenerRssPackage.ListenerRss +const ListenerRRSInfo = ListenerRssPackage.ListenerRssInfos +const YtbBuilder = ListenerRssPackage.YoutubeListenerRSSBuilder +const Director = ListenerRssPackage.ListenerBuildDirector + + // Unit test const chai = require("chai"); const sinon = require("sinon"); const sinon_chai = require("sinon-chai"); chai.use(sinon_chai); -const Parser = require("rss-parser"); - -const Listeners = require('../src/listener-rss') -const ListenerRRSInfo = require('../src/Models/listenerRSSInfos') -const YtbBuilder = require('../src/Models/YoutubeListenerRSSBuilder') -const YtbDirector = require('../src/listener-director') const expect = chai.expect; @@ -58,11 +65,11 @@ describe("test class RSS: jsonfile", function () { beforeEach(function () { // stubs stubParser = sinon.stub(Parser.prototype, 'parseURL') - .withArgs(listenerInfo.address) + .withArgs(infosListener.address) .resolves(mockedRSSOutput); // constructor - myListener = new Listeners(); + myListener = undefined; }) afterEach(function () { @@ -71,27 +78,44 @@ describe("test class RSS: jsonfile", function () { }); describe("Building Ytb listener", function () { - it("The build without problems", function () { + it("The build without issues", function () { let builder = new YtbBuilder(); - let director = new YtbDirector(builder); + let director = new Director(builder); director.build(infosListener); myListener = director.getListener(); // assertions - expect(stubParser).to.have.been.calledOnce; - expect(stubParser).to.have.been.calledWith(infosListener._address); - expect(myListener.customFields).to.eql([ + // myListener data + expect(myListener.timeloop).to.eql(infosListener._timeloop); + expect(myListener.name).to.eql(infosListener._name) + expect(myListener.address).to.eql(infosListener._address) + expect(myListener.customfields).to.eql([ ['description', ['media:group', 'media:description']], ['icon', ['media:group', 'media:thumbnail']] ]); - //TODO test les infos dans "myListener" }); }); - //Todo + describe("fetch some data", function () { + it("fetch without issues", function () { + // classic build + let builder = new YtbBuilder(); + let director = new Director(builder); + director.build(infosListener); + myListener = director.getListener(); + // fetch + myListener.fetchRSS(); + + //assertion + // calls + expect(stubParser).to.have.been.calledOnce; + expect(stubParser).to.have.been.calledWith(infosListener._address); + }) + }) + describe.skip("start", function () { it("Let's start the timer", function () { - myListener.setDatas(listenerInfo); + myListener.setDatas(infosListener); myListener.start();