File Coverage

blib/lib/Feed/Data/Parser/Base.pm
Criterion Covered Total %
statement 18 27 66.6
branch 0 6 0.0
condition n/a
subroutine 7 8 87.5
pod 0 2 0.0
total 25 43 58.1


line stmt bran cond sub pod time code
1             package Feed::Data::Parser::Base;
2              
3 12     12   6318 use Moo;
  12         44  
  12         66  
4 12     12   4355 use Feed::Data::Object;
  12         361  
  12         177  
5 12     12   402 use Compiled::Params::OO qw/cpo/;
  12         46  
  12         90  
6 12     12   7056 use HTML::LinkExtor;
  12         29791  
  12         131  
7              
8 12     12   451 use Types::Standard qw/Object ScalarRef Str HashRef ArrayRef/;
  12         24  
  12         72  
9              
10             our $validate;
11             BEGIN {
12 12     12   13351 $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 13     13 0 111 my ($self) = $validate->parse->(@_);
86 13         436 return $self->feed;
87             }
88              
89             sub first_image_tag {
90 0     0 0   my ($self, $content) = $validate->first_image_tag->(@_);
91 0           my $p = HTML::LinkExtor->new;
92 0           $p->parse($content);
93 0           my @links = $p->links;
94 0           for (@links) {
95 0 0         my ($img, %attr) = @$_ if $_->[0] eq 'img';
96 0 0         if ($img) {
97 0 0         next if $attr{src} =~ m{twitter|facebook|google|pinterest|tumblr|linkedin};
98 0           return $attr{src};
99             }
100             }
101             }
102              
103             1; # End of Feed::Data
104              
105             __END__