Aggregator.pm

filter: Statementstmt 57.3% 43 / 75 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 41.6% 10 / 24 Podpod 100% 4 / 4 totaltotal 54.2% 57 / 105 CC 5 SCAR 11.7
CC
5
Cov
56%
aggregate
12.0
fetch
6.0
to_feed
6.0
CRAP 3.2
« Aggregator/Sort.pm ↑ Summary
1 package XML::Feed::Aggregator;
2 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 $XML::Feed::Aggregator::VERSION = '0.0401';
4 }
5 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use MooseX::Types::Moose qw/ArrayRef Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use MooseX::Types -declare => [qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
8 Sources Feed AtomFeed AtomEntry
9 RSSFeed RSSEntry Feeds Entry
10 6 /];
11 6 ▶use MooseX::Types::URI 'Uri';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use XML::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 6 class_type RSSEntry, {class => 'XML::Feed::Entry::Format::RSS'};
19 6 class_type AtomEntry, {class => 'XML::Feed::Entry::Format::Atom'};
20 6 class_type AtomFeed, {class => 'XML::Feed::Format::RSS'};
21 6 class_type RSSFeed, {class => 'XML::Feed::Format::Atom'};
22  
23 6 subtype Sources,
24 as ArrayRef[Uri];
25  
26 coerce Sources,
27 from ArrayRef[Str],
28 0 ▶ via {
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 [ map { Uri->coerce($_) } @{$_} ]
30 6 };
31  
32 subtype Feed,
33 as AtomFeed|RSSFeed,
34 0 ▶ message { "$_ is not a Feed!" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
35  
36 subtype Entry,
37 as AtomEntry|RSSEntry,
38 0 ▶ message { "$_ is not an Entry!" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
39  
40 has sources => (
41 is => 'rw',
42 isa => Sources,
43 traits => [qw/Array/],
44 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 6 coerce => 1,
46 handles => {
47 all_sources => 'elements',
48 add_source => 'push',
49 },
50 );
51  
52 has feeds => (
53 is => 'rw',
54 isa => ArrayRef[Feed],
55 traits => [qw/Array/],
56 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 6 handles => {
58 all_feeds => 'elements',
59 add_feed => 'push',
60 feed_count => 'count',
61 },
62 );
63  
64 has entries => (
65 is => 'rw',
66 isa => ArrayRef[Entry],
67 traits => [qw/Array/],
68 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 6 handles => {
70 all_entries => 'elements',
71 add_entry => 'push',
72 sort_entries => 'sort_in_place',
73 map_entries => 'map',
74 entry_count => 'count',
75 }
76 );
77  
78 has _errors => (
79 is => 'rw',
80 isa => ArrayRef[Str],
81 traits => [qw/Array/],
82 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 6 handles => {
84 errors => 'elements',
85 error_count => 'count',
86 add_error => 'push',
87 }
88 );
89  
90 6 with 'XML::Feed::Aggregator::Sort';
91 6 with 'XML::Feed::Aggregator::Deduper';
92  
93 0 ▶sub fetch {
Subroutine fetch: not called CC=2 SCAR=17.9
Pod documented
94 0 my ($self) = @_;
95  
96 0 for my $uri ($self->all_sources) {
97 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 0 $self->add_feed(XML::Feed->parse($uri));
99 }
100 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 $self->add_error($uri->as_string." - failed: $_");
102 0 };
103 }
104  
105 0 return $self;
106 }
107  
108 0 ▶sub aggregate {
Subroutine aggregate: not called CC=3 SCAR=24.8
Pod documented
109 0 my ($self) = @_;
110  
111 0 ▶ return $self if $self->entry_count > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->entry_count > 0 0 0 0
112  
113 0 for my $feed ($self->all_feeds) {
114 0 $self->add_entry($feed->entries);
115 }
116  
117 0 ▶ $self->grep_entries(sub { defined $_ });
Subroutine __ANON__: not called CC=1 SCAR=6.9
118  
119 0 return $self;
120 }
121  
122 0 ▶sub grep_entries {
Subroutine grep_entries: not called CC=1 SCAR=6.9
Pod documented
123 0 my ($self, $filter) = @_;
124  
125 0 my @entries = grep { $filter->($_) } $self->all_entries;
126 0 $self->entries(\@entries);
127  
128 0 return $self;
129 }
130  
131 0 ▶sub to_feed {
Subroutine to_feed: not called CC=2 SCAR=17.9
Pod documented
132 0 my ($self, @params) = @_;
133  
134 0 my $feed = XML::Feed->new(@params);
135  
136 0 for my $entry ($self->all_entries) {
137 0 $feed->add_entry($entry);
138 }
139  
140 0 return $feed;
141 }
142  
143 6 1;
144  
145  
146 =pod
147  
148 =head1 NAME
149  
150 XML::Feed::Aggregator
151  
152 =head1 VERSION
153  
154 version 0.0401
155  
156 =head1 SYNOPSIS
157  
158 use XML::Feed::Aggregator;
159  
160 my $syndicator = XML::Feed::Aggregator->new(
161 sources => [
162 "http://blogs.perl.org/atom.xml",
163 "http://news.ycombinator.com/"
164 ],
165 feeds => [ XML::Feed->parse('./slashdot.rss') ]
166
167 )->fetch->aggregate->deduplicate->sort_by_date;
168  
169 $syndicator->grep_entries(sub {
170 $_->author ne 'James'
171 })->deduplicate;
172  
173 say $syndicator->map_entries(sub { $_->title } );
174  
175 =head1 DESCRIPTION
176  
177 This module aggregates feeds from different sources for easy filtering and sorting.
178  
179 =head1 NAME
180  
181 XML::Feed::Aggregator - Simple feed aggregator
182  
183 =head1 ATTRIBUTES
184  
185 =head2 sources
186  
187 Sources to be fetched and loaded into the feeds attribute.
188  
189 Coerces to an ArrayRef of URI objects.
190  
191 =head2 feeds
192  
193 An ArrayRef of XML::Feed objects.
194  
195 =head2 entries
196  
197 List of XML::Feed::Entry objects obtained from each feed
198  
199 =head1 METHODS
200  
201 =head2 fetch
202  
203 Convert each source into an XML::Feed object, via XML::Feed->parse()
204  
205 For a remote address this involves a http request.
206  
207 =head2 aggregate
208  
209 Combine all feed entries into a single 'entries' attribute
210  
211 =head2 to_feed
212  
213 Export aggregated feed to a single XML::Feed object.
214  
215 All parameters passed to L<XML::Feed> constructor.
216  
217 =head1 FEED METHODS
218  
219 Methods relating to the 'feeds' attribute
220  
221 =head2 add_feed
222  
223 Add a new feed to the 'feeds' attribute.
224  
225 =head2 all_feeds
226  
227 Return all feeds as an Array.
228  
229 =head2 feed_count
230  
231 Number of feeds.
232  
233 =head1 ENTRY METHODS
234  
235 Methods relating to the 'entries' attribute
236  
237 =head2 sort_entries
238  
239 See L<XML::Feed::Aggregator::Sort>
240  
241 =head2 map_entries
242  
243 Loop over all entries using $_ within a CodeRef.
244  
245 =head2 grep_entries
246  
247 Grep through entries using $_ within a CodeRef.
248  
249 =head2 add_entry
250  
251 Add a new entry to the aggregated feed.
252  
253 =head2 entry_count
254  
255 Number of entries.
256  
257 =head2 all_entries
258  
259 Returns all entries as an array
260  
261 =head1 ROLES
262  
263 This class consumes the following roles for sorting and deduplication.
264  
265 L<XML::Feed::Aggregator::Deduper>
266 L<XML::Feed::Aggregator::Sort>
267  
268 =head1 ERROR HANDLING
269  
270 =head2 error_count
271  
272 Number of errors occured.
273  
274 =head2 errors
275  
276 An ArrayRef of errors whilst fetching / parsing feeds.
277  
278 =head1 SEE ALSO
279  
280 L<XML::Feed::Aggregator::Deduper>
281  
282 L<XML::Feed::Aggregator::Sort>
283  
284 L<App::Syndicator> L<Perlanet> L<XML::Feed> L<Feed::Find>
285  
286 =head1 AUTHOR
287  
288 Robin Edwards <robin.ge@gmail.com>
289  
290 =head1 COPYRIGHT AND LICENSE
291  
292 This software is copyright (c) 2011 by Robin Edwards.
293  
294 This is free software; you can redistribute it and/or modify it under
295 the same terms as the Perl 5 programming language system itself.
296  
297 =cut
298  
299  
300 __END__
« Aggregator/Sort.pm ↑ Summary