Debian Codegraph @

lib/XML/Feed/Entry.pm

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