Fixing RT #47981
[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(@_) }
12a4079f 59sub enclosure;
0d5e38d1 60
611;
62__END__
63
64=head1 NAME
65
66XML::Feed::Entry - Entry/item in a syndication feed
67
68=head1 SYNOPSIS
69
70 ## $feed is an XML::Feed object.
71 for my $entry ($feed->entries) {
72 print $entry->title, "\n", $entry->summary, "\n\n";
73 }
74
75=head1 DESCRIPTION
76
77I<XML::Feed::Entry> represents an entry/item in an I<XML::Feed> syndication
78feed.
79
80=head1 USAGE
81
973e1f9e 82=head2 XML::Feed::Entry->new($format)
83
84Creates a new I<XML::Feed::Entry> object in the format I<$format>, which
85should be either I<RSS> or I<Atom>.
86
87=head2 $entry->convert($format)
88
89Converts the I<XML::Feed::Entry> object into the I<$format> format, and
90returns the new object.
91
92=head2 $entry->title([ $title ])
0d5e38d1 93
94The title of the entry.
95
5383a560 96=head2 $entry->base([ $base ])
97
98The url base of the entry.
99
973e1f9e 100=head2 $entry->link([ $uri ])
0d5e38d1 101
102The permalink of the entry, in most cases, except in cases where it points
fe71566d 103instead to an offsite URI referenced in the entry.
0d5e38d1 104
973e1f9e 105=head2 $entry->content([ $content ])
0d5e38d1 106
a749d9b9 107Bn I<XML::Feed::Content> object representing the full entry body, or as
108much as is available in the feed.
0d5e38d1 109
110In RSS feeds, this method will look first for
111I<http://purl.org/rss/1.0/modules/content/#encoded> and
112I<http://www.w3.org/1999/xhtml#body> elements, then fall back to a
113I<E<lt>descriptionE<gt>> element.
114
973e1f9e 115=head2 $entry->summary([ $summary ])
0d5e38d1 116
a749d9b9 117An I<XML::Feed::Content> object representing a short summary of the entry.
118Possibly.
0d5e38d1 119
120Since RSS feeds do not have the idea of a summary separate from the entry
a749d9b9 121body, this may not always be what you want. If the entry contains both a
122I<E<lt>descriptionE<gt>> element B<and> another element typically used for
123the full content of the entry--either I<http://www.w3.org/1999/xhtml/body>
124or I<http://purl.org/rss/1.0/modules/content/#encoded>--we treat that as
125the summary. Otherwise, we assume that there isn't a summary, and return
126an I<XML::Feed::Content> object with an empty string in the I<body>.
0d5e38d1 127
973e1f9e 128=head2 $entry->category([ $category ])
0d5e38d1 129
130The category in which the entry was posted.
131
a0cca2a4 132Returns a list of categories if called in array context or the first
133category if called in scalar context.
134
a759e13e 135B<WARNING> It's possible this API might change to have an
136I<add_category> instead.
137
a0cca2a4 138=head2 $entry->tags([ $tag ])
139
140A synonym for I<category>;
141
973e1f9e 142=head2 $entry->author([ $author ])
0d5e38d1 143
144The name or email address of the person who posted the entry.
145
973e1f9e 146=head2 $entry->id([ $id ])
0d5e38d1 147
148The unique ID of the entry.
149
973e1f9e 150=head2 $entry->issued([ $issued ])
0d5e38d1 151
152A I<DateTime> object representing the date and time at which the entry
153was posted.
154
973e1f9e 155If present, I<$issued> should be a I<DateTime> object.
156
157=head2 $entry->modified([ $modified ])
0d5e38d1 158
159A I<DateTime> object representing the last-modified date of the entry.
160
973e1f9e 161If present, I<$modified> should be a I<DateTime> object.
162
8c30ad3d 163=head2 $entry->wrap
164
165Take an entry in its native format and turn it into an I<XML::Feed::Entry> object.
166
167=head2 $entry->unwrap
168
169Take an I<XML::Feed::Entry> object and turn it into its native format.
170
0d5e38d1 171=head1 AUTHOR & COPYRIGHT
172
173Please see the I<XML::Feed> manpage for author, copyright, and license
174information.
175
176=cut