Skip to content

Commit 21fe7b9

Browse files
committed
Add Enclosure support from branch
2 parents 75c12aa + 12a4079 commit 21fe7b9

File tree

13 files changed

+219
-1
lines changed

13 files changed

+219
-1
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Revision history for XML::Feed
55
0.42
66
- Fixed a bug where $e->category fails when XML::RSS::LibXML is preferred.
77
(Tatsuhiko Miyagawa)
8+
- Added support for enclosures
89

910
0.41
1011
- Add handling for multiple categories/tags, including

MANIFEST

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Changes
22
lib/XML/Feed.pm
33
lib/XML/Feed/Content.pm
4+
lib/XML/Feed/Enclosure.pm
45
lib/XML/Feed/Entry.pm
56
lib/XML/Feed/Format/Atom.pm
67
lib/XML/Feed/Format/RSS.pm
@@ -25,17 +26,24 @@ t/12-multi-categories-atom.t
2526
t/12-multi-categories-rss.t
2627
t/12-multi-categories.base
2728
t/12-multi-subjects-rss.t
29+
t/13-category-hash-bug.t
30+
t/14-enclosures.t
2831
t/pod-coverage.t
2932
t/pod.t
3033
t/samples/atom-10-example.xml
34+
t/samples/atom-enclosure.xml
3135
t/samples/atom-full.xml
36+
t/samples/atom-multi-enclosure.xml
3237
t/samples/atom-multiple-categories.xml
3338
t/samples/atom.xml
3439
t/samples/base_atom.xml
3540
t/samples/base_rss.xml
41+
t/samples/category-bug.xml
3642
t/samples/rss-multiple-categories.xml
3743
t/samples/rss-multiple-subjects.xml
3844
t/samples/rss10-invalid-date.xml
3945
t/samples/rss10.xml
46+
t/samples/rss20-enclosure.xml
47+
t/samples/rss20-multi-enclosure.xml
4048
t/samples/rss20-no-summary.xml
4149
t/samples/rss20.xml

MANIFEST.SKIP

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
\.tar\.gz$
22
\.svn
3+
\.bak$
34
_build
45
Build
56
blib

lib/XML/Feed.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use Module::Pluggable search_path => "XML::Feed::Format",
1212
require => 1,
1313
sub_name => 'formatters';
1414

15-
our $VERSION = '0.41';
15+
our $VERSION = '0.42';
1616
our @formatters;
1717
BEGIN {
1818
@formatters = __PACKAGE__->formatters;

lib/XML/Feed/Enclosure.pm

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package XML::Feed::Enclosure;
2+
use strict;
3+
4+
use strict;
5+
6+
use base qw( Class::ErrorHandler );
7+
8+
sub wrap {
9+
my $class = shift;
10+
my($c) = @_;
11+
bless { %$c }, $class;
12+
}
13+
*new = \&wrap;
14+
15+
sub _var {
16+
my $enclosure = shift;
17+
my $var = shift;
18+
$enclosure->{$var} = shift if @_;
19+
$enclosure->{$var};
20+
}
21+
22+
sub type { shift->_var('type', @_) }
23+
sub length { shift->_var('length', @_) }
24+
sub url { shift->_var('url', @_) }
25+
26+
1;
27+
__END__
28+
29+
=head1 NAME
30+
31+
XML::Feed::Enclosure - Wrapper for enclosure objects
32+
33+
=head1 SYNOPSIS
34+
35+
my ($enclosure) = $entry->enclosure;
36+
print $enclosure->type;
37+
38+
=head1 DESCRIPTION
39+
40+
I<XML::Feed::Enclosure> represents a content object in an I<XML::Feed::Entry>
41+
entry in a syndication feed.
42+
43+
=head1 USAGE
44+
45+
=head2 wrap
46+
47+
Take params and turn them into a I<XML::Feed::Enclosure> object.
48+
49+
=head2 new
50+
51+
A synonym for I<wrap>.
52+
53+
=head2 $enclosure->url
54+
55+
The url of the object.
56+
57+
=head2 $enclosure->type
58+
59+
The MIME type of the item referred to in I<url>.
60+
61+
=head2 $enclosure->length
62+
63+
The length of object refereed to in I<url>
64+
65+
=head1 AUTHOR & COPYRIGHT
66+
67+
Please see the I<XML::Feed> manpage for author, copyright, and license
68+
information.
69+
70+
=cut
71+

lib/XML/Feed/Entry.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ sub lat;
5656
sub long;
5757
sub format;
5858
sub tags { shift->category(@_) }
59+
sub enclosure;
5960

6061
1;
6162
__END__

lib/XML/Feed/Format/Atom.pm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,23 @@ sub long {
293293
}
294294
}
295295

