Data/Stream.pm

filter: Statementstmt 75.4% 43 / 57 Branchbran 42.8% 6 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 5 totaltotal 67.4% 60 / 89 CC 8 SCAR 12.6
CC
8
Cov
69%
open_url
12.0
__ANON__
4.1
write_file
2.0
CRAP 3.5
« Data.pm ↑ Summary Data/Parser/Atom.pm »
1 package Feed::Data::Stream;
2  
3 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Compiled::Params::OO qw/cpo/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 ▶use Types::Standard qw/Str Any Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 our $validate;
11 BEGIN {
12 13 ▶ $validate = cpo(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 open_stream => [Object],
14 open_url => [Object],
15 open_file => [Object],
16 open_string => [Object],
17 write_file => [Object, Str]
18 );
19 }
20  
21 13 has 'stream' => (
22 is => 'rw',
23 isa => Str,
24 lazy => 1,
25 default => q{}
26 );
27  
28 has 'stream_type' => (
29 is => 'ro',
30 isa => Str,
31 default => sub {
32 19 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=14.2
33 19 ▶ return 'url' if $self->stream =~ m{^http}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $self->stream =~ /^http/msx 0 19 19
34 19 ▶ return 'string' if $self->stream =~ m{\<\?xml}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $self->stream =~ /\<\?xml/msx 0 19 19
35 19 ▶ return 'file' if $self->stream =~ m{(\.xml|\.html|\.txt|\.json|\.csv|\.yml)}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $self->stream =~ /(\.xml|\.html|\.txt|\.json|\.csv|\.yml)/msx 18 1 19
36 }
37 13 );
38  
39 sub open_stream {
40 13 ▶ my ($self) = $validate->open_stream->(@_);
Subroutine open_stream: called CC=1 SCAR=0.0
Pod undocumented
41 13 my $type = 'open_' . $self->stream_type;
42 13 return $self->$type;
43 }
44  
45 sub open_url {
46 0 ▶ my ($self) = $validate->open_url->(@_);
Subroutine open_url: not called CC=3 SCAR=24.8
Pod undocumented
47 0 my $stream = $self->stream;
48 0 my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
49 0 $ua->env_proxy;
50 0 $ua->agent("Mozilla/8.0");
51 0 my $req = HTTP::Request->new( GET => $stream );
52 0 $req->header( 'Accept-Encoding', 'gzip' );
53 0 ▶ my $res = $ua->request($req) or croak "Failed to fetch URI: $stream";
Branch: 1 branch
BranchTrueFalseTotal
my $res = $ua->request($req) or croak("Failed to fetch URI: $stream") 0 0 0
54 0 ▶ if ( $res->code == 410 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 410) 0 0 0
55 0 croak "This feed has been permantly removed";
56 }
57 0 my $content = $res->decoded_content(charset => 'utf8');
58 0 return \$content;
59 }
60  
61 sub open_file {
62 13 ▶ my ($self) = $validate->open_file->(@_);
Subroutine open_file: called CC=2 SCAR=7.0
Pod undocumented
63  
64 13 my $stream = $self->stream;
65  
66 13 ▶ open ( my $fh, '<', $stream ) or croak "could not open file: $stream";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $stream or croak("could not open file: $stream") 0 13 13
67  
68 13 my $content = do { local $/; <$fh> };
69 13 close $fh;
70  
71 13 return \$content;
72 }
73  
74 sub open_string {
75 0 ▶ my ($self) = $validate->open_string(@_);
Subroutine open_string: not called CC=1 SCAR=6.9
Pod undocumented
76 0 return shift->stream;
77 }
78  
79 sub write_file {
80 6 ▶ my ($self, $feed) = $validate->write_file->(@_);
Subroutine write_file: called CC=2 SCAR=7.0
Pod undocumented
81 6 my $stream = $self->stream;
82 6 ▶ open my $FILE, ">", $stream or croak "could not open file: $stream";
Branch: 1 branch
BranchTrueFalseTotal
open my $FILE, ">", $stream or croak("could not open file: $stream") 0 6 6
83 6 print $FILE $feed;
84 6 close $FILE;
85 }
86  
87 13 1; # End of Feed::Data
« Data.pm ↑ Summary Data/Parser/Atom.pm »