Mojo/Feed/Reader.pm

filter: Statementstmt 94.6% 71 / 75 Branchbran 81.8% 36 / 44 Conditioncond 64.2% 9 / 14 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 87.5% 14 / 16 Podpod 100% 3 / 3 totaltotal 84.4% 136 / 161 CC 31 SCAR 17.2
CC
31
Cov
87%
parse_opml
9.0
parse
7.3
__ANON__
7.0
CRAP 5.6
« Mojolicious/Plugin/FeedReader.pm ↑ Summary Mojo/Feed.pm »
1 package Mojo::Feed::Reader;
2 9 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 9 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Mojo::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Mojo::Util 'decode', 'trim';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 has charset => 'UTF-8';
12  
13 0 ▶has ua => sub { Mojo::UserAgent->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
14  
15 72 ▶sub parse {
Subroutine parse: called CC=7 SCAR=19.9
Pod documented
16 72 my ( $self, $xml, $charset ) = @_;
17 72 ▶ return undef unless ($xml);
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 72 72
18 72 my ( $body, $source, $url, $file, $feed );
19 72 ▶ if ( $body = $self->_from_string($xml) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($body = $self->_from_string($xml)) { } 7 65 72
elsif ($file = $self->_from_file($xml)) { } 32 33 65
elsif ($url = $self->_from_url($xml)) { } 33 0 33
20 7 $feed = Mojo::Feed->new( body => $body );
21 }
22 elsif ( $file = $self->_from_file($xml) ) {
23 32 $feed = Mojo::Feed->new( file => $file );
24 }
25 elsif ( $url = $self->_from_url($xml) ) {
26 33 $feed = Mojo::Feed->new( url => $url, ua => $self->ua );
27 }
28 else {
29 0 croak "unknown argument $xml";
30 }
31 72 ▶ $feed->charset($charset) if ($charset);
Branch: 1 branch
BranchTrueFalseTotal
if $charset 0 72 72
32 72 ▶ return ( $feed->is_valid ) ? $feed : undef;
Branch: 1 branch
BranchTrueFalseTotal
$feed->is_valid ? : 51 5 56
33 }
34  
35 72 ▶sub _from_string {
Subroutine _from_string: called CC=4 SCAR=13.9
36 72 my ( $self, $xml ) = @_;
37 72 ▶ my $str = ( !ref $xml ) ? $xml : ( ref $xml eq 'SCALAR' ) ? $$xml : '';
Branch: 2 branches
BranchTrueFalseTotal
!ref($xml) ? : 15 57 72
ref $xml eq 'SCALAR' ? : 2 55 57
38 72 ▶ return ( $str =~ /^\s*\</s ) ? $str : undef;
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /^\s*\</su ? : 7 65 72
39 }
40  
41 33 ▶sub _from_url {
Subroutine _from_url: called CC=4 SCAR=17.9
42 33 my ( $self, $xml ) = @_;
43 33 ▶ my $url =
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($xml) && $xml->isa('Mojo::URL') ? : 33 0 33
$xml =~ /^https?\:/u ? : 0 0 0
Condition33%1/3
builtin::blessed($xml) and $xml->isa("Mojo::URL")
!ll&&!rl&&r
0033
MC/DC0%0/2
builtin::blessed($xml) and $xml->isa("Mojo::URL")
builtin::blessed($xml) $xml->isa("Mojo::URL")
Truth table33%1/3
A and B
A= builtin::blessed($xml)B= $xml->isa("Mojo::URL")
ABresult
0X0
100
111
44 ( blessed $xml && $xml->isa('Mojo::URL') ) ? $xml->clone()
45 : ( $xml =~ /^https?\:/ ) ? Mojo::URL->new("$xml")
46 : undef;
47 33 return $url;
48 }
49  
50 65 ▶sub _from_file {
Subroutine _from_file: called CC=5 SCAR=16.3
51 65 my ( $self, $xml ) = @_;
52 65 ▶ my $file =
Branch: 3 branches
BranchTrueFalseTotal
ref $xml ? : 55 10 65
builtin::blessed($xml) && $xml->can('slurp') ? : 22 33 55
-r "$xml" ? : 10 0 10
Condition66%2/3
builtin::blessed($xml) and $xml->can("slurp")
!ll&&!rl&&r
03322
MC/DC50%1/2
builtin::blessed($xml) and $xml->can("slurp")
builtin::blessed($xml) $xml->can("slurp")
Truth table66%2/3
A and B
A= builtin::blessed($xml)B= $xml->can("slurp")
ABresult
0X0
100
111
53 ( ref $xml )
54 ? ( blessed $xml && $xml->can('slurp') )
55 ? $xml
56 : undef
57 : ( -r "$xml" ) ? Mojo::File->new($xml)
58 : undef;
59 65 return $file;
60 }
61  
62 # discover - get RSS/Atom feed URL from argument.
63 # Code adapted to use Mojolicious from Feed::Find by Benjamin Trott
64 # Any stupid mistakes are my own
65 27 ▶sub discover {
Subroutine discover: called CC=1 SCAR=0.0
Pod documented
66 27 my ( $self, $url ) = @_;
67  
68 # $self->ua->max_redirects(5)->connect_timeout(30);
69 return $self->ua->get_p($url)
70 0 ▶ ->catch( sub { my ($err) = shift; croak "Connection Error: $err" } )
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 ->then(
72 27 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
73 27 my ($tx) = @_;
74 27 ▶ if ( $tx->res->is_success && $tx->res->code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->res->is_success and $tx->res->code == 200) 25 2 27
Condition66%2/3
$tx->res->is_success and $tx->res->code == 200
!ll&&!rl&&r
2025
MC/DC50%1/2
$tx->res->is_success and $tx->res->code == 200
$tx->res->is_success $tx->res->code == 200
Truth table66%2/3
A and B
A= $tx->res->is_successB= $tx->res->code == 200
ABresult
0X0
100
111
75 25 my $feed = Mojo::Feed->new(url => $tx->req->url);
76 25 ▶ return $feed->url if ($feed->is_feed_content_type($tx->res->headers->content_type));
Branch: 1 branch
BranchTrueFalseTotal
if $feed->is_feed_content_type($tx->res->headers->content_type) 4 21 25
77 21 my @feeds = $feed->find_feed_links($tx->res);
78 21 ▶ return @feeds if (@feeds);
Branch: 1 branch
BranchTrueFalseTotal
if @feeds 11 10 21
79 10 $feed->body($tx->res->body);
80 10 ▶ $feed->charset($tx->res->content->charset) if ($tx->res->content->charset);
Branch: 1 branch
BranchTrueFalseTotal
if $tx->res->content->charset 6 4 10
81 10 ▶ return $feed->url if ($feed->is_valid);
Branch: 1 branch
BranchTrueFalseTotal
if $feed->is_valid 2 8 10
82 }
83 10 return;
84 }
85 27 );
86 }
87  
88 6 ▶sub parse_opml {
Subroutine parse_opml: called CC=9 SCAR=22.0
Pod documented
89 6 my ( $self, $opml_file ) = @_;
90 6 ▶ my $opml_str = decode $self->charset,
Branch: 1 branch
BranchTrueFalseTotal
ref $opml_file ? : 3 3 6
91 ( ref $opml_file )
92 ? $opml_file->slurp
93 : Mojo::File->new($opml_file)->slurp;
94 6 my $d = Mojo::DOM->new->parse($opml_str);
95 6 my ( %subscriptions, %categories );
96 6 for my $item ( $d->find(q{outline})->each ) {
97 1924 my $node = $item->attr;
98 1924 ▶ if ( !defined $node->{xmlUrl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $node->{'xmlUrl'}) { } 80 1844 1924
99 80 ▶ my $cat = $node->{title} || $node->{text};
Condition66%2/3
$node->{'title'} || $node->{'text'}
l!l&&r!l&&!r
52280
MC/DC0%0/2
$node->{'title'} || $node->{'text'}
$node->{'title'} $node->{'text'}
Truth table66%2/3
A || B
A= $node->{'title'}B= $node->{'text'}
ABresult
1X1
011
000
100 80 $categories{$cat} =
101 $item->children('[xmlUrl]')->map( 'attr', 'xmlUrl' );
102 }
103 else { # file by RSS URL:
104 1844 $subscriptions{ $node->{xmlUrl} } = $node;
105 }
106 }
107  
108 # assign categories
109 6 for my $cat ( keys %categories ) {
110 80 for my $rss ( $categories{$cat}->each ) {
111 next
112 1840 ▶ unless ( $subscriptions{$rss} )
Branch: 1 branch
BranchTrueFalseTotal
unless $subscriptions{$rss} 0 1840 1840
113 ; # don't auto-vivify for empty "categories"
114 1840 ▶ $subscriptions{$rss}{'categories'} ||= [];
Condition100%2/2
$subscriptions{$rss}{"categories"} ||= []
l!l
801760
MC/DC100%1/1
$subscriptions{$rss}{"categories"} ||= []
$subscriptions{$rss}{"categories"}
Truth table100%2/2
A
A= $subscriptions{$rss}{"categories"}
Aresult
11
00
115 1840 push @{ $subscriptions{$rss}{'categories'} }, $cat;
116 }
117 }
118 6 return ( values %subscriptions );
119 }
120  
121 9 1;
122 __END__
« Mojolicious/Plugin/FeedReader.pm ↑ Summary Mojo/Feed.pm »