First cut at multiple category/tag support
[catagits/XML-Feed.git] / lib / XML / Feed / Entry.pm
CommitLineData
3353d70c 1# $Id$
0d5e38d1 2
3package XML::Feed::Entry;
4use strict;
973e1f9e 5use base qw( Class::ErrorHandler );
6
566afdf5 7use Scalar::Util qw( blessed );
8
973e1f9e 9use Carp;
0d5e38d1 10
11sub wrap {
12 my $class = shift;
13 my($item) = @_;
14 bless { entry => $item }, $class;
15}
16
973e1f9e 17sub unwrap { $_[0]->{entry} }
18
19sub new {
20 my $class = shift;
21 my($format) = @_;
22 $format ||= 'Atom';
729cd7a8 23 my $format_class = 'XML::Feed::Format::' . $format;
973e1f9e 24 eval "use $format_class";
25 Carp::croak("Unsupported format $format: $@") if $@;
729cd7a8 26 my $entry = bless {}, join('::', __PACKAGE__, "Format", $format);
973e1f9e 27 $entry->init_empty or return $class->error($entry->errstr);
28 $entry;
29}
30
31sub init_empty { 1 }
32
33sub convert {
34 my $entry = shift;
35 my($format) = @_;
36 my $new = __PACKAGE__->new($format);
9a36f82c 37 for my $field (qw( title link content summary category author id issued modified lat long )) {
23103173 38 my $val = $entry->$field();
39 next unless defined $val;
566afdf5 40 next if blessed $val && $val->isa('XML::Feed::Content') && ! defined $val->body;
23103173 41 $new->$field($val);
973e1f9e 42 }
43 $new;
44}
45
0d5e38d1 46sub title;
47sub link;
48sub content;
49sub summary;
50sub category;
51sub author;
52sub id;
53sub issued;
54sub modified;
9a36f82c 55sub lat;
56sub long;
3bdbab6f 57sub format;
a0cca2a4 58sub tags { shift->category(@_) }
0d5e38d1 59
601;
61__END__
62
63=head1 NAME
64
65XML::Feed::Entry - Entry/item in a syndication feed
66
67=head1 SYNOPSIS
68
69 ## $feed is an XML::Feed object.
70 for my $entry ($feed->entries) {
71 print $entry->title, "\n", $entry->summary, "\n\n";
72 }
73
74=head1 DESCRIPTION
75
76I<XML::Feed::Entry> represents an entry/item in an I<XML::Feed> syndication
77feed.
78
79=head1 USAGE
80
973e1f9e 81=head2 XML::Feed::Entry->new($format)
82
83Creates a new I<XML::Feed::Entry> object in the format I<$format>, which
84should be either I<RSS> or I<Atom>.
85
86=head2 $entry->convert($format)
87
88Converts the I<XML::Feed::Entry> object into the I<$format> format, and
89returns the new object.
90
91=head2 $entry->title([ $title ])
0d5e38d1 92
93The title of the entry.
94
5383a560 95=head2 $entry->base([ $base ])
96
97The url base of the entry.
98
973e1f9e 99=head2 $entry->link([ $uri ])
0d5e38d1 100
101The permalink of the entry, in most cases, except in cases where it points
fe71566d 102instead to an offsite URI referenced in the entry.
0d5e38d1 103
973e1f9e 104=head2 $entry->content([ $content ])
0d5e38d1 105
a749d9b9 106Bn I<XML::Feed::Content> object representing the full entry body, or as
107much as is available in the feed.
0d5e38d1 108
109In RSS feeds, this method will look first for
110I<http://purl.org/rss/1.0/modules/content/#encoded> and
111I<http://www.w3.org/1999/xhtml#body> elements, then fall back to a
112I<E<lt>descriptionE<gt>> element.
113
973e1f9e 114=head2 $entry->summary([ $summary ])
0d5e38d1 115
a749d9b9 116An I<XML::Feed::Content> object representing a short summary of the entry.
117Possibly.
0d5e38d1 118
119Since RSS feeds do not have the idea of a summary separate from the entry
a749d9b9 120body, this may not always be what you want. If the entry contains both a
121I<E<lt>descriptionE<gt>> element B<and> another element typically used for
122the full content of the entry--either I<http://www.w3.org/1999/xhtml/body>
123or I<http://purl.org/rss/1.0/modules/content/#encoded>--we treat that as
124the summary. Otherwise, we assume that there isn't a summary, and return
125an I<XML::Feed::Content> object with an empty string in the I<body>.
0d5e38d1 126
973e1f9e 127=head2 $entry->category([ $category ])
0d5e38d1 128
129The category in which the entry was posted.
130
a0cca2a4 131Returns a list of categories if called in array context or the first
132category if called in scalar context.
133
134=head2 $entry->tags([ $tag ])
135
136A synonym for I<category>;
137
973e1f9e 138=head2 $entry->author([ $author ])
0d5e38d1 139
140The name or email address of the person who posted the entry.
141
973e1f9e 142=head2 $entry->id([ $id ])
0d5e38d1 143
144The unique ID of the entry.
145
973e1f9e 146=head2 $entry->issued([ $issued ])
0d5e38d1 147
148A I<DateTime> object representing the date and time at which the entry
149was posted.
150
973e1f9e 151If present, I<$issued> should be a I<DateTime> object.
152
153=head2 $entry->modified([ $modified ])
0d5e38d1 154
155A I<DateTime> object representing the last-modified date of the entry.
156
973e1f9e 157If present, I<$modified> should be a I<DateTime> object.
158
8c30ad3d 159=head2 $entry->wrap
160
161Take an entry in its native format and turn it into an I<XML::Feed::Entry> object.
162
163=head2 $entry->unwrap
164
165Take an I<XML::Feed::Entry> object and turn it into its native format.
166
0d5e38d1 167=head1 AUTHOR & COPYRIGHT
168
169Please see the I<XML::Feed> manpage for author, copyright, and license
170information.
171
172=cut