File Coverage

blib/lib/App/Rssfilter/Match/Category.pm
Criterion Covered Total %
statement 23 23 100.0
branch 2 2 100.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 32 32 100.0


line stmt bran cond sub pod time code
1             # ABSTRACT: match an RSS item by category
2              
3 1     1   78736 use strict;
  1         2  
  1         26  
4 1     1   4 use warnings;
  1         1  
  1         44  
5              
6              
7             package App::Rssfilter::Match::Category;
8             {
9             $App::Rssfilter::Match::Category::VERSION = '0.07';
10             }
11 1     1   906 use Method::Signatures;
  1         78260  
  1         8  
12 1     1   1534 use List::MoreUtils qw( any );
  1         1192  
  1         115  
13              
14              
15 1 100   1   153408 func match ( $item, @bad_cats ) {
  14     14   4854  
  13         21  
  13         39  
16 13         47 my @categories = $item->find("category")->pluck( 'text' )->each;
17 13         6695 my @split_categories = map { ( / \A ( [^:]+ ) ( [:] .* ) \z /xms, $_ ) } @categories;
  14         67  
18 13         26 my %cats = map { $_ => 1 } @split_categories;
  26         76  
19 13     13   95 return List::MoreUtils::any { defined $_ } @cats{ @bad_cats };
  13         78  
20             }
21              
22             1;
23              
24             __END__
25              
26             =pod
27              
28             =encoding UTF-8
29              
30             =head1 NAME
31              
32             App::Rssfilter::Match::Category - match an RSS item by category
33              
34             =head1 VERSION
35              
36             version 0.07
37              
38             =head1 SYNOPSIS
39              
40             use App::Rssfilter::Match::Category;
41              
42             use Mojo::DOM;
43             my $rss = Mojo::DOM->new( <<"End_of_RSS" );
44             <?xml version="1.0" encoding="UTF-8"?>
45             <rss>
46             <channel>
47             <item>
48             <title>Jumping jackrabbit smash long jump record</title>
49             <category>Sport:leporine</category>
50             </item>
51             <item>
52             <title>Online poll proves programmers cool, successful</title>
53             <category>Internet:very_real_and_official</category>
54             </item>
55             </channel>
56             </rss>
57             End_of_RSS
58              
59             print $_, "\n" for $rss->find( 'item' )->grep(
60             sub {
61             App::Rssfilter::Match::Category::match( shift, 'Sport' ) ) {
62             }
63             );
64              
65             # or with an App::Rssfilter::Rule
66              
67             use App::Rssfilter::Rule;
68             App::Rssfilter::Rule->new(
69             condition => 'Category[Sport]',
70             action => sub { print shift->to_xml, "\n" },
71             )->constrain( $rss );
72              
73             # either way, prints
74              
75             # <item>
76             # <title>Jumping jackrabbit smash long jump record</title>
77             # <category>Sport:leporine</category>
78             # </item>
79              
80             =head1 DESCRIPTION
81              
82             This module will match an RSS item if it has one or more specific category.
83              
84             =head1 FUNCTIONS
85              
86             =head2 match
87              
88             my $item_has_category = App::Rssfilter::Match::Category::match( $item, @categories );
89              
90             Returns true if C<$item> has a category which matches any of C<@categories>. Since some RSS feeds specify categories & subcategories as 'C<main category:subcategory>', elements of C<@categories> can be:
91              
92             =over 4
93              
94             =item *
95              
96             C<category> - this category, with any subcategory
97              
98             =item *
99              
100             C<category:subcategory> - only this category with this subcategory
101              
102             =item *
103              
104             C<:subcategory> - any category with a matching subcategory
105              
106             =back
107              
108             =head1 SEE ALSO
109              
110             =over 4
111              
112             =item *
113              
114             L<App::Rssfilter>
115              
116             =item *
117              
118             L<App::Rssfilter::Rule>
119              
120             =back
121              
122             =head1 AUTHOR
123              
124             Daniel Holz <dgholz@gmail.com>
125              
126             =head1 COPYRIGHT AND LICENSE
127              
128             This software is copyright (c) 2013 by Daniel Holz.
129              
130             This is free software; you can redistribute it and/or modify it under
131             the same terms as the Perl 5 programming language system itself.
132              
133             =cut