| 1 | package Feed::Data::Parser::Base; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 12 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 12 | ▶ | use Feed::Data::Object; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 12 | ▶ | use Compiled::Params::OO qw/cpo/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 12 | ▶ | use HTML::LinkExtor; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 12 | ▶ | use Types::Standard qw/Object ScalarRef Str HashRef ArrayRef/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 12 | our $validate; | |||||||||||||
| 11 | BEGIN { | ||||||||||||||
| 12 | 12 | ▶ | $validate = cpo( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | parse => [Object], | ||||||||||||||
| 14 | first_image_tag => [Object, Str] | ||||||||||||||
| 15 | ); | ||||||||||||||
| 16 | } | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 12 | has 'content_ref' => ( | |||||||||||||
| 19 | is => 'rw', | ||||||||||||||
| 20 | lazy => 1, | ||||||||||||||
| 21 | isa => ScalarRef | ||||||||||||||
| 22 | default => q{}, | ||||||||||||||
| 23 | ); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 12 | has 'parser' => ( | |||||||||||||
| 26 | is => 'rw', | ||||||||||||||
| 27 | isa => Object|HashRef, | ||||||||||||||
| 28 | lazy => 1, | ||||||||||||||
| 29 | ); | ||||||||||||||
| 30 | |||||||||||||||
| 31 | has 'potential_fields' => ( | ||||||||||||||
| 32 | is => 'rw', | ||||||||||||||
| 33 | isa => HashRef, | ||||||||||||||
| 34 | lazy => 1, | ||||||||||||||
| 35 | default => sub { | ||||||||||||||
| 36 | return { | ||||||||||||||
| 37 | 9 | ▶ | title => 'title', | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | description => 'description', | ||||||||||||||
| 39 | date => 'pubDate', | ||||||||||||||
| 40 | author => 'author', | ||||||||||||||
| 41 | category => 'category', | ||||||||||||||
| 42 | permalink => 'permaLink', | ||||||||||||||
| 43 | comment => 'comments', | ||||||||||||||
| 44 | link => 'link', | ||||||||||||||
| 45 | content => 'content', | ||||||||||||||
| 46 | image => 'image', | ||||||||||||||
| 47 | }; | ||||||||||||||
| 48 | }, | ||||||||||||||
| 49 | 12 | ); | |||||||||||||
| 50 | |||||||||||||||
| 51 | has 'feed' => ( | ||||||||||||||
| 52 | is => 'rw', | ||||||||||||||
| 53 | isa => ArrayRef, | ||||||||||||||
| 54 | lazy => 1, | ||||||||||||||
| 55 | default => sub { | ||||||||||||||
| 56 | 13 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.0
| |||||||||||||||
| 57 | 13 | my @feed; | |||||||||||||
| 58 | 13 | foreach my $item ( @{ $self->parser->{items} } ) { | |||||||||||||
| 59 | 24 | my %args; | |||||||||||||
| 60 | 24 | my $potential = $self->potential_fields; | |||||||||||||
| 61 | 24 | ▶ | while (my ($field, $action) = each %{ $potential }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 240 | my $value; | |||||||||||||
| 63 | 240 | ▶ | if ($value = $self->get_value($item, $action)){ | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 64 | 149 | $args{$field} = $value; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | elsif ($action eq 'image') { | ||||||||||||||
| 67 | 22 | my $content = $self->get_value($item, 'content'); | |||||||||||||
| 68 | 22 | ▶ | if ( $content ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 0 | $value = $self->first_image_tag($content); | |||||||||||||
| 70 | 0 | $args{$field} = $value; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | else { | ||||||||||||||
| 73 | 22 | next; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | } | ||||||||||||||
| 76 | } | ||||||||||||||
| 77 | 24 | my $object = Feed::Data::Object->new(object => \%args); | |||||||||||||
| 78 | 24 | push @feed, $object; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | 13 | return \@feed; | |||||||||||||
| 81 | }, | ||||||||||||||
| 82 | 12 | ); | |||||||||||||
| 83 | |||||||||||||||
| 84 | sub parse { | ||||||||||||||
| 85 | 13 | ▶ | my ($self) = $validate->parse->(@_); | ||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 86 | 13 | return $self->feed; | |||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | sub first_image_tag { | ||||||||||||||
| 90 | 0 | ▶ | my ($self, $content) = $validate->first_image_tag->(@_); | ||||||||||||
|
Subroutine
first_image_tag: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 91 | 0 | my $p = HTML::LinkExtor->new; | |||||||||||||
| 92 | 0 | $p->parse($content); | |||||||||||||
| 93 | 0 | my @links = $p->links; | |||||||||||||
| 94 | 0 | for (@links) { | |||||||||||||
| 95 | 0 | ▶ | my ($img, %attr) = @$_ if $_->[0] eq 'img'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | 0 | ▶ | if ($img) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | 0 | ▶ | next if $attr{src} =~ m{twitter|facebook|google|pinterest|tumblr|linkedin}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | 0 | return $attr{src}; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | } | ||||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 12 | 1; # End of Feed::Data | |||||||||||||
| 104 | |||||||||||||||
| 105 | __END__ | ||||||||||||||