First cut at enclosures enclosures origin/enclosures
Simon Wistow [Thu, 11 Dec 2008 01:55:52 +0000 (01:55 +0000)]
lib/XML/Feed/Enclosure.pm
lib/XML/Feed/Entry.pm
lib/XML/Feed/Format/Atom.pm
lib/XML/Feed/Format/RSS.pm
t/13-enclosures.t [new file with mode: 0644]
t/samples/atom-enclosure.xml [new file with mode: 0644]
t/samples/rss20-enclosure.xml [new file with mode: 0644]

index dcbd323..8764562 100644 (file)
@@ -32,7 +32,7 @@ XML::Feed::Enclosure - Wrapper for enclosure objects
 
 =head1 SYNOPSIS
 
-    my ($enclosure) = $entry->enclosures;
+    my ($enclosure) = $entry->enclosure;
     print $enclosure->type;
 
 =head1 DESCRIPTION
index 169fef3..0b66d1a 100644 (file)
@@ -55,6 +55,7 @@ sub modified;
 sub lat;
 sub long;
 sub format;
+sub enclosure;
 
 1;
 __END__
index 071e9f7..4e2d859 100644 (file)
@@ -288,15 +288,22 @@ sub long {
 }
 
 
-sub add_enclosure {
-    my($entry, $enclosure) = @_;
-    my $link = XML::Atom::Link->new;
-    $link->rel('enclosure');
-    $link->type($enclosure->type);
-    $link->href($enclosure->url);
-    $link->length($enclosure->length);
-    $entry->{entry}->add_link($link);
-};
+sub enclosure {
+    my $entry = shift;
+
+    if (@_) {
+        my $enclosure = shift;
+        # TODO Atom can have multiple enclosures
+        $entry->{entry}->link({ rel => 'enclosure', href => $enclosure->{url},
+                                length => $enclosure->{length},
+                                type   => $enclosure->{type} });
+        return 1;
+    } else {
+        my $l = first { defined $_->rel && $_->rel eq 'enclosure' } $entry->{entry}->link;
+        return undef unless $l;
+        return XML::Feed::Enclosure->new({ url => $l->href, length => $l->length, type => $l->type });
+    }
+}
 
 
 1;
index c5c11b7..55a51f7 100644 (file)
@@ -324,15 +324,19 @@ sub long {
     }
 }
 
-sub add_enclosure {
-    my($entry, $enclosure) = @_;
-    $entry->{entry}->{enclosure} = XML::RSS::LibXML::MagicElement->new(
-        attributes => {
+sub enclosure {
+    my $entry  = shift;
+
+    if (@_) {
+        my $enclosure = shift;
+        $entry->{entry}->{enclosure} = {
                  url    => $enclosure->{url},
                  type   => $enclosure->{type},
                  length => $enclosure->{length}
-        },
-    );
+            };
+    } else {
+        return XML::Feed::Enclosure->new($entry->{entry}->{enclosure});
+    }
 }
 
 1;
diff --git a/t/13-enclosures.t b/t/13-enclosures.t
new file mode 100644 (file)
index 0000000..80463dc
--- /dev/null
@@ -0,0 +1,41 @@
+#!perl -w
+
+use strict;
+use Test::More; 
+use XML::Feed;
+use XML::Feed::Enclosure;
+
+my @formats = qw(atom rss20);
+plan tests => scalar(@formats)*22;
+
+foreach my $format (@formats) {
+    ok (my $feed      = XML::Feed->parse("t/samples/$format-enclosure.xml"), "Parsed $format");
+    my ($entry)       = $feed->entries;
+    ok (my $enclosure = $entry->enclosure,                                   "Got enclosure");
+    ok ($enclosure->isa("XML::Feed::Enclosure"),                             "Object isa XML::Feed::Enclosure");
+    is ($enclosure->type,   "audio/mpeg",                                    "Got the enclosure mime type");
+    is ($enclosure->length, "2478719",                                       "Got enclosure length");
+    is ($enclosure->url,    "http://example.com/sample_podcast.mp3",         "Got enclosure url");
+
+    ok (my $tmp       = XML::Feed::Enclosure->new({ type => "image/jpeg" }), "Created a new enclosure");
+    is ($tmp->type,         "image/jpeg",                                    "Got type back");
+    ok ($tmp->url("http://example.com/sample_image.jpg"),                    "Set url");
+    ok ($tmp->length("1337"),                                                "Set length");
+    ok ($entry->enclosure($tmp),                                             "Set the enclosure");
+
+    ok ($enclosure    = $entry->enclosure,                                   "Got enclosure again");
+    ok ($enclosure->isa("XML::Feed::Enclosure"),                             "Object still isa XML::Feed::Enclosure");
+    is ($enclosure->type,   "image/jpeg",                                    "Got the enclosure mime type");
+    is ($enclosure->length, "1337",                                          "Got enclosure length again");
+    is ($enclosure->url,    "http://example.com/sample_image.jpg",           "Got enclosure url again");
+
+    my $xml = $feed->as_xml;
+    ok ($feed         = XML::Feed->parse(\$xml),                             "Parsed xml again");
+    ok ($enclosure    = $entry->enclosure,                                   "Got enclosure again");
+    ok ($enclosure->isa("XML::Feed::Enclosure"),                             "Object still isa XML::Feed::Enclosure");
+    is ($enclosure->type,   "image/jpeg",                                    "Got the enclosure mime type");
+    is ($enclosure->length, "1337",                                          "Got enclosure length again");
+    is ($enclosure->url,    "http://example.com/sample_image.jpg",           "Got enclosure url again");
+
+
+}
diff --git a/t/samples/atom-enclosure.xml b/t/samples/atom-enclosure.xml
new file mode 100644 (file)
index 0000000..6030d51
--- /dev/null
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8" ?>
+
+<feed xmlns="http://www.w3.org/2005/Atom">
+  <title>Enclosure Demo</title>
+  <updated>2008-10-13T07:45:39Z</updated>
+  <entry>
+    <title>Attachment/Enclosure Example</title>
+    <updated>2004-11-02T14:44:33</updated>
+    <content>description</content>
+    <link rel="enclosure" href="http://example.com/sample_podcast.mp3" length="2478719" type="audio/mpeg" />
+  </entry>
+</feed>
+
diff --git a/t/samples/rss20-enclosure.xml b/t/samples/rss20-enclosure.xml
new file mode 100644 (file)
index 0000000..21c0b90
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<rss version="2.0"
+  xmlns:ent="http://www.purl.org/NET/ENT/1.0/"
+  xmlns:dc="http://purl.org/dc/elements/1.1/">
+<channel>
+  <title>Enclosure Demo</title>
+  <lastBuildDate>Mon, 13 Oct 2008 03:45:39 -0400</lastBuildDate>
+  <item>
+    <title>Attachment/Enclosure Example</title>
+    <pubDate>Tue, 02 Nov 2004 09:44:33 -0500</pubDate>
+    <description>description</description>
+    <enclosure url="http://example.com/sample_podcast.mp3" length="2478719" type="audio/mpeg" />
+  </item>
+</channel>
+</rss>
+