Enbld/HTTP.pm

filter: Statementstmt 69.4% 50 / 72 Branchbran 30.0% 6 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 7 totaltotal 60.3% 67 / 111 CC 11 SCAR 18.3
CC
11
Cov
61%
download
13.3
get
9.8
register_get_hook
2.4
CRAP 6.2
« Enbld/Module/Perl.pm ↑ Summary Enbld/Target/Symlink.pm »
1 package Enbld::HTTP;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $ua;
12 4 our $download_hook;
13 4 our $get_hook;
14  
15 4 require Enbld::Message;
16 4 require Enbld::Error;
17 4 require Enbld::Exception;
18  
19 0 ▶sub initialize_ua {
Subroutine initialize_ua: not called CC=1 SCAR=6.9
Pod undocumented
20 0 $ua = HTTP::Tiny->new;
21 }
22  
23 54 ▶sub download {
Subroutine download: called CC=6 SCAR=25.8
Pod undocumented
24 54 my ( $pkg, $url, $path ) = @_;
25  
26 54 my ( undef, undef, $file ) = File::Spec->splitpath( $path );
27  
28 54 ▶ if ( -e $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path) 2 52 54
29 2 my $msg = "--> Use file '$file' that is previously downloaded.";
30 2 Enbld::Message->notify( $msg );
31 2 return $path;
32 }
33  
34 # for debug hook
35 52 ▶ if ( $download_hook ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Enbld::HTTP::download_hook) 52 0 52
36 52 $download_hook->( $pkg, $url, $path );
37 52 return $path;
38 }
39  
40 0 Enbld::Message->notify( "--> Download '$file' from '$url'." );
41  
42 0 my $curl = `which curl`;
43 0 chomp $curl;
44 0 ▶ if ( $curl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curl) { } 0 0 0
45 0 system("$curl -L $url -o $path -s");
46 } else {
47 0 ▶ initialize_ua() unless $ua;
Branch: 1 branch
BranchTrueFalseTotal
unless $Enbld::HTTP::ua 0 0 0
48 0 my $res = $ua->mirror( $url, $path );
49  
50 0 ▶ if ( ! $res->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"success"}) 0 0 0
51 0 Enbld::Error->throw( $res->{reason} );
52 }
53 }
54  
55 0 return $path;
56 }
57  
58 29 ▶sub download_archivefile {
Subroutine download_archivefile: called CC=1 SCAR=0.0
Pod undocumented
59 29 my ( $pkg, $url, $path ) = @_;
60  
61 29 my $downloaded = Enbld::HTTP->download( $url, $path );
62  
63 29 require Enbld::Archivefile;
64 29 return Enbld::Archivefile->new( $downloaded );
65 }
66  
67 35 ▶sub get {
Subroutine get: called CC=4 SCAR=22.9
Pod undocumented
68 35 my ( $pkg, $url ) = @_;
69  
70 35 ▶ if ( $get_hook ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Enbld::HTTP::get_hook) 35 0 35
71 35 return $get_hook->( $pkg, $url );
72 }
73  
74 0 ▶ initialize_ua() unless $ua;
Branch: 1 branch
BranchTrueFalseTotal
unless $Enbld::HTTP::ua 0 0 0
75 0 my $res = $ua->get( $url );
76  
77 0 ▶ if ( ! $res->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"success"}) 0 0 0
78 0 Enbld::Error->throw( $res->{reason} );
79 }
80  
81 0 return $res->{content};
82 }
83  
84 35 ▶sub get_html {
Subroutine get_html: called CC=1 SCAR=0.0
Pod undocumented
85 35 my ( $pkg, $url ) = @_;
86  
87 35 my $content = Enbld::HTTP->get( $url );
88  
89 35 require Enbld::HTML;
90 35 return Enbld::HTML->new( $content );
91 }
92  
93 # For debug methods.
94  
95 3 ▶sub register_get_hook {
Subroutine register_get_hook: called CC=2 SCAR=8.5
Pod undocumented
96 3 my ( $pkg, $coderef ) = @_;
97  
98 3 ▶ if ( ref( $coderef ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $coderef eq "CODE") 3 0 3
99 3 $get_hook = $coderef;
100 3 return $pkg;
101 }
102  
103 0 my $err = "register get hook requires CODE reference parameter.";
104 0 require Enbld::Exception;
105 0 Enbld::Exception->throw( $err, $coderef );
106 }
107  
108 1 ▶sub register_download_hook {
Subroutine register_download_hook: called CC=2 SCAR=8.5
Pod undocumented
109 1 my ( $pkg, $coderef ) = @_;
110  
111 1 ▶ if ( ref( $coderef ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $coderef eq "CODE") 1 0 1
112 1 $download_hook = $coderef;
113 1 return $pkg;
114 }
115  
116 0 my $err = "register download hook requires CODE reference parameter.";
117 0 require Enbld::Exception;
118 0 Enbld::Exception->throw( $err, $coderef );
119 }
120  
121 4 1;
« Enbld/Module/Perl.pm ↑ Summary Enbld/Target/Symlink.pm »