Headline.pm

filter: Statementstmt 24.5% 29 / 118 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 37.5% 9 / 24 Podpod 100% 13 / 13 totaltotal 22.1% 51 / 230 CC 33 SCAR 41.1
CC
33
Cov
16%
new
210.0
_description_headline
42.0
id
12.0
CRAP 60.9
↑ Summary Headline/PerlJobs.pm »
1 package XML::RSS::Headline;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Digest::MD5 qw(md5_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use HTML::Entities qw(decode_entities);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # length of headline when from description
12 7 ▶use constant DESCRIPTION_HEADLINE => 45;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 our $VERSION = 2.32;
15  
16 0 ▶sub new {
Subroutine new: not called CC=14 SCAR=53.5
Pod documented
17 0 my ( $class, %args ) = @_;
18 0 my $self = bless {}, $class;
19 0 my $first_seen = $args{first_seen};
20 0 ▶ my $headline_as_id = $args{headline_as_id} || 0;
Condition0%0/2
$args{'headline_as_id'} || 0
l!l
00
MC/DC0%0/1
$args{'headline_as_id'} || 0
$args{'headline_as_id'}
Truth table0%0/2
A
A= $args{'headline_as_id'}
Aresult
11
00
21 0 ▶ delete $args{first_seen} if exists $args{first_seen};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"first_seen"} 0 0 0
22 0 ▶ delete $args{headline_as_id} if exists $args{headline_as_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"headline_as_id"} 0 0 0
23  
24 0 ▶ if ( $args{item} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'item'}) { } 0 0 0
25 0 ▶ unless ( ( $args{item}->{title} || $args{item}->{description} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"item"}{"title"} || $args{"item"}{"description"} and $args{"item"}{"link"}) 0 0 0
Condition0%0/6
$args{"item"}{"title"} || $args{"item"}{"description"} and $args{"item"}{"link"}
!ll&&!rl&&r
000
$args{"item"}{"title"} or $args{"item"}{"description"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{"item"}{"title"} || $args{"item"}{"description"} and $args{"item"}{"link"}
$args{"item"}{"title"} $args{"item"}{"description"} $args{"item"}{"link"}
Truth table0%0/5
A or B and C
A= $args{"item"}{"title"}B= $args{"item"}{"description"}C= $args{"item"}{"link"}
ABCresult
00X0
1X00
0100
1X11
0111
26 && $args{item}->{link} )
27 {
28 0 carp 'item must contain either title/link or description/link';
29 0 return;
30 }
31 }
32 else {
33 0 ▶ unless ( $args{url} && ( $args{headline} || $args{description} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"url"} and $args{"headline"} || $args{"description"}) 0 0 0
Condition0%0/6
$args{"url"} and $args{"headline"} || $args{"description"}
!ll&&!rl&&r
000
$args{"headline"} or $args{"description"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{"url"} and $args{"headline"} || $args{"description"}
$args{"url"} $args{"headline"} $args{"description"}
Truth table0%0/4
A and B or C
A= $args{"url"}B= $args{"headline"}C= $args{"description"}
ABCresult
0XX0
1000
11X1
1011
34 0 carp 'Either item, url/headline. or url/description are required';
35 0 return;
36 }
37 }
38  
39 0 $self->headline_as_id($headline_as_id);
40  
41 0 for my $method ( keys %args ) {
42 0 ▶ if ( $self->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) { } 0 0 0
43 0 $self->$method( $args{$method} );
44 }
45 else {
46 0 carp "Invalid argument: '$method'";
47 }
48 }
49  
50 0 ▶ unless ( $self->headline ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->headline) 0 0 0
51 0 carp 'Failed to set headline';
52 0 return;
53 }
54  
55 0 $self->set_first_seen($first_seen);
56 0 return $self;
57 }
58  
59 0 ▶sub id {
Subroutine id: not called CC=3 SCAR=24.8
Pod documented
60 0 my ($self) = @_;
61 0 ▶ return $self->{_rss_headline_id} if $self->headline_as_id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->headline_as_id 0 0 0
62 0 ▶ return $self->guid || $self->url;
Condition0%0/3
$self->guid || $self->url
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->guid || $self->url
$self->guid $self->url
Truth table0%0/3
A || B
A= $self->guidB= $self->url
ABresult
1X1
011
000
63 }
64  
65 0 ▶sub guid {
Subroutine guid: not called CC=2 SCAR=17.9
Pod documented
66 0 my ( $self, $guid ) = @_;
67 0 ▶ $self->{guid} = $guid if $guid;
Branch: 1 branch
BranchTrueFalseTotal
if $guid 0 0 0
68 0 return $self->{guid};
69 }
70  
71 0 ▶sub _cache_id {
Subroutine _cache_id: not called CC=2 SCAR=17.9
72 0 my ($self) = @_;
73 $self->{_rss_headline_id}
74 = md5_base64( encode_utf8( $self->{safe_headline} ) )
75 0 ▶ if $self->{safe_headline};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"safe_headline"} 0 0 0
76 0 return;
77 }
78  
79 0 ▶sub multiline_headline {
Subroutine multiline_headline: not called CC=2 SCAR=17.9
Pod documented
80 0 my ($self) = @_;
81 0 my @multiline_headline = split /\n/, $self->headline;
82 0 ▶ return wantarray ? @multiline_headline : \@multiline_headline;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
83 }
84  
85 0 ▶sub item {
Subroutine item: not called CC=2 SCAR=17.9
Pod documented
86 0 my ( $self, $item ) = @_;
87 0 ▶ return unless $item;
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 0 0
88 0 $self->url( $item->{link} );
89 0 $self->headline( $item->{title} );
90 0 $self->description( $item->{description} );
91 0 $self->guid( $item->{guid} );
92 0 return;
93 }
94  
95 0 ▶sub set_first_seen {
Subroutine set_first_seen: not called CC=2 SCAR=17.9
Pod documented
96 0 my ( $self, $hires_time ) = @_;
97 0 $self->{hires_timestamp} = $hires_time;
98 0 ▶ $self->{hires_timestamp} = Time::HiRes::time() unless $hires_time;
Branch: 1 branch
BranchTrueFalseTotal
unless $hires_time 0 0 0
99 0 return 1;
100 }
101  
102 0 ▶sub first_seen {
Subroutine first_seen: not called CC=1 SCAR=6.9
Pod documented
103 0 my ($self) = @_;
104 0 return int $self->{hires_timestamp};
105 }
106  
107 0 ▶sub first_seen_hires {
Subroutine first_seen_hires: not called CC=1 SCAR=6.9
Pod documented
108 0 my ($self) = @_;
109 0 return $self->{hires_timestamp};
110 }
111  
112 0 ▶sub headline {
Subroutine headline: not called CC=3 SCAR=24.8
Pod documented
113 0 my ( $self, $headline ) = @_;
114 0 ▶ if ($headline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headline) 0 0 0
115 0 $self->{headline} = decode_entities $headline;
116 0 ▶ if ( $self->{headline_as_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"headline_as_id"}) 0 0 0
117 0 $self->{safe_headline} = $headline;
118 0 $self->_cache_id;
119 }
120 }
121 0 return $self->{headline};
122 }
123  
124 0 ▶sub url {
Subroutine url: not called CC=2 SCAR=17.9
Pod documented
125 0 my ( $self, $url ) = @_;
126  
127 # clean the URL up a bit
128 0 ▶ $self->{url} = URI->new($url)->canonical if $url;
Branch: 1 branch
BranchTrueFalseTotal
if $url 0 0 0
129 0 return $self->{url};
130 }
131  
132 0 ▶sub description {
Subroutine description: not called CC=3 SCAR=24.8
Pod documented
133 0 my ( $self, $description ) = @_;
134 0 ▶ if ($description) {
Branch: 1 branch
BranchTrueFalseTotal
if ($description) 0 0 0
135 0 $self->{description} = decode_entities $description;
136 0 ▶ $self->_description_headline unless $self->headline;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->headline 0 0 0
137 }
138 0 return $self->{description};
139 }
140  
141 0 ▶sub _description_headline {
Subroutine _description_headline: not called CC=6 SCAR=37.4
142 0 my ($self) = @_;
143 0 my $punctuation = qr/[.,?!:;]+/s;
144  
145 0 my $description = $self->{description};
146 0 $description =~ s/<br *\/*>/\n/g; # turn br into newline
147 0 $description =~ s/<.+?>/ /g;
148  
149 0 ▶ my $headline = ( split $punctuation, $description )[0] || '';
Condition0%0/2
(split(/$punctuation/, $description, 0))[0] || ''
l!l
00
MC/DC0%0/1
(split(/$punctuation/, $description, 0))[0] || ''
(split(/$punctuation/, $description, 0))[0]
Truth table0%0/2
A
A= (split(/$punctuation/, $description, 0))[0]
Aresult
11
00
150 0 $headline =~ s/^\s+//;
151 0 $headline =~ s/\s+$//;
152  
153 0 my $build_headline = '';
154 0 for my $word ( split /\s+/, $headline ) {
155 0 ▶ $build_headline .= ' ' if $build_headline;
Branch: 1 branch
BranchTrueFalseTotal
if $build_headline 0 0 0
156 0 $build_headline .= $word;
157 0 ▶ last if length $build_headline > DESCRIPTION_HEADLINE;
Branch: 1 branch
BranchTrueFalseTotal
if length $build_headline > 45 0 0 0
158 }
159  
160 0 ▶ return unless $build_headline;
Branch: 1 branch
BranchTrueFalseTotal
unless $build_headline 0 0 0
161 0 $self->headline( $build_headline .= '...' );
162 0 return;
163 }
164  
165 0 ▶sub headline_as_id {
Subroutine headline_as_id: not called CC=2 SCAR=17.9
Pod documented
166 0 my ( $self, $bool ) = @_;
167 0 ▶ if ( defined $bool ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bool) 0 0 0
168 0 $self->{headline_as_id} = $bool;
169 0 $self->_cache_id;
170 }
171 0 return $self->{headline_as_id};
172 }
173  
174 0 ▶sub timestamp {
Subroutine timestamp: not called CC=2 SCAR=17.9
Pod documented
175 0 my ( $self, $timestamp ) = @_;
176 0 ▶ $self->{timestamp} = $timestamp if $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if $timestamp 0 0 0
177 0 return $self->{timestamp};
178 }
179  
180 7 1;
181  
182 __END__
↑ Summary Headline/PerlJobs.pm »