| 1 | package Feed::Pipe; | ||||||||||||||||||||||||||||||||||||||
| 2 | 6 | $Feed::Pipe::VERSION = '1.004'; | |||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use DateTime; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use DateTime::Format::HTTP; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use XML::Feed; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use XML::Atom; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use XML::Atom::Feed; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | 6 | $XML::Atom::DefaultVersion = 1.0; | |||||||||||||||||||||||||||||||||||||
| 11 | 6 | $XML::Atom::ForceUnicode = 1; | |||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | has id => ( | ||||||||||||||||||||||||||||||||||||||
| 14 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||
| 15 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | builder => sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_id: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 17 | 0 | require Data::UUID; | |||||||||||||||||||||||||||||||||||||
| 18 | 0 | my $gen = Data::UUID->new; | |||||||||||||||||||||||||||||||||||||
| 19 | 0 | return 'urn:' . $gen->to_string($gen->create()); | |||||||||||||||||||||||||||||||||||||
| 20 | }, | ||||||||||||||||||||||||||||||||||||||
| 21 | 6 | ); | |||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | 6 | has title => ( | |||||||||||||||||||||||||||||||||||||
| 24 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||
| 25 | default => "Combined Feed", | ||||||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||
| 28 | has updated => ( | ||||||||||||||||||||||||||||||||||||||
| 29 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||
| 30 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | builder => sub { DateTime->now() }, | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_updated: not called CC=1 SCAR=6.9
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 32 | 6 | ); | |||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub count { scalar @{shift->_entries} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
count: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub entries { @{shift->_entries} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
entries: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub _entry_at { shift->_entries->[shift] } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_entry_at: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub _grep { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_grep: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 38 | 0 | my ($self, $sub) = @_; | |||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||
| 40 | 0 | grep $sub->($_), @{$self->_entries} | |||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub _map { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_map: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my ($self, $sub) = @_; | |||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||
| 46 | 0 | map $sub->($_), @{$self->_entries} | |||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||
| 49 | 3 | ▶ | sub _push { push @{shift->_entries}, @_ } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_push: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub _sort_in_place { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_sort_in_place: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my ($self, $cmp) = @_; | |||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||
| 54 | 0 | @{$self->_entries} = (sort { $cmp->($a, $b) } @{$self->_entries}); | |||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | has _entries => ( | ||||||||||||||||||||||||||||||||||||||
| 58 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||
| 59 | 3 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 60 | 6 | ); | |||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||
| 62 | # FIXME: I really want this to add a <source> element to each entry so it can | ||||||||||||||||||||||||||||||||||||||
| 63 | # be traced back to its origin. And to be much more clever. And not to rely | ||||||||||||||||||||||||||||||||||||||
| 64 | # on XML::Feed. | ||||||||||||||||||||||||||||||||||||||
| 65 | 3 | ▶ | sub cat { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
cat: called CC=6 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 66 | 3 | my ($proto, @feed_urls) = @_; | |||||||||||||||||||||||||||||||||||||
| 67 | 3 | ▶ | my $self = ref($proto) ? $proto : $proto->new(); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||
| 69 | 3 | foreach my $f (@feed_urls) { | |||||||||||||||||||||||||||||||||||||
| 70 | 6 | ▶ | if (ref($f) eq 'Feed::Pipe') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $self->_push($f->entries); | |||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||
| 73 | } elsif (ref($f) eq 'XML::Atom::Feed') { | ||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $self->_add_atom($f); | |||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||
| 76 | } elsif (ref($f) =~ /^XML::Feed/) { | ||||||||||||||||||||||||||||||||||||||
| 77 | 0 | $f = $self->_xf_to_atom($f); | |||||||||||||||||||||||||||||||||||||
| 78 | 0 | $self->_add_atom($f); | |||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||||||||||||
| 81 | 6 | my $feed = XML::Feed->parse($f); | |||||||||||||||||||||||||||||||||||||
| 82 | 3 | $feed = $self->_xf_to_atom($feed); | |||||||||||||||||||||||||||||||||||||
| 83 | 3 | $self->_add_atom($feed); | |||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||
| 86 | 0 | return $self; # ALWAYS return $self for chaining! | |||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub sort { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
sort: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my ($self, $sub) = @_; | |||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $sub ||= sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | ($_[1]->updated || $_[1]->published) | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$_[1]->updated || $_[1]->published
$_[0]->updated || $_[0]->published
MC/DC0%0/2
$_[1]->updated || $_[1]->published
$_[1]->updated
$_[1]->published
MC/DC0%0/2
$_[0]->updated || $_[0]->published
$_[0]->updated
$_[0]->published
Truth table0%0/3
A || B
A= $_[1]->updatedB= $_[1]->published
Truth table0%0/3
A || B
A= $_[0]->updatedB= $_[0]->published
| |||||||||||||||||||||||||||||||||||||||
| 93 | cmp($_[0]->updated || $_[0]->published) | ||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | }; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$sub ||= sub {
($_[1]->updated || $_[1]->published) cmp ($_[0]->updated || $_[0]->published);
}
MC/DC0%0/2
$sub ||= sub {
($_[1]->updated || $_[1]->published) cmp ($_[0]->updated || $_[0]->published);
}
$sub
sub {
($_[1]->updated || $_[1]->published) cmp ($_[0]->updated || $_[0]->published);
}
Truth table0%0/3
A ||= B
A= $subB= sub {
($_[1]->updated || $_[1]->published) cmp ($_[0]->updated || $_[0]->published);
}
| |||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $self->_sort_in_place($sub); | |||||||||||||||||||||||||||||||||||||
| 96 | 0 | return $self; # ALWAYS return $self for chaining! | |||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub reverse { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
reverse: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 101 | 0 | $self->_entries([reverse $self->entries]); | |||||||||||||||||||||||||||||||||||||
| 102 | 0 | return $self; # ALWAYS return $self for chaining! | |||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub head { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
head: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my ($self, $limit) = @_; | |||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $limit ||= 10; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$limit ||= 10
MC/DC0%0/1
$limit ||= 10
$limit
Truth table0%0/2
A
A= $limit
| |||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $self->_entries([splice(@{$self->_entries}, 0, $limit)]); | |||||||||||||||||||||||||||||||||||||
| 109 | 0 | return $self; # ALWAYS return $self for chaining! | |||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub tail { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
tail: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my ($self, $limit) = @_; | |||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | $limit ||= 10; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$limit ||= 10
MC/DC0%0/1
$limit ||= 10
$limit
Truth table0%0/2
A
A= $limit
| |||||||||||||||||||||||||||||||||||||||
| 115 | 0 | $self->_entries([splice(@{$self->_entries}, -$limit)]); | |||||||||||||||||||||||||||||||||||||
| 116 | 0 | return $self; # ALWAYS return $self for chaining! | |||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub grep { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
grep: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 120 | 0 | my ($self, $sub) = @_; | |||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | $sub ||= sub { $_->content || $_->summary }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
Condition0%0/3
$sub ||= sub {
$_->content or $_->summary;
}
MC/DC0%0/2
$sub ||= sub {
$_->content or $_->summary;
}
$sub
sub {
$_->content or $_->summary;
}
Truth table0%0/3
A ||= B
A= $subB= sub {
$_->content or $_->summary;
}
| |||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $self->_entries([$self->_grep($sub)]); | |||||||||||||||||||||||||||||||||||||
| 123 | 0 | return $self; # ALWAYS return $self for chaining! | |||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub map { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
map: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 127 | 0 | my ($self, $sub) = @_; | |||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | unless ($sub) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 129 | 0 | my ($package, $file, $line) = caller(); | |||||||||||||||||||||||||||||||||||||
| 130 | 0 | warn | |||||||||||||||||||||||||||||||||||||
| 131 | sprintf('Ignoring map() without a code reference at %s:%s', $file, $line); | ||||||||||||||||||||||||||||||||||||||
| 132 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||
| 134 | 0 | $self->_entries([$self->_map($sub)]); | |||||||||||||||||||||||||||||||||||||
| 135 | 0 | return $self; # ALWAYS return $self for chaining! | |||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub as_atom_obj { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
as_atom_obj: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 139 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 140 | 0 | my $feed = XML::Atom::Feed->new; | |||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||
| 142 | # FIXME: Add support for (at least) the following elements: author category | ||||||||||||||||||||||||||||||||||||||
| 143 | # contributor generator icon link logo rights subtitle | ||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $feed->title($self->title); | |||||||||||||||||||||||||||||||||||||
| 145 | 0 | $feed->id($self->id); | |||||||||||||||||||||||||||||||||||||
| 146 | 0 | $feed->updated(DateTime::Format::HTTP->format_isoz($self->updated)); | |||||||||||||||||||||||||||||||||||||
| 147 | 0 | $feed->add_entry($_) for $self->entries; | |||||||||||||||||||||||||||||||||||||
| 148 | 0 | return $feed; | |||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub as_xml { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
as_xml: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||
| 153 | 0 | return $self->as_atom_obj->as_xml; | |||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||
| 156 | # This code stolen from XML::Feed::convert and mangled slightly. | ||||||||||||||||||||||||||||||||||||||
| 157 | 3 | ▶ | sub _xf_to_atom { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_xf_to_atom: called CC=5 SCAR=28.4
| |||||||||||||||||||||||||||||||||||||||
| 158 | 3 | my ($self, $feed) = @_; | |||||||||||||||||||||||||||||||||||||
| 159 | 3 | ▶ | return $feed->{atom} if $feed->format eq 'Atom'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my $new = XML::Feed->new('Atom'); | |||||||||||||||||||||||||||||||||||||
| 162 | 0 | for my $field ( | |||||||||||||||||||||||||||||||||||||
| 163 | qw( title link self_link description language author copyright modified generator ) | ||||||||||||||||||||||||||||||||||||||
| 164 | ) { | ||||||||||||||||||||||||||||||||||||||
| 165 | 0 | my $val = $feed->$field(); | |||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | next unless defined $val; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 167 | 0 | $new->$field($val); | |||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||
| 169 | 0 | for my $entry ($feed->entries) { | |||||||||||||||||||||||||||||||||||||
| 170 | 0 | $new->add_entry($entry->convert('Atom')); | |||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||
| 172 | 0 | return $new->{atom}; | |||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||
| 175 | 3 | ▶ | sub _add_atom { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_atom: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||
| 176 | 3 | my ($self, $feed) = @_; | |||||||||||||||||||||||||||||||||||||
| 177 | 3 | my @entries = $feed->entries; # This clones the entry nodes. | |||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||
| 179 | # Clean out the entries so we can use $feed as source | ||||||||||||||||||||||||||||||||||||||
| 180 | 3 | for my $node ($feed->elem->childNodes) { | |||||||||||||||||||||||||||||||||||||
| 181 | 75 | ▶ | if ($node->nodeName eq 'entry') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||
| 183 | 12 | $node->unbindNode(); | |||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||
| 186 | 3 | ▶ | $self->_push(map { $_->source($feed) unless $_->source; $_ } @entries); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||
| 189 | 6 | 1; | |||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||
| 191 | __END__ | ||||||||||||||||||||||||||||||||||||||