File Coverage

blib/lib/Enbld/HTTP.pm
Criterion Covered Total %
statement 43 61 70.4
branch 6 18 33.3
condition n/a
subroutine 11 12 91.6
pod 0 7 0.0
total 60 98 61.2


line stmt bran cond sub pod time code
1             package Enbld::HTTP;
2              
3 4     4   1518 use strict;
  4         4  
  4         98  
4 4     4   12 use warnings;
  4         5  
  4         76  
5              
6 4     4   15 use File::Spec;
  4         4  
  4         69  
7 4     4   11 use Carp;
  4         3  
  4         163  
8              
9 4     4   2185 use HTTP::Tiny;
  4         117379  
  4         1796  
10              
11             our $ua;
12             our $download_hook;
13             our $get_hook;
14              
15             require Enbld::Message;
16             require Enbld::Error;
17             require Enbld::Exception;
18              
19             sub initialize_ua {
20 0     0 0 0 $ua = HTTP::Tiny->new;
21             }
22              
23             sub download {
24 54     54 0 139 my ( $pkg, $url, $path ) = @_;
25              
26 54         828 my ( undef, undef, $file ) = File::Spec->splitpath( $path );
27              
28 54 100       1096 if ( -e $path ) {
29 2         9 my $msg = "--> Use file '$file' that is previously downloaded.";
30 2         12 Enbld::Message->notify( $msg );
31 2         5 return $path;
32             }
33              
34             # for debug hook
35 52 50       130 if ( $download_hook ) {
36 52         220 $download_hook->( $pkg, $url, $path );
37 52         16614 return $path;
38             }
39              
40 0         0 Enbld::Message->notify( "--> Download '$file' from '$url'." );
41              
42 0 0       0 initialize_ua() unless $ua;
43 0         0 my $res = $ua->mirror( $url, $path );
44              
45 0 0       0 if ( ! $res->{success} ) {
46 0         0 die Enbld::Error->new( $res->{reason} );
47             }
48              
49 0         0 return $path;
50             }
51              
52             sub download_archivefile {
53 29     29 0 48 my ( $pkg, $url, $path ) = @_;
54              
55 29         152 my $downloaded = Enbld::HTTP->download( $url, $path );
56              
57 29         495 require Enbld::Archivefile;
58 29         360 return Enbld::Archivefile->new( $downloaded );
59             }
60              
61             sub get {
62 35     35 0 69 my ( $pkg, $url ) = @_;
63              
64 35 50       104 if ( $get_hook ) {
65 35         180 return $get_hook->( $pkg, $url );
66             }
67              
68 0 0       0 initialize_ua() unless $ua;
69 0         0 my $res = $ua->get( $url );
70              
71 0 0       0 if ( ! $res->{success} ) {
72 0         0 die Enbld::Error->new( $res->{reason} );
73             }
74              
75 0         0 return $res->{content};
76             }
77              
78             sub get_html {
79 35     35 0 62 my ( $pkg, $url ) = @_;
80              
81 35         129 my $content = Enbld::HTTP->get( $url );
82              
83 35         1420 require Enbld::HTML;
84 35         279 return Enbld::HTML->new( $content );
85             }
86              
87             # For debug methods.
88              
89             sub register_get_hook {
90 3     3 0 897 my ( $pkg, $coderef ) = @_;
91              
92 3 50       15 if ( ref( $coderef ) eq 'CODE' ) {
93 3         3 $get_hook = $coderef;
94 3         7 return $pkg;
95             }
96              
97 0         0 my $err = "register get hook requires CODE reference parameter.";
98 0         0 require Enbld::Exception;
99 0         0 croak( Enbld::Exception->new( $err, $coderef ));
100             }
101              
102             sub register_download_hook {
103 1     1 0 3 my ( $pkg, $coderef ) = @_;
104              
105 1 50       3 if ( ref( $coderef ) eq 'CODE' ) {
106 1         1 $download_hook = $coderef;
107 1         1 return $pkg;
108             }
109              
110 0           my $err = "register download hook requires CODE reference parameter.";
111 0           require Enbld::Exception;
112 0           croak( Enbld::Exception->new( $err, $coderef ));
113             }
114              
115             1;