| 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=5 SCAR=20.5
Pod
undocumented
| |||||||||||
| 25 | |||||||||||
| 26 | 54 | my ( undef, undef, $file ) = File::Spec->splitpath( $path ); | |||||||||
| 27 | |||||||||||
| 28 | 54 | ▶ | if ( -e $path ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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 | ▶ | initialize_ua() unless $ua; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | my $res = $ua->mirror( $url, $path ); | |||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | if ( ! $res->{success} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | die Enbld::Error->new( $res->{reason} ); | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 0 | return $path; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | sub download_archivefile { | ||||||||||
| 53 | 29 | ▶ | my ( $pkg, $url, $path ) = @_; | ||||||||
|
Subroutine
download_archivefile: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 54 | |||||||||||
| 55 | 29 | my $downloaded = Enbld::HTTP->download( $url, $path ); | |||||||||
| 56 | |||||||||||
| 57 | 29 | require Enbld::Archivefile; | |||||||||
| 58 | 29 | return Enbld::Archivefile->new( $downloaded ); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | sub get { | ||||||||||
| 62 | 35 | ▶ | my ( $pkg, $url ) = @_; | ||||||||
|
Subroutine
get: called CC=4 SCAR=22.9
Pod
undocumented
| |||||||||||
| 63 | |||||||||||
| 64 | 35 | ▶ | if ( $get_hook ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 35 | return $get_hook->( $pkg, $url ); | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | initialize_ua() unless $ua; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | my $res = $ua->get( $url ); | |||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | if ( ! $res->{success} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | die Enbld::Error->new( $res->{reason} ); | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 0 | return $res->{content}; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub get_html { | ||||||||||
| 79 | 35 | ▶ | my ( $pkg, $url ) = @_; | ||||||||
|
Subroutine
get_html: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 80 | |||||||||||
| 81 | 35 | my $content = Enbld::HTTP->get( $url ); | |||||||||
| 82 | |||||||||||
| 83 | 35 | require Enbld::HTML; | |||||||||
| 84 | 35 | return Enbld::HTML->new( $content ); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | # For debug methods. | ||||||||||
| 88 | |||||||||||
| 89 | sub register_get_hook { | ||||||||||
| 90 | 3 | ▶ | my ( $pkg, $coderef ) = @_; | ||||||||
|
Subroutine
register_get_hook: called CC=2 SCAR=8.5
Pod
undocumented
| |||||||||||
| 91 | |||||||||||
| 92 | 3 | ▶ | if ( ref( $coderef ) eq 'CODE' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 3 | $get_hook = $coderef; | |||||||||
| 94 | 3 | return $pkg; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 0 | my $err = "register get hook requires CODE reference parameter."; | |||||||||
| 98 | 0 | require Enbld::Exception; | |||||||||
| 99 | 0 | croak( Enbld::Exception->new( $err, $coderef )); | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | sub register_download_hook { | ||||||||||
| 103 | 1 | ▶ | my ( $pkg, $coderef ) = @_; | ||||||||
|
Subroutine
register_download_hook: called CC=2 SCAR=8.5
Pod
undocumented
| |||||||||||
| 104 | |||||||||||
| 105 | 1 | ▶ | if ( ref( $coderef ) eq 'CODE' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 1 | $download_hook = $coderef; | |||||||||
| 107 | 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 | 4 | 1; | |||||||||