Data/Parser/Base.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 79 CC 4 SCAR 21.5
CC
4
Cov
0%
first_image_tag
20.0
BEGIN
2.0
parse
2.0
CRAP 8.5
1 package Feed::Data::Parser::Base;
2  
3 use Moo;
4 use Feed::Data::Object;
5 use Compiled::Params::OO qw/cpo/;
6 use HTML::LinkExtor;
7  
8 use Types::Standard qw/Object ScalarRef Str HashRef ArrayRef/;
9  
10 our $validate;
11 BEGIN {
12 $validate = cpo(
13 parse => [Object],
14 first_image_tag => [Object, Str]
15 );
16 }
17  
18 has 'content_ref' => (
19 is => 'rw',
20 lazy => 1,
21 isa => ScalarRef
22 default => q{},
23 );
24  
25 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 title => 'title',
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 );
50  
51 has 'feed' => (
52 is => 'rw',
53 isa => ArrayRef,
54 lazy => 1,
55 default => sub {
56 my $self = shift;
57 my @feed;
58 foreach my $item ( @{ $self->parser->{items} } ) {
59 my %args;
60 my $potential = $self->potential_fields;
61 while (my ($field, $action) = each %{ $potential }) {
62 my $value;
63 if ($value = $self->get_value($item, $action)){
64 $args{$field} = $value;
65 }
66 elsif ($action eq 'image') {
67 my $content = $self->get_value($item, 'content');
68 if ( $content ) {
69 $value = $self->first_image_tag($content);
70 $args{$field} = $value;
71 }
72 else {
73 next;
74 }
75 }
76 }
77 my $object = Feed::Data::Object->new(object => \%args);
78 push @feed, $object;
79 }
80 return \@feed;
81 },
82 );
83  
84 sub parse {
85 my ($self) = $validate->parse->(@_);
86 return $self->feed;
87 }
88  
89 sub first_image_tag {
90 my ($self, $content) = $validate->first_image_tag->(@_);
91 my $p = HTML::LinkExtor->new;
92 $p->parse($content);
93 my @links = $p->links;
94 for (@links) {
95 my ($img, %attr) = @$_ if $_->[0] eq 'img';
96 if ($img) {
97 next if $attr{src} =~ m{twitter|facebook|google|pinterest|tumblr|linkedin};
98 return $attr{src};
99 }
100 }
101 }
102  
103 1; # End of Feed::Data
104  
105 __END__