| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 1081 | ▶ | if ($k eq 'author' && $p->at('name')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$k eq "author" and $p->at("name")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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 ||= ""
MC/DC100%1/1
$text ||= ""
$text
Truth table100%2/2
A
A= $text
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1045 | ▶ | if ($k eq 'published') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 10 | ▶ | if ($k eq 'author' && $p->at('name')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$k eq "author" and $p->at("name")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | return $p->at('name')->content($val); | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 10 | ▶ | if ($k eq 'published') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
(shift())->_get_selector($k) || undef
(shift())->_get_selector($k)
Truth table100%2/2
A
A= (shift())->_get_selector($k)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$l->attr("href") and $l->attr("rel") and $l->attr("rel") eq "enclosure"
$l->attr("href") and $l->attr("rel")
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$l->attr("href") and !$l->attr("rel") || $l->attr("rel") eq "alternate"
not $l->attr("rel") or $l->attr("rel") eq "alternate"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$self->$k and $self->$k ne $self->_get_selector($k)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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->$_ || ''
MC/DC100%1/1
$self->$_ || ''
$self->$_
Truth table100%2/2
A
A= $self->$_
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 146 | ▶ | if ($self->enclosures->size) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 6 | $hash->{'enclosures'} = $self->enclosures->map('to_hash')->to_array; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 146 | ▶ | if ($self->tags->size) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 88 | $hash->{'tags'} = $self->tags->to_array; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 146 | return $hash; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||||||||||||||||||||||||||