From 6656f65da915ac587761dc28d039438d2428f9a4 Mon Sep 17 00:00:00 2001 From: Wito Chandra Date: Sun, 1 Apr 2018 21:46:53 +0700 Subject: [PATCH] add properties into feed & fix toString method --- lib/domain/rss_category.dart | 8 +++++ lib/domain/rss_cloud.dart | 11 ++++++ lib/domain/rss_feed.dart | 70 ++++++++++++++++++++++++++++++++++-- lib/util/helpers.dart | 19 +++++++--- test/rss_test.dart | 17 +++++++++ test/xml/RSS.xml | 17 +++++++++ 6 files changed, 135 insertions(+), 7 deletions(-) diff --git a/lib/domain/rss_category.dart b/lib/domain/rss_category.dart index f170f87..a5add9b 100644 --- a/lib/domain/rss_category.dart +++ b/lib/domain/rss_category.dart @@ -13,4 +13,12 @@ class RssCategory { return new RssCategory(domain, value); } + + @override + String toString() { + return ''' + domain: $domain + value: $value + '''; + } } \ No newline at end of file diff --git a/lib/domain/rss_cloud.dart b/lib/domain/rss_cloud.dart index 2cd0042..6a2c792 100644 --- a/lib/domain/rss_cloud.dart +++ b/lib/domain/rss_cloud.dart @@ -17,4 +17,15 @@ class RssCloud { var protocol = node.getAttribute("protocol"); return new RssCloud(domain, port, path, registerProcedure, protocol); } + + @override + String toString() { + return ''' + domain: $domain + port: $port + path: $path + registerProcedure: $registerProcedure + protocol: $protocol + '''; + } } \ No newline at end of file diff --git a/lib/domain/rss_feed.dart b/lib/domain/rss_feed.dart index 7509a75..1632472 100644 --- a/lib/domain/rss_feed.dart +++ b/lib/domain/rss_feed.dart @@ -17,13 +17,33 @@ class RssFeed { final RssImage image; final RssCloud cloud; final List categories; + final List skipDays; + final List skipHours; final String lastBuildDate; final String language; final String generator; final String copyright; + final String docs; + final String managingEditor; + final String rating; + final String webMaster; + final int ttl; RssFeed(this.title, this.description, this.link, this.items, - {this.image, this.cloud, this.categories, this.lastBuildDate, this.language, this.generator, this.copyright}); + {this.image, + this.cloud, + this.categories, + this.skipDays, + this.skipHours, + this.lastBuildDate, + this.language, + this.generator, + this.copyright, + this.docs, + this.managingEditor, + this.rating, + this.webMaster, + this.ttl}); factory RssFeed.parse(XmlDocument document) { XmlElement channelElement; @@ -54,19 +74,50 @@ class RssFeed { return new RssCategory.parse(element); }).toList(); + List skipDays = new List(); + var skipDaysNodes = channelElement.findElements("skipDays"); + if (skipDaysNodes.isNotEmpty) { + skipDays = skipDaysNodes.first.findAllElements("day").map((element) { + return element.text; + }).toList(); + } + List skipHours = new List(); + var skipHoursNodes = channelElement.findElements("skipHours"); + if (skipHoursNodes.isNotEmpty) { + skipHours = skipHoursNodes.first.findAllElements("hour").map((element) { + try { + return int.parse(element.text); + } on FormatException { + return null; + } + }).toList(); + } + var lastBuildDate = xmlGetString(channelElement, "lastBuildDate", strict: false); var language = xmlGetString(channelElement, "language", strict: false); var generator = xmlGetString(channelElement, "generator", strict: false); var copyright = xmlGetString(channelElement, "copyright", strict: false); + var docs = xmlGetString(channelElement, "docs", strict: false); + var managingEditor = xmlGetString(channelElement, "managingEditor", strict: false); + var rating = xmlGetString(channelElement, "rating", strict: false); + var webMaster = xmlGetString(channelElement, "webMaster", strict: false); + var ttl = xmlGetInt(channelElement, "ttl", strict: false); return new RssFeed(title, description, link, feeds, image: image, cloud: cloud, categories: categories, + skipDays: skipDays, + skipHours: skipHours, lastBuildDate: lastBuildDate, language: language, generator: generator, - copyright: copyright); + copyright: copyright, + docs: docs, + managingEditor: managingEditor, + rating: rating, + webMaster: webMaster, + ttl: ttl); } @override @@ -75,8 +126,21 @@ class RssFeed { title: $title description: $description link: $link - lastBuildDate: $lastBuildDate items: $items + image: $image + cloud: $cloud + categories: $categories + skipDays: $skipDays + skipHours: $skipHours + lastBuildDate: $lastBuildDate + language: $language + generator: $generator + copyright: $copyright + docs: $docs + managingEditor: $managingEditor + rating: $rating + webMaster: $webMaster + ttl: $ttl '''; } } diff --git a/lib/util/helpers.dart b/lib/util/helpers.dart index bca963c..53620d2 100644 --- a/lib/util/helpers.dart +++ b/lib/util/helpers.dart @@ -4,10 +4,7 @@ import 'package:xml/xml.dart'; String xmlGetString(XmlElement element, String name, {strict: true}) { try { - return element - .findElements(name) - .first - .text; + return element.findElements(name).first.text; } on StateError { if (strict) { throw new ArgumentError("$name not found"); @@ -15,3 +12,17 @@ String xmlGetString(XmlElement element, String name, {strict: true}) { } return null; } + +int xmlGetInt(XmlElement element, String name, {strict: true}) { + var value = xmlGetString(element, name, strict: strict); + if (value != null) { + try { + return int.parse(value); + } on FormatException { + if (strict) { + throw new ArgumentError("$name has invalid format"); + } + } + } + return null; +} diff --git a/test/rss_test.dart b/test/rss_test.dart index 3761e92..c109e32 100644 --- a/test/rss_test.dart +++ b/test/rss_test.dart @@ -27,6 +27,11 @@ void main() { expect(feed.lastBuildDate, "Mon, 26 Mar 2018 14:00:00 PDT"); expect(feed.generator, "Custom"); expect(feed.copyright, "Copyright 2018, Foo bar Inc."); + expect(feed.docs, "https://foo.bar.news/docs"); + expect(feed.managingEditor, "alice@foo.bar.news"); + expect(feed.rating, "The PICS rating of the feed"); + expect(feed.webMaster, "webmaster@foo.bar.news"); + expect(feed.ttl, 60); expect(feed.image.title, "Foo bar News"); expect(feed.image.url, "https://foo.bar.news/logo.gif"); @@ -44,6 +49,18 @@ void main() { expect(feed.categories[1].domain, "news"); expect(feed.categories[1].value, "Lorem Ipsum"); + expect(feed.skipDays.length, 3); + expect(feed.skipDays.contains("Monday"), true); + expect(feed.skipDays.contains("Tuesday"), true); + expect(feed.skipDays.contains("Sunday"), true); + + expect(feed.skipHours.length, 5); + expect(feed.skipHours.contains(0), true); + expect(feed.skipHours.contains(1), true); + expect(feed.skipHours.contains(2), true); + expect(feed.skipHours.contains(3), true); + expect(feed.skipHours.contains(4), true); + expect(feed.items.length, 2); expect(feed.items.first.title, "The standard Lorem Ipsum passage, used since the 1500s"); diff --git a/test/xml/RSS.xml b/test/xml/RSS.xml index f9d54f2..f498bbc 100644 --- a/test/xml/RSS.xml +++ b/test/xml/RSS.xml @@ -17,6 +17,23 @@ Mon, 26 Mar 2018 14:00:00 PDT Custom Copyright 2018, Foo bar Inc. + https://foo.bar.news/docs + alice@foo.bar.news + The PICS rating of the feed + webmaster@foo.bar.news + 60 + + Monday + Tuesday + Sunday + + + 0 + 1 + 2 + 3 + 4 + The standard Lorem Ipsum passage, used since the 1500s https://foo.bar.news/1