From f8c79f2ccd8d220dc734922f33aa447b3dccaa2e Mon Sep 17 00:00:00 2001 From: Wito Chandra Date: Sun, 1 Apr 2018 22:07:02 +0700 Subject: [PATCH] add author, source & comments into rss_item --- lib/domain/rss_item.dart | 21 +++++++++++++++++++-- lib/domain/rss_source.dart | 23 +++++++++++++++++++++++ test/rss_test.dart | 4 ++++ test/xml/RSS.xml | 6 ++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 lib/domain/rss_source.dart diff --git a/lib/domain/rss_item.dart b/lib/domain/rss_item.dart index b4a8275..eba3ecc 100644 --- a/lib/domain/rss_item.dart +++ b/lib/domain/rss_item.dart @@ -1,4 +1,5 @@ import 'package:webfeedclient/domain/rss_category.dart'; +import 'package:webfeedclient/domain/rss_source.dart'; import 'package:webfeedclient/util/helpers.dart'; import 'package:xml/xml.dart'; @@ -10,8 +11,12 @@ class RssItem { final List categories; final String guid; final String pubDate; + final String author; + final String comments; + final RssSource source; - RssItem(this.title, this.description, this.link, {this.categories, this.guid, this.pubDate}); + RssItem(this.title, this.description, this.link, + {this.categories, this.guid, this.pubDate, this.author, this.comments, this.source}); factory RssItem.parse(XmlElement element) { var title = xmlGetString(element, "title"); @@ -24,8 +29,16 @@ class RssItem { var guid = xmlGetString(element, "guid", strict: false); var pubDate = xmlGetString(element, "pubDate", strict: false); + var author = xmlGetString(element, "author", strict: false); + var comments = xmlGetString(element, "comments", strict: false); - return new RssItem(title, description, link, categories: categories, guid: guid, pubDate: pubDate); + RssSource source; + try { + source = new RssSource.parse(element.findElements("source").first); + } on StateError {} + + return new RssItem(title, description, link, + categories: categories, guid: guid, pubDate: pubDate, author: author, comments: comments, source: source); } @override @@ -34,7 +47,11 @@ class RssItem { title: $title description: $description link: $link + guid: $guid pubDate: $pubDate + author: $author + comments: $comments + source: $source '''; } } diff --git a/lib/domain/rss_source.dart b/lib/domain/rss_source.dart new file mode 100644 index 0000000..0dbca74 --- /dev/null +++ b/lib/domain/rss_source.dart @@ -0,0 +1,23 @@ +import 'package:xml/xml/nodes/element.dart'; + +class RssSource { + String url; + String value; + + RssSource(this.url, this.value); + + factory RssSource.parse(XmlElement node) { + var url = node.getAttribute("url"); + var value = node.text; + + return new RssSource(url, value); + } + + @override + String toString() { + return ''' + url: $url + value: $value + '''; + } +} diff --git a/test/rss_test.dart b/test/rss_test.dart index 2d60441..e2ef62e 100644 --- a/test/rss_test.dart +++ b/test/rss_test.dart @@ -68,5 +68,9 @@ void main() { expect(feed.items.first.pubDate, "Mon, 26 Mar 2018 14:00:00 PDT"); expect(feed.items.first.categories.first.domain, "news"); expect(feed.items.first.categories.first.value, "Lorem"); + expect(feed.items.first.author, "alice@foo.bar.news"); + expect(feed.items.first.source.url, "https://foo.bar.news/1?source"); + expect(feed.items.first.source.value, "Foo Bar"); + expect(feed.items.first.comments, "https://foo.bar.news/1/comments"); }); } diff --git a/test/xml/RSS.xml b/test/xml/RSS.xml index f498bbc..4a999de 100644 --- a/test/xml/RSS.xml +++ b/test/xml/RSS.xml @@ -41,6 +41,9 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit Lorem Mon, 26 Mar 2018 14:00:00 PDT + alice@foo.bar.news + Foo Bar + https://foo.bar.news/1/comments Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC @@ -51,6 +54,9 @@ Ipsum Tue, 20 Mar 2018 10:00:00 PDT + bob@foo.bar.news + Lorem Ipsum + https://foo.bar.news/2/comments \ No newline at end of file