From 6d4f181b5ab7618bc457f7b1a04f2e8dfd730383 Mon Sep 17 00:00:00 2001 From: James Perih Date: Thu, 11 Aug 2022 13:25:02 -0600 Subject: [PATCH 1/2] Load all children into custom namespace. --- lib/domain/custom_namespace/custom.dart | 26 +++++++++++++++++++++++++ lib/domain/rss_item.dart | 4 ++++ 2 files changed, 30 insertions(+) create mode 100644 lib/domain/custom_namespace/custom.dart diff --git a/lib/domain/custom_namespace/custom.dart b/lib/domain/custom_namespace/custom.dart new file mode 100644 index 0000000..e02f756 --- /dev/null +++ b/lib/domain/custom_namespace/custom.dart @@ -0,0 +1,26 @@ +import 'package:xml/xml.dart'; + +class CustomNamespace { + final Map customTags; + + CustomNamespace({ required this.customTags }); + + String? valueForKey(String key) { + return customTags[key]; + } + + factory CustomNamespace.parse(XmlElement element) { + final customTags = {}; + + element.descendantElements.forEach((descendantElement) { + customTags.addEntries([ + MapEntry( + descendantElement.name.toString(), + descendantElement.text, + ), + ]); + }); + + return CustomNamespace(customTags: customTags); + } +} \ No newline at end of file diff --git a/lib/domain/rss_item.dart b/lib/domain/rss_item.dart index ef9ff20..dbc9b3a 100644 --- a/lib/domain/rss_item.dart +++ b/lib/domain/rss_item.dart @@ -1,3 +1,4 @@ +import 'package:webfeed/domain/custom_namespace/custom.dart'; import 'package:webfeed/domain/dublin_core/dublin_core.dart'; import 'package:webfeed/domain/itunes/itunes.dart'; import 'package:webfeed/domain/media/media.dart'; @@ -25,6 +26,7 @@ class RssItem { final RssEnclosure? enclosure; final DublinCore? dc; final Itunes? itunes; + final CustomNamespace? customNamespace; RssItem({ this.title, @@ -41,6 +43,7 @@ class RssItem { this.enclosure, this.dc, this.itunes, + this.customNamespace, }); factory RssItem.parse(XmlElement element) { @@ -71,6 +74,7 @@ class RssItem { .firstOrNull, dc: DublinCore.parse(element), itunes: Itunes.parse(element), + customNamespace: CustomNamespace.parse(element), ); } } From 883eae0564f9b2b286316de4e93fb93237bcdad6 Mon Sep 17 00:00:00 2001 From: James Perih Date: Thu, 11 Aug 2022 13:39:22 -0600 Subject: [PATCH 2/2] Write opportunistic test. --- test/custom_namespace_test.dart | 49 ++++++++++++++++++++++++ test/xml/Custom.xml | 68 +++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 test/custom_namespace_test.dart create mode 100644 test/xml/Custom.xml diff --git a/test/custom_namespace_test.dart b/test/custom_namespace_test.dart new file mode 100644 index 0000000..744bc78 --- /dev/null +++ b/test/custom_namespace_test.dart @@ -0,0 +1,49 @@ +import 'dart:core'; +import 'dart:io'; + +import 'package:test/test.dart'; +// import 'package:webfeed/domain/itunes/itunes_episode_type.dart'; +// import 'package:webfeed/domain/itunes/itunes_type.dart'; +// import 'package:webfeed/domain/syndication/syndication.dart'; +import 'package:webfeed/webfeed.dart'; + +void main() { + test('Loads tags into customNamespace map.xml', () { + final expectValues = >[ + { + 'g-core:price': '26500.0', + 'geo:lat': '50.43701', + 'geo:long': '-104.62499', + }, + { + 'g-core:price': '7495.0', + 'geo:lat': '50.4631', + 'geo:long': '-104.59493', + }, + { + 'g-core:price': '7998.0', + 'geo:lat': '50.48064', + 'geo:long': '-104.61908', + }, + { + 'g-core:price': '22495.0', + 'geo:lat': '50.46893', + 'geo:long': '-104.61199', + } + ]; + + final feed = RssFeed.parse(File('test/xml/Custom.xml').readAsStringSync()); + + expect(feed.title, 'Latest Kijiji ads. Location: Regina. Category: Cars & Trucks'); + expect(feed.items!.length, 4); + + var mapIndex = 0; + for (var item in feed.items!) { + expect(item.customNamespace!.valueForKey('g-core:price'), expectValues[mapIndex]['g-core:price']); + expect(item.customNamespace!.valueForKey('geo:lat'), expectValues[mapIndex]['geo:lat']); + expect(item.customNamespace!.valueForKey('geo:long'), expectValues[mapIndex]['geo:long']); + + mapIndex++; + } + }); +} diff --git a/test/xml/Custom.xml b/test/xml/Custom.xml new file mode 100644 index 0000000..4b864ab --- /dev/null +++ b/test/xml/Custom.xml @@ -0,0 +1,68 @@ + + + + Latest Kijiji ads. Location: Regina. Category: Cars & Trucks + https://www.kijiji.ca/b-cars-trucks/regina/fiat-new__used/c174l1700196a54a49 + Latest Kijiji ads. Location: Regina. Category: Cars & Trucks + en-ca + (c) Kijiji Canada 2022 + Wed, 10 Aug 2022 17:02:30 GMT + 2022-08-10T17:02:30Z + en-ca + (c) Kijiji Canada 2022 + + + Latest Kijiji ads. Location: Regina. Category: Cars & Trucks + https://ca.classistatic.com/static/V/11112/img/icon/logo.gif + https://www.kijiji.ca/b-cars-trucks/regina/fiat-new__used/c174l1700196a54a49 + + + Fiat 500 Abarth + https://www.kijiji.ca/v-cars-trucks/regina/fiat-500-abarth/1626712297 + Fiat 500 Abarth turbocharged four-cylinder -This coupe Abarth is a special car with a ton of charisma. One owner - very little mileage and kept in a garage - comes with a set of winter tires. The ... + + Wed, 10 Aug 2022 03:07:53 GMT + https://www.kijiji.ca/v-cars-trucks/regina/fiat-500-abarth/1626712297 + 2022-08-10T03:07:53Z + 50.43701 + -104.62499 + 26500.0 + + + 2012 FIAT 500 SPORT HB / 5 SPD MANUAL TRANS / VERY CHEAP ON GAS + https://www.kijiji.ca/v-cars-trucks/regina/2012-fiat-500-sport-hb-5-spd-manual-trans-very-cheap-on-gas/m4014879 + 2012 FIAT 500 SPORT HB / 5 SPEED MANUAL TRANSMISSION / VERY CHEAP ON GAS NEW TYRES / CUTE LITTLE CAR / LEATHER SEATS / FRESH OIL CHANGE / FRESH DETAIL MUST SEE We make every effort to present ... + + Tue, 09 Aug 2022 00:25:25 GMT + https://www.kijiji.ca/v-cars-trucks/regina/2012-fiat-500-sport-hb-5-spd-manual-trans-very-cheap-on-gas/m4014879 + 2022-08-09T00:25:25Z + 50.4631 + -104.59493 + 7495.0 + + + 2012 Fiat 500 Pop + https://www.kijiji.ca/v-cars-trucks/regina/2012-fiat-500-pop/m4324207 + Engine: 4 Cylinder Engine 1.4L L/100Km City: 7.4 L/100Km Hwy: 5.7 Safety Equipment Electronic stability control Driver & front passenger advanced multi-stage frontal airbags Supplemental front seat ... + + Mon, 08 Aug 2022 15:24:04 GMT + https://www.kijiji.ca/v-cars-trucks/regina/2012-fiat-500-pop/m4324207 + 2022-08-08T15:24:04Z + 50.48064 + -104.61908 + 7998.0 + + + 2017 Fiat 500X LOW KM | LOCAL TRADE | AWD! + https://www.kijiji.ca/v-cars-trucks/regina/2017-fiat-500x-low-km-local-trade-awd/m4412340 + See Dealer Website for Details. Safety Equipment Electronic Stability Control (ESC) And Roll Stability Control (RSC) ABS And Driveline Traction Control Side Impact Beams Dual Stage Driver And ... + + Fri, 29 Jul 2022 10:27:16 GMT + https://www.kijiji.ca/v-cars-trucks/regina/2017-fiat-500x-low-km-local-trade-awd/m4412340 + 2022-07-29T10:27:16Z + 50.46893 + -104.61199 + 22495.0 + + +