Feed.pm

filter: Statementstmt 79.2% 157 / 198 Branchbran 50.0% 40 / 80 Conditioncond 45.0% 9 / 20 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 84.8% 28 / 33 Podpod 92.3% 12 / 13 totaltotal 70.0% 250 / 357 CC 54 SCAR 25.3
CC
54
Cov
69%
_parse_args
56.0
useragent
30.0
parse
21.5
CRAP 12.6
« Feed/Entry/Format/RSS.pm ↑ Summary Feed/Util.pm »
1 package XML::Feed;
2 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 35 ▶use base qw( Class::ErrorHandler );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use Feed::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use URI::Fetch;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use Module::Pluggable search_path => "XML::Feed::Format",
Subroutine BEGIN: called CC=1 SCAR=0.0
13 require => 1,
14 35 sub_name => 'formatters';
15  
16 35 our $VERSION = '1.0.2';
17 35 our $MULTIPLE_ENCLOSURES = 0;
18 35 our @formatters;
19 35 our %formatters;
20 35 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
21 35 @formatters = __PACKAGE__->formatters;
22 35 for my $formatter (@formatters) {
23 70 (my $name = $formatter) =~ s/^XML::Feed::Format:://;
24 70 $formatters{$name} = 1;
25 }
26 }
27  
28 0 ▶sub _parse_args {
Subroutine _parse_args: not called CC=7 SCAR=40.3
29 0 my $class = shift;
30 0 my ($possible_args) = @_;
31  
32 0 my @valid_args = qw[useragent];
33 0 my $args = {};
34  
35 0 for (@valid_args) {
36 $args->{$_} = delete $possible_args->{$_}
37 0 ▶ if exists $possible_args->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $possible_args->{$_} 0 0 0
38 }
39  
40 0 ▶ unless (keys %$args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$args) 0 0 0
41 0 Carp::croak('No valid keys passed to ' . __PACKAGE__ . 'new()');
42 }
43  
44 # Validate useragent parameter if provided
45 0 ▶ if (exists $args->{useragent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"useragent"}) 0 0 0
46 0 ▶ unless (blessed($args->{useragent}) && $args->{useragent}->isa('LWP::UserAgent')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($args->{"useragent"}) and $args->{"useragent"}->isa("LWP::UserAgent")) 0 0 0
Condition0%0/3
builtin::blessed($args->{"useragent"}) and $args->{"useragent"}->isa("LWP::UserAgent")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($args->{"useragent"}) and $args->{"useragent"}->isa("LWP::UserAgent")
builtin::blessed($args->{"useragent"}) $args->{"useragent"}->isa("LWP::UserAgent")
Truth table0%0/3
A and B
A= builtin::blessed($args->{"useragent"})B= $args->{"useragent"}->isa("LWP::UserAgent")
ABresult
0X0
100
111
47 0 Carp::croak("useragent must be an LWP::UserAgent object");
48 }
49 }
50  
51 0 return $args;
52 }
53  
54 14 ▶sub new {
Subroutine new: called CC=7 SCAR=20.4
Pod documented
55 14 my $class = shift;
56 14 ▶ my $format = shift // 'Atom';
Condition100%2/2
shift() // "Atom"
l!l
131
MC/DC100%1/1
shift() // "Atom"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
57
58 # Handle optional args hash
59 14 my $args = {};
60  
61 14 ▶ if (@_ && ref($_[-1]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and ref $_[-1] eq "HASH") 0 14 14
Condition33%1/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
1400
MC/DC0%0/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
62 0 $args = $class->_parse_args(pop @_);
63 }
64
65 Carp::croak("Unsupported format $format: not a recognised feed format")
66 14 ▶ unless $formatters{$format};
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::Feed::formatters{$format} 1 13 14
67 13 my $format_class = 'XML::Feed::Format::' . $format;
68 13 eval "use $format_class";
69 13 ▶ Carp::croak("Unsupported format $format: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 13 13
70 13 my $feed = bless $args, join('::', __PACKAGE__, "Format", $format);
71
72 13 ▶ $feed->init_empty(@_) or return $class->error($feed->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$feed->init_empty(@_) or return $class->error($feed->errstr) 7 0 7
73 7 $feed;
74 }
75  
76 0 ▶sub init_empty { 1 }
Subroutine init_empty: not called CC=1 SCAR=6.9
Pod undocumented
77  
78 36 ▶sub parse {
Subroutine parse: called CC=17 SCAR=30.7
Pod documented
79 36 my $class = shift;
80 36 my($stream) = @_;
81 36 ▶ return $class->error("Stream parameter is required") unless $stream;
Branch: 1 branch
BranchTrueFalseTotal
unless $stream 0 36 36
82  
83 36 my ($specified_format, $args) = (undef, {});
84  
85 36 ▶ if (@_ == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 3) { } 0 36 36
elsif (@_ == 2) { } 0 36 36
86 0 ($specified_format, $args) = @_[1, 2];
87 } elsif (@_ == 2) {
88 0 ▶ if (ref($_[-1]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] eq 'HASH') { } 0 0 0
89 0 $args = $_[1];
90 } else {
91 0 $specified_format = $_[1];
92 }
93 }
94  
95 36 ▶ if (keys %$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$args) 0 36 36
96 0 $args = $class->_parse_args($args);
97 }
98  
99 36 my $feed = bless $args, $class;
100 36 my $xml = '';
101 36 ▶ if (blessed($stream) and $stream->isa('URI')) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($stream) and $stream->isa('URI')) { } 1 35 36
elsif (ref $stream eq 'SCALAR') { } 8 27 35
elsif (ref $stream) { } 1 26 27
Condition66%2/3
builtin::blessed($stream) and $stream->isa("URI")
!ll&&!rl&&r
3501
MC/DC50%1/2
builtin::blessed($stream) and $stream->isa("URI")
builtin::blessed($stream) $stream->isa("URI")
Truth table66%2/3
A and B
A= builtin::blessed($stream)B= $stream->isa("URI")
ABresult
0X0
100
111
102 1 $xml = $feed->get_uri($stream);
103 } elsif (ref($stream) eq 'SCALAR') {
104 8 $xml = $$stream;
105 } elsif (ref($stream)) {
106 1 $xml = $feed->get_fh($stream);
107 } else {
108 26 $xml = $feed->get_file($stream);
109 }
110 36 my $errstr = "Can't get feed XML content from $stream";
111 36 ▶ if ($class->errstr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->errstr) 0 36 36
112 0 $errstr .= ": " . $class->errstr;
113 }
114 36 ▶ return $class->error($errstr)
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 36 36
115 unless $xml;
116 36 my $format;
117 36 ▶ if ($specified_format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($specified_format) { } 0 36 36
118 0 $format = $specified_format;
119 } else {
120 36 ▶ $format = $feed->identify_format(\$xml) or return $class->error($feed->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$format = $feed->identify_format(\$xml) or return $class->error($feed->errstr) 36 0 36
121 }
122  
123 36 my $format_class = join '::', __PACKAGE__, "Format", $format;
124 return $class->error("Unsupported format $format: not a recognised feed format")
125 36 ▶ unless $formatters{$format};
Branch: 1 branch
BranchTrueFalseTotal
unless $XML::Feed::formatters{$format} 0 36 36
126 36 ▶ eval "use $format_class";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
127 36 ▶ return $class->error("Unsupported format $format: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 36 36
128 36 bless $feed, $format_class;
129 36 ▶ $feed->init_string(\$xml) or return $class->error($feed->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$feed->init_string(\$xml) or return $class->error($feed->errstr) 18 0 18
130 18 $feed;
131 }
132  
133 26 ▶sub get_file {
Subroutine get_file: called CC=2 SCAR=7.0
Pod documented
134 26 my $class = shift;
135 26 my ($filename) = @_;
136  
137 26 ▶ open my $fh, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or return $class->error("Can't open ${filename}: $!") 26 0 26
138 or return $class->error("Can't open $filename: $!");
139 26 my $xml = $class->get_fh($fh);
140 26 close $fh;
141  
142 26 return $xml;
143 }
144  
145 27 ▶sub get_fh {
Subroutine get_fh: called CC=2 SCAR=6.9
Pod documented
146 27 my $class = shift;
147 27 my ($fh) = @_;
148  
149 27 my $xml;
150 27 ▶ while (read $fh, my($chunk), 8192) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $chunk, 8192) 28 27 55
151 28 $xml .= $chunk;
152 }
153  
154 27 return $xml;
155 }
156  
157 1 ▶sub get_uri {
Subroutine get_uri: called CC=5 SCAR=17.4
Pod documented
158 1 my $class = shift;
159 1 my ($stream) = @_;
160  
161 # Get or create UserAgent
162 1 my $ua;
163 1 ▶ if (blessed($class) && $class->{useragent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($class) and $class->{'useragent'}) { } 0 1 1
Condition33%1/3
builtin::blessed($class) and $class->{"useragent"}
!ll&&!rl&&r
010
MC/DC0%0/2
builtin::blessed($class) and $class->{"useragent"}
builtin::blessed($class) $class->{"useragent"}
Truth table33%1/3
A and B
A= builtin::blessed($class)B= $class->{"useragent"}
ABresult
0X0
100
111
164 0 $ua = $class->{useragent};
165 } else {
166 1 $ua = LWP::UserAgent->new;
167 1 $ua->agent(__PACKAGE__ . "/$VERSION");
168 1 $ua->env_proxy; # force allowing of proxies
169 }
170
171 1 ▶ my $res = URI::Fetch->fetch($stream, UserAgent => $ua)
Branch: 1 branch
BranchTrueFalseTotal
my $res = "URI::Fetch"->fetch($stream, "UserAgent", $ua) or return $class->error("URI::Fetch"->errstr) 1 0 1
172 or return $class->error(URI::Fetch->errstr);
173 1 ▶ return $class->error("This feed has been permanently removed")
Branch: 1 branch
BranchTrueFalseTotal
if $res->status == 410 0 1 1
174 if $res->status == URI::Fetch::URI_GONE();
175 1 return $res->content;
176 }
177  
178 36 ▶sub identify_format {
Subroutine identify_format: called CC=4 SCAR=14.0
Pod documented
179 36 my $feed = shift;
180 36 my($xml) = @_;
181 36 foreach my $class (@formatters) {
182 54 my ($name) = ($class =~ m!([^:]+)$!);
183 # TODO ugly
184 54 my $tmp = $$xml;
185 54 ▶ return $name if eval { $class->identify(\$tmp) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $class->identify(\$tmp) } } 36 18 54
186 18 ▶ return $feed->error($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 18 18
187 }
188 0 return $feed->error("Cannot detect feed type");
189 }
190  
191 54 ▶sub _get_first_tag {
Subroutine _get_first_tag: called CC=5 SCAR=16.2
192 54 my $class = shift;
193 54 my ($xml) = @_;
194  
195  
196 ## Auto-detect feed type based on first element. This is prone
197 ## to breakage, but then again we don't want to parse the whole
198 ## feed ourselves.
199 54 my $tag;
200 54 ▶ while ($$xml =~ /<(\S+)/sg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$xml =~ /<(\S+)/gs) 114 0 114
201 114 (my $t = $1) =~ tr/a-zA-Z0-9:\-\?!//cd;
202 114 my $first = substr $t, 0, 1;
203 114 ▶ $tag = $t, last unless $first eq '?' || $first eq '!';
Branch: 1 branch
BranchTrueFalseTotal
unless $first eq "?" or $first eq "!" 54 60 114
Condition100%3/3
$first eq "?" or $first eq "!"
l!l&&r!l&&!r
58254
MC/DC100%2/2
$first eq "?" or $first eq "!"
$first eq "?" $first eq "!"
Truth table100%3/3
A or B
A= $first eq "?"B= $first eq "!"
ABresult
1X1
011
000
204 }
205 54 ▶ die ("Cannot find first element") unless $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag 0 54 54
206 54 $tag =~ s/^.*://;
207 54 return $tag;
208 }
209  
210 0 ▶sub find_feeds {
Subroutine find_feeds: not called CC=2 SCAR=17.9
Pod documented
211 0 my $class = shift;
212 0 my($uri) = @_;
213 0 ▶ my @feeds = Feed::Find->find($uri)
Branch: 1 branch
BranchTrueFalseTotal
my(@feeds) = "Feed::Find"->find($uri) or return $class->error("Feed::Find"->errstr) 0 0 0
214 or return $class->error(Feed::Find->errstr);
215 0 @feeds;
216 }
217  
218 2 ▶sub convert {
Subroutine convert: called CC=4 SCAR=13.9
Pod documented
219 2 my $feed = shift;
220 2 my($format) = @_;
221 2 my $new = XML::Feed->new($format);
222 2 for my $field (qw( title link description language author copyright modified generator )) {
223 16 my $val = $feed->$field();
224 16 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 5 11 16
225 11 $new->$field($val);
226 }
227 2 for my $entry ($feed->entries) {
228 3 $new->add_entry($entry->convert($format));
229 }
230 2 $new;
231 }
232  
233 2 ▶sub splice {
Subroutine splice: called CC=3 SCAR=11.0
Pod documented
234 2 my $feed = shift;
235 2 my($other) = @_;
236 2 my %ids = map { $_->id => 1 } $feed->entries;
237 2 for my $entry ($other->entries) {
238 4 ▶ $feed->add_entry($entry) unless $ids{$entry->id}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids{$entry->id}++ 2 2 4
239 }
240 }
241  
242 8 ▶sub _convert_entry {
Subroutine _convert_entry: called CC=2 SCAR=7.1
243 8 my $feed = shift;
244 8 my $entry = shift;
245 8 my $feed_format = ref($feed); $feed_format =~ s!^XML::Feed::Format::!!;
246 8 my $entry_format = ref($entry); $entry_format =~ s!^XML::Feed::Entry::Format::!!;
247 8 ▶ return $entry if $entry_format eq $feed_format;
Branch: 1 branch
BranchTrueFalseTotal
if $entry_format eq $feed_format 8 0 8
248 0 return $entry->convert($feed_format);
249 }
250  
251 0 ▶sub useragent {
Subroutine useragent: not called CC=5 SCAR=34.0
Pod documented
252 0 my $feed = shift;
253 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
254 # Setter: validate the useragent
255 0 my $ua = shift;
256 0 ▶ unless (blessed($ua) && $ua->isa('LWP::UserAgent')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($ua) and $ua->isa("LWP::UserAgent")) 0 0 0
Condition0%0/3
builtin::blessed($ua) and $ua->isa("LWP::UserAgent")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($ua) and $ua->isa("LWP::UserAgent")
builtin::blessed($ua) $ua->isa("LWP::UserAgent")
Truth table0%0/3
A and B
A= builtin::blessed($ua)B= $ua->isa("LWP::UserAgent")
ABresult
0X0
100
111
257 0 Carp::croak("useragent must be an LWP::UserAgent object");
258 }
259 0 $feed->{useragent} = $ua;
260 0 return $ua;
261 } else {
262 # Getter: return existing or create new one
263 0 ▶ unless ($feed->{useragent}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($feed->{"useragent"}) 0 0 0
264 0 $feed->{useragent} = LWP::UserAgent->new;
265 0 $feed->{useragent}->agent(__PACKAGE__ . "/$VERSION");
266 0 $feed->{useragent}->env_proxy;
267 }
268 0 return $feed->{useragent};
269 }
270 }
271  
272 sub base;
273 sub format;
274 sub title;
275 sub link;
276 sub self_link;
277 sub description;
278 sub language;
279 sub author;
280 sub copyright;
281 sub modified;
282 sub generator;
283 sub add_entry;
284 sub entries;
285 sub as_xml;
286 sub id;
287 sub image;
288  
289 4 ▶sub tagline { shift->description(@_) }
Subroutine tagline: called CC=1 SCAR=0.0
Pod documented
290 0 ▶sub items { $_[0]->entries }
Subroutine items: not called CC=1 SCAR=6.9
Pod documented
291  
292 # RFC 5005
293 sub first_link;
294 sub last_link;
295 sub previous_link;
296 sub next_link;
297 sub current_link;
298 sub prev_archive_link;
299 sub next_archive_link;
300  
301 35 1;
302 __END__
« Feed/Entry/Format/RSS.pm ↑ Summary Feed/Util.pm »