Mojo/Feed.pm

filter: Statementstmt 98.4% 192 / 195 Branchbran 88.2% 60 / 68 Conditioncond 73.9% 34 / 46 MC/DCmcdc 56.0% 14 / 25 Subroutinesub 97.8% 45 / 46 Podpod 83.3% 5 / 6 totaltotal 90.6% 350 / 386 CC 56 SCAR 16.7
CC
56
Cov
93%
_load
15.0
_get_selector
9.0
_set_selector
7.8
CRAP 5.3
« Mojo/Feed/Reader.pm ↑ Summary Mojo/Feed/Item.pm »
1 package Mojo::Feed;
2 13 ▶use Mojo::Base '-base';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Mojo::Util qw(decode trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use HTTP::Date qw(str2time);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 13 ▶use Mojo::Feed::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 use overload
16 47 ▶ bool => sub { shift->is_valid },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
17 15 ▶ '""' => sub { shift->to_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 13 fallback => 1;
19  
20 13 our $VERSION = "0.22";
21  
22 13 has charset => 'UTF-8';
23  
24 4 ▶has ua => sub { Mojo::UserAgent->new() };
Subroutine __ANON__: called CC=1 SCAR=0.0
25 9 ▶has max_redirects => sub { $ENV{MOJO_MAX_REDIRECTS} || 3 };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"MOJO_MAX_REDIRECTS"} or 3 0 9 9
26 17 ▶has redirects => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
27 2 ▶has related => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29 52 ▶has url => sub { Mojo::URL->new() };
Subroutine __ANON__: called CC=1 SCAR=0.0
30 2 ▶has file => sub { Mojo::File->new() };
Subroutine __ANON__: called CC=1 SCAR=0.0
31 9 ▶has source => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
32 9 my $self = shift;
33 return
34 9 ▶ ($self->url ne '') ? $self->url
Branch: 2 branches
BranchTrueFalseTotal
$self->url ne '' ? : 5 4 9
-f $self->file ? : 2 2 4
35 : (-f $self->file) ? $self->file
36 : undef;
37 13 };
38  
39 100 ▶has body => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
40 100 my $self = shift;
41 100 ▶ if ($self->url ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->url ne '') { } 50 50 100
42 50 return $self->_load();
43 }
44 else { # skip file tests, just slurp (for Mojo::Asset::File)
45 50 return $self->file->slurp();
46 }
47 13 };
48  
49 139 ▶has text => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
50 139 my $self = shift;
51 139 ▶ return decode($self->charset, $self->body) || '';
Condition100%2/2
decode($self->charset, $self->body) || ''
l!l
1191
MC/DC100%1/1
decode($self->charset, $self->body) || ''
decode($self->charset, $self->body)
Truth table100%2/2
A
A= decode($self->charset, $self->body)
Aresult
11
00
52 13 };
53  
54 139 ▶has dom => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 139 my ($self) = @_;
56 139 return Mojo::DOM->new->xml(1)->parse($self->text);
57 13 };
58  
59 # The top node, not the root
60 85 ▶has top => sub { shift->dom->children->first };
Subroutine __ANON__: called CC=1 SCAR=0.0
61  
62 6 ▶has feed_type => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
63 6 my $top = shift->top;
64 6 my $tag = $top->tag;
65 6 my $version = $top->attr('version');
66 6 my $ns = $top->namespace;
67 return
68 6 ▶ ($tag =~ /feed/i)
Branch: 4 branches
BranchTrueFalseTotal
$tag =~ /feed/iu ? : 3 3 6
$version ? : 1 2 3
$tag =~ /rss/iu ? : 2 1 3
$tag =~ /rdf/iu ? : 1 0 1
69 ? ($version)
70 ? 'Atom ' . $version
71 : 'Atom 1.0'
72 : ($tag =~ /rss/i) ? 'RSS ' . $version
73 : ($tag =~ /rdf/i) ? 'RSS 1.0'
74 : 'unknown';
75 13 };
76  
77 81 ▶has namespaces => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
78 81 my $top = shift->top;
79 81 my $namespaces = { atom => $top->namespace }; # only Atom feeds declare a namespace?
80 81 my $attrs = $top->attr;
81 81 for my $at (keys %$attrs) {
82 279 ▶ if ($at =~ /xmlns\:(\w+)/) { # extra namespace declaration
Branch: 1 branch
BranchTrueFalseTotal
if ($at =~ /xmlns\:(\w+)/u) 120 159 279
83 120 $namespaces->{$1} = $attrs->{$at};
84 }
85 }
86 81 return $namespaces;
87 13 };
88  
89 13 my %generic = (
90 description => ['description', 'tagline', 'subtitle'],
91 published => [
92 'published', 'pubDate', 'dc|date', 'created',
93 'issued', 'updated', 'modified'
94 ],
95 author => ['author', 'dc|creator', 'webMaster', 'copyright'],
96 title => ['title'],
97 subtitle => ['subtitle', 'tagline'],
98 link => ['link:not([rel])', 'link[rel=alternate]'],
99 );
100  
101 334 ▶sub _get_selector {
Subroutine _get_selector: called CC=9 SCAR=22.0
102 334 my ($self, $k) = @_;
103 334 for my $generic (@{$generic{$k}}) {
104 707 ▶ if (my $p = $self->dom->at("channel > $generic, feed > $generic", %{$self->namespaces})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $self->dom->at("channel > $generic, feed > $generic", %{$self->namespaces;})) 285 419 704
105 285 ▶ if ($k eq 'author' && $p->at('name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "author" and $p->at("name")) 22 263 285
Condition100%3/3
$k eq "author" and $p->at("name")
!ll&&!rl&&r
2402322
MC/DC100%2/2
$k eq "author" and $p->at("name")
$k eq "author" $p->at("name")
Truth table100%3/3
A and B
A= $k eq "author"B= $p->at("name")
ABresult
0X0
100
111
106 22 return trim $p->at('name')->text;
107 }
108 263 ▶ my $text = trim($p->text || $p->content || $p->attr('href') || '');
Condition87%7/8
$p->text || $p->content || $p->attr('href') || ''
l!l
2585
$p->text or $p->content or $p->attr("href")
l!l&&r!l&&!r
230285
$p->text or $p->content
l!l&&r!l&&!r
230033
MC/DC66%2/3
$p->text || $p->content || $p->attr('href') || ''
$p->text $p->content $p->attr("href")
Truth table75%3/4
A or B or C
A= $p->textB= $p->contentC= $p->attr("href")
ABCresult
1XX1
01X1
0011
0000
109 263 ▶ if ($k eq 'published') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "published") 47 216 263
110 47 return str2time($text);
111 }
112 216 return $text;
113 }
114 }
115 };
116  
117 2 ▶sub _set_selector {
Subroutine _set_selector: called CC=6 SCAR=20.5
118 2 my ($self, $k, $val) = @_;
119 2 for my $generic (@{$generic{$k}}) {
120 2 ▶ if (my $p = $self->dom->at("channel > $generic, feed > $generic", %{$self->namespaces})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $self->dom->at("channel > $generic, feed > $generic", %{$self->namespaces;})) 2 0 2
121 2 ▶ if ($k eq 'author' && $p->at('name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "author" and $p->at("name")) 0 2 2
Condition33%1/3
$k eq "author" and $p->at("name")
!ll&&!rl&&r
200
MC/DC0%0/2
$k eq "author" and $p->at("name")
$k eq "author" $p->at("name")
Truth table33%1/3
A and B
A= $k eq "author"B= $p->at("name")
ABresult
0X0
100
111
122 0 return $p->at('name')->content($val);
123 }
124 2 ▶ if ($k eq 'published') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "published") 0 2 2
125 0 return $p->content(Mojo::Date->new($val)->to_datetime()); # let's pretend we're all OK with Atom dates
126 }
127 2 return $p->content($val);
128 }
129 }
130 };
131 13 foreach my $k (keys %generic) {
132 254 ▶ has $k => sub { return shift->_get_selector($k) || undef; };
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
(shift())->_get_selector($k) || undef
l!l
20051
MC/DC100%1/1
(shift())->_get_selector($k) || undef
(shift())->_get_selector($k)
Truth table100%2/2
A
A= (shift())->_get_selector($k)
Aresult
11
00
133 }
134  
135 69 ▶has items => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
136 69 my $self = shift;
137 $self->dom->find('item, entry')
138 69 ▶ ->map(sub { Mojo::Feed::Item->new(dom => $_, feed => $self) });
Subroutine __ANON__: called CC=1 SCAR=0.0
139 13 };
140  
141 # alias
142 0 ▶sub entries { shift->items() };
Subroutine entries: not called CC=1 SCAR=6.9
Pod documented
143  
144 # change the underlying DOM when we change the items list:
145 1 ▶sub set_items {
Subroutine set_items: called CC=1 SCAR=0.0
Pod undocumented
146 1 my ($self, $new_items) = @_;
147 1 ▶ $self->dom->find('item, entry')->each(sub { $_->remove() });
Subroutine __ANON__: called CC=1 SCAR=0.0
148 1 ▶ $new_items->each(sub { $self->top->append($_->dom) });
Subroutine __ANON__: called CC=1 SCAR=0.0
149 1 return $self->items($new_items);
150 };
151  
152 108 ▶has is_valid => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
153 108 my $top = shift->dom->children->first;
154 92 ▶ return defined $top && $top->tag =~ /^(feed|rss|rdf|rdf:rdf)$/i;
Condition100%3/3
defined $top && $top->tag =~ /^(feed|rss|rdf|rdf:rdf)$/iu
!ll&&!rl&&r
11873
MC/DC100%2/2
defined $top && $top->tag =~ /^(feed|rss|rdf|rdf:rdf)$/iu
defined $top $top->tag =~ /^(feed|rss|rdf|rdf:rdf)$/iu
Truth table100%3/3
A && B
A= defined $topB= $top->tag =~ /^(feed|rss|rdf|rdf:rdf)$/iu
ABresult
0X0
100
111
155 13 };
156  
157 121 ▶sub is_feed_content_type {
Subroutine is_feed_content_type: called CC=3 SCAR=11.0
Pod documented
158 121 my ($self, $content_type) = @_;
159 121 ▶ return undef unless defined $content_type && length $content_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content_type and length $content_type 1 120 121
Condition66%2/3
defined $content_type and length $content_type
!ll&&!rl&&r
10120
MC/DC50%1/2
defined $content_type and length $content_type
defined $content_type length $content_type
Truth table66%2/3
A and B
A= defined $content_typeB= length $content_type
ABresult
0X0
100
111
160 # use split to remove charset attribute from content_type header
161 120 ($content_type) = split(/[; ]+/, $content_type);
162 # feed mime-types:
163 120 my @feed_types = (
164 'application/x.atom+xml', 'application/atom+xml',
165 'application/xml', 'text/xml',
166 'application/rss+xml', 'application/rdf+xml'
167 );
168 120 ▶ return List::Util::first { $_ eq $content_type } @feed_types;
Subroutine __ANON__: called CC=1 SCAR=0.0
169 }
170  
171  
172 72 ▶sub _load {
Subroutine _load: called CC=15 SCAR=27.1
173 72 my ($self) = @_;
174 72 my $tx = $self->ua->get($self->url);
175 72 ▶ if (my $error = $tx->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $tx->error) 5 67 72
176 5 ▶ unless ($tx->res->code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tx->res->code) 5 0 5
177 5 $self->is_valid(undef);
178 croak "Error getting feed from url ", $self->url, ": ",
179 5 $error->{message};
180 }
181 }
182 67 my $result = $tx->result;
183  
184 67 ▶ if ($result->is_error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($result->is_error) { } 8 59 67
elsif ($result->code =~ /^(301|302|303|307|308)$/u) { } 20 39 59
185 8 $self->is_valid(undef);
186 8 ▶ croak "Error getting feed from url ", $self->url, ": ",
Condition50%1/2
$result->message // ""
l!l
80
MC/DC0%0/1
$result->message // ""
$result->message
Truth table50%1/2
A
A= $result->message
Aresult
11
00
187 $result->code, " ", ($result->message // '');
188 }
189  
190 # Redirect:
191 elsif ($result->code =~ /^(301|302|303|307|308)$/) {
192 20 my $new_url = Mojo::URL->new($result->headers->location)
193 ->to_abs($tx->req->url);
194 20 push @{$self->redirects}, $self->url;
195 20 $self->url($new_url);
196 20 ▶ if (@{$self->redirects} > $self->max_redirects) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->redirects;} > $self->max_redirects) 3 17 20
197 3 $self->is_valid(undef);
198 3 croak "Number of redirects exceeded when loading feed"
199 }
200 17 return $self->_load();
201 }
202  
203 # Is this a feed (by content type)?
204 39 ▶ if ($self->is_feed_content_type($result->headers->content_type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_feed_content_type($result->headers->content_type)) { } 25 14 39
205 25 ▶ $self->charset($result->content->charset) if ($result->content->charset);
Branch: 1 branch
BranchTrueFalseTotal
if $result->content->charset 3 22 25
206 25 return $result->body;
207 }
208 else {
209 # we are in a web page. PHEAR.
210  
211 # Set real (absolute) URL (is this only relevant for testing?):
212 14 ▶ if ($self->url ne $tx->req->url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->url ne $tx->req->url) 5 9 14
213 5 push @{$self->redirects}, $self->url; # for logging?
214 5 $self->url($tx->req->url);
215 }
216 14 my @feeds = $self->find_feed_links($result);
217  
218 14 ▶ if (@feeds) {
Branch: 1 branch
BranchTrueFalseTotal
if (@feeds) { } 5 9 14
219 5 push @{$self->redirects}, $self->url; # not really a redirect, but save it
220 5 $self->url(shift @feeds);
221  
222 # save any remaining feed links as related
223 5 ▶ push @{$self->related}, @feeds if (@feeds);
Branch: 1 branch
BranchTrueFalseTotal
if @feeds 2 3 5
224 5 return $self->_load();
225 }
226 else {
227 # call me crazy, but maybe this is just a feed served as HTML?
228 9 my $test = Mojo::Feed->new( url => $self->url, body => $result->body );
229 9 ▶ $test->charset($result->content->charset) if ($result->content->charset);
Branch: 1 branch
BranchTrueFalseTotal
if $result->content->charset 3 6 9
230 9 ▶ if ($test->is_valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->is_valid) { } 6 3 9
231 # can't avoid parsing twice;
232 # body is probably being called in the dom initializer
233 # :(
234 # $self->dom($test->dom);
235 6 ▶ $self->charset($test->charset) if ($test->charset);
Branch: 1 branch
BranchTrueFalseTotal
if $test->charset 6 0 6
236 6 return $test->body;
237 }
238 else {
239 3 $self->is_valid(undef);
240 3 croak "No valid feed found at ", $self->url;
241 }
242 }
243 }
244 }
245  
246 35 ▶sub find_feed_links {
Subroutine find_feed_links: called CC=3 SCAR=11.2
Pod documented
247 35 my ($self, $result) = @_;
248 35 my @feeds;
249  
250 # Find feed link elements in HEAD:
251 35 ▶ my $base
Condition33%1/3
$result->dom->find('head base')->map('attr', 'href')->join('') || $self->url
l!l&&r!l&&!r
3500
MC/DC0%0/2
$result->dom->find('head base')->map('attr', 'href')->join('') || $self->url
$result->dom->find('head base')->map('attr', 'href')->join('') $self->url
Truth table33%1/3
A || B
A= $result->dom->find('head base')->map('attr', 'href')->join('')B= $self->url
ABresult
1X1
011
000
252 = Mojo::URL->new(
253 $result->dom->find('head base')->map('attr', 'href')->join('') || $self->url)
254 ->to_abs($self->url);
255 35 ▶ my $title
Condition33%1/3
$result->dom->find('head > title')->map('text')->join('') || $self->url
l!l&&r!l&&!r
3500
MC/DC0%0/2
$result->dom->find('head > title')->map('text')->join('') || $self->url
$result->dom->find('head > title')->map('text')->join('') $self->url
Truth table33%1/3
A || B
A= $result->dom->find('head > title')->map('text')->join('')B= $self->url
ABresult
1X1
011
000
256 = $result->dom->find('head > title')->map('text')->join('') || $self->url;
257 338 ▶ $result->dom->find('head link')->each(sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
258 338 my $attrs = $_->attr();
259 338 ▶ return unless ($attrs->{'rel'});
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs->{"rel"} 6 332 338
260 332 my %rel = map { $_ => 1 } split /\s+/, lc($attrs->{'rel'});
261 332 ▶ my $type = ($attrs->{'type'}) ? lc trim $attrs->{'type'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$attrs->{'type'} ? : 55 277 332
262 332 ▶ if ($type && $self->is_feed_content_type($type)
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $self->is_feed_content_type($type) and $rel{"alternate"} || $rel{"service.feed"}) 21 311 332
Condition66%6/9
$type and $self->is_feed_content_type($type) and $rel{"alternate"} || $rel{"service.feed"}
!ll&&!rl&&r
311021
$type and $self->is_feed_content_type($type)
!ll&&!rl&&r
2773421
$rel{"alternate"} or $rel{"service.feed"}
l!l&&r!l&&!r
2100
MC/DC50%2/4
$type and $self->is_feed_content_type($type) and $rel{"alternate"} || $rel{"service.feed"}
$type $self->is_feed_content_type($type) $rel{"alternate"} $rel{"service.feed"}
Truth table60%3/5
A and B and C or D
A= $typeB= $self->is_feed_content_type($type)C= $rel{"alternate"}D= $rel{"service.feed"}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
263 && ($rel{'alternate'} || $rel{'service.feed'}))
264 {
265 21 push @feeds, Mojo::URL->new($attrs->{'href'})->to_abs($base);
266 }
267 35 });
268  
269 # Find feed links (<A HREF="...something feed-like">)
270 35 state $feed_exp = qr/((\.(?:rss|xml|rdf)$)|(\/feed\/*$)|(feeds*\.))/;
271 854 ▶ $result->dom->find('a')->grep(sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
272 854 ▶ $_->attr('href') && $_->attr('href') =~ /$feed_exp/io;
Branch: 1 branch
BranchTrueFalseTotal
$_->attr("href") and $_->attr("href") =~ /$feed_exp/iou 854 0 854
273 8 ▶ })->each(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
274 8 push @feeds, Mojo::URL->new($_->attr('href'))->to_abs($base);
275 35 });
276 35 return @feeds;
277 }
278  
279 28 ▶sub to_hash {
Subroutine to_hash: called CC=2 SCAR=6.9
Pod documented
280 28 my $self = shift;
281 28 ▶ my $hash = {map { $_ => '' . ($self->$_ || '') } (keys %generic)};
Condition100%2/2
$self->$_ || ''
l!l
13236
MC/DC100%1/1
$self->$_ || ''
$self->$_
Truth table100%2/2
A
A= $self->$_
Aresult
11
00
282 28 $hash->{items} = $self->items->map('to_hash')->to_array;
283 28 return $hash;
284 }
285  
286 15 ▶sub to_string {
Subroutine to_string: called CC=4 SCAR=13.9
Pod documented
287 15 my $self = shift;
288 15 foreach my $k (keys %generic) {
289 90 ▶ if ($self->$k && $self->$k ne $self->_get_selector($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$k and $self->$k ne $self->_get_selector($k)) 2 88 90
Condition100%3/3
$self->$k and $self->$k ne $self->_get_selector($k)
!ll&&!rl&&r
10782
MC/DC100%2/2
$self->$k and $self->$k ne $self->_get_selector($k)
$self->$k $self->$k ne $self->_get_selector($k)
Truth table100%3/3
A and B
A= $self->$kB= $self->$k ne $self->_get_selector($k)
ABresult
0X0
100
111
290 # write it to the DOM:
291 2 $self->_set_selector($k, $self->$k);
292 }
293 }
294 15 ▶ $self->items->each(sub { $_->to_string }); # maybe break this out to a sync method
Subroutine __ANON__: called CC=1 SCAR=0.0
295 15 $self->dom->to_string;
296 }
297  
298 13 1;
299 __END__
« Mojo/Feed/Reader.pm ↑ Summary Mojo/Feed/Item.pm »