296+
297+
sub enclosure {
298+
my $entry = shift;
299+
300+
if (@_) {
301+
my $enclosure = shift;
302+
# TODO Atom can have multiple enclosures
303+
$entry->{entry}->link({ rel => 'enclosure', href => $enclosure->{url},
304+
length => $enclosure->{length},
305+
type => $enclosure->{type} });
306+
return 1;
307+
} else {
308+
my $l = first { defined $_->rel && $_->rel eq 'enclosure' } $entry->{entry}->link;
309+
return undef unless $l;
310+
return XML::Feed::Enclosure->new({ url => $l->href, length => $l->length, type => $l->type });
311+
}
312+
}
313+
314+
296315
1;

lib/XML/Feed/Format/RSS.pm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,19 @@ sub long {
329329
}
330330
}
331331

332+
sub enclosure {
333+
my $entry = shift;
334+
335+
if (@_) {
336+
my $enclosure = shift;
337+
$entry->{entry}->{enclosure} = {
338+
url => $enclosure->{url},
339+
type => $enclosure->{type},
340+
length => $enclosure->{length}
341+
};
342+
} else {
343+
return XML::Feed::Enclosure->new($entry->{entry}->{enclosure});
344+
}
345+
}
332346

333347
1;

t/14-enclosures.t

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!perl -w
2+
3+
use strict;
4+
use Test::More;
5+
use XML::Feed;
6+
use XML::Feed::Enclosure;
7+
8+
my @formats = qw(atom rss20);
9+
plan tests => scalar(@formats)*22;
10+
11+
foreach my $format (@formats) {
12+
ok (my $feed = XML::Feed->parse("t/samples/$format-enclosure.xml"), "Parsed $format");
13+
my ($entry) = $feed->entries;
14+
ok (my $enclosure = $entry->enclosure, "Got enclosure");
15+
ok ($enclosure->isa("XML::Feed::Enclosure"), "Object isa XML::Feed::Enclosure");
16+
is ($enclosure->type, "audio/mpeg", "Got the enclosure mime type");
17+
is ($enclosure->length, "2478719", "Got enclosure length");
18+
is ($enclosure->url, "http://example.com/sample_podcast.mp3", "Got enclosure url");
19+
20+
ok (my $tmp = XML::Feed::Enclosure->new({ type => "image/jpeg" }), "Created a new enclosure");
21+
is ($tmp->type, "image/jpeg", "Got type back");
22+
ok ($tmp->url("http://example.com/sample_image.jpg"), "Set url");
23+
ok ($tmp->length("1337"), "Set length");
24+
ok ($entry->enclosure($tmp), "Set the enclosure");
25+
26+
ok ($enclosure = $entry->enclosure, "Got enclosure again");
27+
ok ($enclosure->isa("XML::Feed::Enclosure"), "Object still isa XML::Feed::Enclosure");
28+
is ($enclosure->type, "image/jpeg", "Got the enclosure mime type");
29+
is ($enclosure->length, "1337", "Got enclosure length again");
30+
is ($enclosure->url, "http://example.com/sample_image.jpg", "Got enclosure url again");
31+
32+
my $xml = $feed->as_xml;
33+
ok ($feed = XML::Feed->parse(\$xml), "Parsed xml again");
34+
ok ($enclosure = $entry->enclosure, "Got enclosure again");
35+
ok ($enclosure->isa("XML::Feed::Enclosure"), "Object still isa XML::Feed::Enclosure");
36+
is ($enclosure->type, "image/jpeg", "Got the enclosure mime type");
37+
is ($enclosure->length, "1337", "Got enclosure length again");
38+
is ($enclosure->url, "http://example.com/sample_image.jpg", "Got enclosure url again");
39+
40+
41+
}

t/samples/atom-enclosure.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
3+
<feed xmlns="http://www.w3.org/2005/Atom">
4+
<title>Enclosure Demo</title>
5+
<updated>2008-10-13T07:45:39Z</updated>
6+
<entry>
7+
<title>Attachment/Enclosure Example</title>
8+
<updated>2004-11-02T14:44:33</updated>
9+
<content>description</content>
10+
<link rel="enclosure" href="http://example.com/sample_podcast.mp3" length="2478719" type="audio/mpeg" />
11+
</entry>
12+
</feed>
13+

0 commit comments

Comments
 (0)