diff --git a/test/test.ts b/test/test.ts index 9b413ca..fb89fd7 100644 --- a/test/test.ts +++ b/test/test.ts @@ -8,4 +8,25 @@ tap.test('should create a feedVersion', async () => { expect(testSmartFeed).to.be.instanceOf(smartfeed.Smartfeed); }); +tap.test('should create a feed', async () => { + const feed = testSmartFeed.createFeed({ + domain: 'central.eu', + title: 'central.eu - ideas for Europe', + category: 'Politics', + company: 'Lossless GmbH', + companyDomain: 'https://lossless.com', + companyEmail: 'hello@lossless.com', + description: 'ideas for Europe', + }); + feed.addItem({ + title: 'A better European Union', + authorName: 'Phil', + imageUrl: 'https://central.eu/someimage.png', + timestamp: Date.now(), + url: 'https://central.eu/article/somearticle' + }); + const rssFeed = feed.exportRssFeedString(); + console.log(rssFeed); +}); + tap.start(); diff --git a/ts/smartfeed.classes.feed.ts b/ts/smartfeed.classes.feed.ts index 2b4a5d7..b3966d0 100644 --- a/ts/smartfeed.classes.feed.ts +++ b/ts/smartfeed.classes.feed.ts @@ -4,25 +4,32 @@ export interface IFeedOptions { domain: string; title: string; description: string; + category: string; company: string; companyEmail: string; companyDomain: string; } -export interface IFeedItem {} +export interface IFeedItem { + title: string; + timestamp: number; + url: string; + authorName: string; + imageUrl: string; +} export class Feed { options: IFeedOptions; - items: IFeedOptions[]; + items: IFeedItem[] = []; constructor(optionsArg: IFeedOptions) { this.options = optionsArg; } - public addItem() { - + public addItem(itemArg: IFeedItem) { + this.items.push(itemArg); } - public exportRssFeed() { + private getFeedObject() { const feed = new plugins.feed.Feed({ copyright: `All rights reserved, ${this.options.company}`, id: this.options.domain, @@ -36,5 +43,31 @@ export class Feed { generator: '@pushrocks/smartfeed', language: "en" }); + + feed.addCategory(this.options.category); + for (const itemArg of this.items) { + feed.addItem({ + title: itemArg.title, + date: new Date(itemArg.timestamp), + link: itemArg.url, + image: itemArg.imageUrl, + author: [{ + name: itemArg.authorName + }] + }); + } + return feed; + } + + public exportRssFeedString(): string { + return this.getFeedObject().rss2(); + } + + public exportAtomFeed(): string { + return this.getFeedObject().atom1(); + } + + public exportJsonFeed(): string { + return this.getFeedObject().json1(); } } \ No newline at end of file