Fix mixing and matching of RSS and Atom
Simon Wistow [Wed, 22 Oct 2008 23:44:24 +0000 (23:44 +0000)]
Changes
lib/XML/Feed.pm
lib/XML/Feed/Atom.pm
lib/XML/Feed/RSS.pm
t/04-spice-atom.t [copied from t/04-splice.t with 100% similarity]
t/04-splice-atom.t [copied from t/04-splice.t with 100% similarity]
t/04-splice-rss.t [moved from t/04-splice.t with 93% similarity]
t/10-mix-and-match.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index 0fcbfe4..3a06fc9 100644 (file)
--- a/Changes
+++ b/Changes
@@ -2,6 +2,14 @@
 
 Revision history for XML::Feed
 
+0.23 
+    - Fix mixing and matching of RSS and Atom
+      http://rt.cpan.org/Ticket/Display.html?id=21335
+      (Shlomi Fish SHLOMIF)
+    - Note that multiple categories was fixed at some point
+      http://rt.cpan.org/Ticket/Display.html?id=30234
+      (mattn)
+
 0.22 2008-10-22
     - Correct namespace for terms in RSS
       http://rt.cpan.org/Ticket/Display.html?id=25393
index 59c25b6..fcafc2c 100644 (file)
@@ -124,6 +124,15 @@ sub splice {
     }
 }
 
+sub _convert_entry {
+    my $feed   = shift;
+    my $entry  = shift;
+    my $feed_format  = ref($feed);   $feed_format  =~ s!^XML::Feed::!!;
+    my $entry_format = ref($entry);  $entry_format =~ s!^XML::Feed::Entry::!!;
+    return $entry if $entry_format eq $feed_format;
+    return $entry->convert($feed_format); 
+}
+
 sub format;
 sub title;
 sub link;
index 36655b8..d6fa522 100644 (file)
@@ -103,8 +103,9 @@ sub entries {
 }
 
 sub add_entry {
-    my $feed = shift;
-    my($entry) = @_;
+    my $feed  = shift;
+    my $entry = shift || return;
+    $entry    = $feed->_convert_entry($entry);
     $feed->{atom}->add_entry($entry->unwrap);
 }
 
index 3e8187d..432b6af 100644 (file)
@@ -130,9 +130,9 @@ sub entries {
 }
 
 sub add_entry {
-    my $feed = shift;
-    my($entry) = @_;
-    use Data::Dumper;
+    my $feed  = shift;
+    my $entry = shift || return;
+    $entry    = $feed->_convert_entry($entry);
     $feed->{rss}->add_item(%{ $entry->unwrap });
 }
 
similarity index 100%
copy from t/04-splice.t
copy to t/04-spice-atom.t
similarity index 100%
copy from t/04-splice.t
copy to t/04-splice-atom.t
similarity index 93%
rename from t/04-splice.t
rename to t/04-splice-rss.t
index 21f7a5e..5245620 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use Test::More tests => 3;
 use XML::Feed;
 
-my $feed = XML::Feed->new('Atom');
+my $feed = XML::Feed->new('RSS');
 
 my $other = XML::Feed->parse('t/samples/atom.xml')->convert('Atom');
 $feed->splice($other);
diff --git a/t/10-mix-and-match.t b/t/10-mix-and-match.t
new file mode 100644 (file)
index 0000000..12854b9
--- /dev/null
@@ -0,0 +1,32 @@
+# $Id: $
+
+use strict;
+use Test::More tests => 6;
+use XML::Feed;
+
+
+my $afeed  = XML::Feed->new('Atom');
+my $rfeed  = XML::Feed->new('RSS');
+my $atom   = make_entry('Atom');
+my $rss    = make_entry('RSS');
+
+
+ok($afeed->add_entry($atom), "Added Atom entry to Atom feed");
+ok($afeed->add_entry($rss),  "Added RSS  entry to Atom feed");
+
+ok($rfeed->add_entry($rss),  "Added RSS  entry to RSS feed");
+ok($rfeed->add_entry($atom), "Added Atom entry to RSS feed");
+
+
+
+is(scalar $afeed->entries, 2, 'Now 2 entries in Atom feed');
+is(scalar $rfeed->entries, 2, 'Now 2 entries in RSS  feed');
+
+sub make_entry {
+    my $format = shift;
+    my $entry  = XML::Feed::Entry->new($format);
+    $entry->title("Test Title ".rand());
+    $entry->content("Foo");
+    $entry->summary("Bar");
+    return $entry;
+}