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 SCAR 29.0
CC
11
Cov
61%
download
13.3
get
9.8
register_get_hook
2.4
CRAP 18.2
« Enbld/Module/Perl.pm ↑ Summary Enbld/Condition.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 sub initialize_ua {
20 0 ▶ $ua = HTTP::Tiny->new;
Subroutine initialize_ua: not called CC=1 SCAR=6.9
Pod undocumented
21 }
22  
23 sub download {
24 54 ▶ my ( $pkg, $url, $path ) = @_;
Subroutine download: called CC=6 SCAR=25.8
Pod undocumented
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 sub download_archivefile {
59 29 ▶ my ( $pkg, $url, $path ) = @_;
Subroutine download_archivefile: called CC=1 SCAR=0.0
Pod undocumented
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 sub get {
68 35 ▶ my ( $pkg, $url ) = @_;
Subroutine get: called CC=4 SCAR=22.9
Pod undocumented
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 sub get_html {
85 35 ▶ my ( $pkg, $url ) = @_;
Subroutine get_html: called CC=1 SCAR=0.0
Pod undocumented
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 sub register_get_hook {
96 3 ▶ my ( $pkg, $coderef ) = @_;
Subroutine register_get_hook: called CC=2 SCAR=8.5
Pod undocumented
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 sub register_download_hook {
109 1 ▶ my ( $pkg, $coderef ) = @_;
Subroutine register_download_hook: called CC=2 SCAR=8.5
Pod undocumented
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/Condition.pm »