Mojo/Feed/Item.pm

filter: Statementstmt 93.1% 82 / 88 Branchbran 89.2% 25 / 28 Conditioncond 92.5% 25 / 27 MC/DCmcdc 86.6% 13 / 15 Subroutinesub 90.0% 18 / 20 Podpod 100% 2 / 2 totaltotal 91.6% 165 / 180 CC 28 SCAR 15.0
CC
28
Cov
92%
_set_selector
9.6
_get_selector
7.0
__ANON__
5.0
CRAP 4.5
« Mojo/Feed.pm ↑ Summary Mojo/Feed/Item/Enclosure.pm »
1 package Mojo::Feed::Item;
2 13 ▶use Mojo::Base '-base';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use Mojo::Util qw( trim );
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 13 ▶use HTTP::Date 'str2time';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Mojo::Feed::Item::Enclosure;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 use overload
10 0 ▶ bool => sub {1},
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=1 SCAR=6.9
11 0 ▶ '""' => sub { shift->to_string },
Subroutine __ANON__: not called CC=1 SCAR=6.9
12 13 fallback => 1;
13  
14  
15 13 has [qw(title link content id description published author)];
16  
17 156 ▶has tags => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
18 shift->dom->find('category, dc\:subject')
19 156 ▶ ->map(sub { $_[0]->text || $_[0]->attr('term') });
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->text or $_[0]->attr("term") 59 60 119
20 13 };
21  
22 13 has 'dom';
23 13 has feed => undef, weak => 1;
24  
25 6 ▶has summary => sub { shift->description };
Subroutine __ANON__: called CC=1 SCAR=0.0
26  
27 13 my %selector = (
28 content => ['content', 'content|encoded', 'xhtml|body', 'description'],
29 description => ['description', 'summary'],
30 published => [
31 'published', 'pubDate', 'dc|date', 'created',
32 'issued', 'updated', 'modified'
33 ],
34 author => ['|author', 'atom|author', 'dc|creator'],
35 id => ['id', 'guid', 'link'],
36 title => ['title'],
37 link => ['link']
38 );
39  
40 1242 ▶sub _get_selector {
Subroutine _get_selector: called CC=7 SCAR=19.5
41 1242 my ($self, $k) = @_;
42 1242 for my $selector (@{$selector{$k}}) {
43 2069 ▶ if (my $p = $self->dom->at($selector, %{$self->feed->namespaces})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $self->dom->at($selector, %{$self->feed->namespaces;})) 1081 988 2069
44 1081 ▶ if ($k eq 'author' && $p->at('name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "author" and $p->at("name")) 36 1045 1081
Condition100%3/3
$k eq "author" and $p->at("name")
!ll&&!rl&&r
10044136
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
45 36 return trim $p->at('name')->text;
46 }
47 1045 my ($text) = grep $_, map trim($_), grep $_, $p->text, $p->content;
48 1045 ▶ $text ||= '';
Condition100%2/2
$text ||= ""
l!l
103510
MC/DC100%1/1
$text ||= ""
$text
Truth table100%2/2
A
A= $text
Aresult
11
00
49 1045 ▶ if ($k eq 'published') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "published") 195 850 1045
50 195 return str2time($text);
51 }
52 850 return $text;
53 }
54 }
55 };
56  
57 10 ▶sub _set_selector {
Subroutine _set_selector: called CC=7 SCAR=22.6
58 10 my ($self, $k, $val) = @_;
59 10 for my $selector (@{$selector{$k}}) {
60 11 ▶ if (my $p = $self->dom->at($selector, %{$self->feed->namespaces})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $self->dom->at($selector, %{$self->feed->namespaces;})) 10 1 11
61 10 ▶ if ($k eq 'author' && $p->at('name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "author" and $p->at("name")) 0 10 10
Condition33%1/3
$k eq "author" and $p->at("name")
!ll&&!rl&&r
1000
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
62 0 return $p->at('name')->content($val);
63 }
64 10 ▶ if ($k eq 'published') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "published") 2 8 10
65 2 return $p->content(Mojo::Date->new($val)->to_datetime()); # let's pretend we're all OK with Atom dates
66 }
67 8 return $p->content($val);
68 }
69 }
70 # still here? I guess the element is missing, so add it?
71 # THIS is another reason to move being feed-type specific:
72 0 ▶ if ($k eq 'author') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "author") 0 0 0
73 0 return $self->dom->append_content($self->dom->new_tag('author', $val));
74 }
75 0 return $self->dom->append_content($self->dom->new_tag($selector{$k}[0], $val));
76 };
77  
78  
79  
80 13 foreach my $k (keys %selector) {
81 1031 ▶ 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
867164
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
82 91 }
83  
84 150 ▶has enclosures => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 150 my $self = shift;
86 150 my @enclosures;
87 8 ▶ $self->dom->find('enclosure')->each(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 8 push @enclosures, $_;
89 150 });
90 155 ▶ $self->dom->find('link')->each(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
91 155 my $l = shift;
92 155 ▶ if ($l->attr('href') && $l->attr('rel') && $l->attr('rel') eq 'enclosure') {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->attr("href") and $l->attr("rel") and $l->attr("rel") eq "enclosure") 6 149 155
Condition100%6/6
$l->attr("href") and $l->attr("rel") and $l->attr("rel") eq "enclosure"
!ll&&!rl&&r
121286
$l->attr("href") and $l->attr("rel")
!ll&&!rl&&r
1310834
MC/DC100%3/3
$l->attr("href") and $l->attr("rel") and $l->attr("rel") eq "enclosure"
$l->attr("href") $l->attr("rel") $l->attr("rel") eq "enclosure"
Truth table100%4/4
A and B and C
A= $l->attr("href")B= $l->attr("rel")C= $l->attr("rel") eq "enclosure"
ABCresult
0XX0
10X0
1100
1111
93 6 push @enclosures, $l;
94 }
95 150 });
96 return Mojo::Collection->new(
97 150 map { Mojo::Feed::Item::Enclosure->new(dom => $_) } @enclosures);
98 13 };
99  
100 166 ▶has link => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
101  
102 # let's handle links seperately, because ATOM loves these buggers:
103 166 my $link;
104 172 ▶ shift->dom->find('link')->each(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
105 172 my $l = shift;
106 172 ▶ if ($l->attr('href')
Branch: 1 branch
BranchTrueFalseTotal
if ($l->attr('href') and !$l->attr('rel') || $l->attr('rel') eq 'alternate') { } 137 35 172
Condition100%6/6
$l->attr("href") and !$l->attr("rel") || $l->attr("rel") eq "alternate"
!ll&&!rl&&r
2411137
not $l->attr("rel") or $l->attr("rel") eq "alternate"
l!l&&r!l&&!r
1122511
MC/DC100%3/3
$l->attr("href") and !$l->attr("rel") || $l->attr("rel") eq "alternate"
$l->attr("href") not $l->attr("rel") $l->attr("rel") eq "alternate"
Truth table100%4/4
A and not(B) or C
A= $l->attr("href")B= not $l->attr("rel")C= $l->attr("rel") eq "alternate"
ABCresult
0XX0
1000
11X1
1011
107 && (!$l->attr('rel') || $l->attr('rel') eq 'alternate'))
108 {
109 137 $link = trim $l->attr('href');
110 }
111 else {
112 35 ▶ if ($l->text =~ /\w+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->text =~ /\w+/u) 24 11 35
113 24 $link = trim $l->text; # simple link
114 }
115 }
116 166 });
117 166 return $link;
118 13 };
119  
120 31 ▶sub to_string {
Subroutine to_string: called CC=4 SCAR=13.9
Pod documented
121 31 my $self = shift;
122 31 foreach my $k (keys %selector) {
123 217 ▶ 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)) 10 207 217
Condition100%3/3
$self->$k and $self->$k ne $self->_get_selector($k)
!ll&&!rl&&r
620110
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
124 # write it to the DOM:
125 10 $self->_set_selector($k, $self->$k);
126 }
127 }
128 31 $self->dom->to_string;
129 }
130  
131 146 ▶sub to_hash {
Subroutine to_hash: called CC=4 SCAR=13.9
Pod documented
132 146 my $self = shift;
133 146 ▶ my $hash = {map { $_ => '' . ($self->$_ || '') } keys %selector};
Condition100%2/2
$self->$_ || ''
l!l
860162
MC/DC100%1/1
$self->$_ || ''
$self->$_
Truth table100%2/2
A
A= $self->$_
Aresult
11
00
134 146 ▶ if ($self->enclosures->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->enclosures->size) 6 140 146
135 6 $hash->{'enclosures'} = $self->enclosures->map('to_hash')->to_array;
136 }
137 146 ▶ if ($self->tags->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tags->size) 88 58 146
138 88 $hash->{'tags'} = $self->tags->to_array;
139 }
140 146 return $hash;
141 }
142  
143 13 1;
144  
145 __END__
« Mojo/Feed.pm ↑ Summary Mojo/Feed/Item/Enclosure.pm »