| 1 | package App::Grok::Common; | ||||||||||
| 2 | 40 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 40 | $App::Grok::Common::AUTHORITY = 'cpan:HINRIK'; | |||||||||
| 4 | } | ||||||||||
| 5 | { | ||||||||||
| 6 | 40 | $App::Grok::Common::VERSION = '0.26'; | |||||||||
| 7 | } | ||||||||||
| 8 | |||||||||||
| 9 | 40 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 40 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 40 | ▶ | use File::HomeDir qw<my_data>; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 40 | ▶ | use File::Spec::Functions qw<catdir>; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 40 | ▶ | use base qw(Exporter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 40 | our @EXPORT_OK = qw(download data_dir); | |||||||||
| 16 | 40 | our %EXPORT_TAGS = ( ALL => [@EXPORT_OK] ); | |||||||||
| 17 | |||||||||||
| 18 | 40 | ▶ | sub data_dir { | ||||||||
|
Subroutine
data_dir: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||
| 19 | 40 | my $data_dir = catdir(my_data(), '.grok'); | |||||||||
| 20 | 40 | ▶ | if (!-d $data_dir) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 1 | ▶ | mkdir $data_dir or die "Can't create $data_dir: $!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 40 | my $res_dir = catdir($data_dir, 'resources'); | |||||||||
| 25 | 40 | ▶ | if (!-d $res_dir) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 1 | ▶ | mkdir $res_dir or die "Can't create $res_dir: $!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 40 | return $data_dir; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | sub download { | ||||||||
|
Subroutine
download: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 33 | 0 | my ($title, $url) = @_; | |||||||||
| 34 | |||||||||||
| 35 | 0 | require LWP::UserAgent; | |||||||||
| 36 | 0 | my $ua = LWP::UserAgent->new; | |||||||||
| 37 | 0 | $ua->timeout(10); | |||||||||
| 38 | 0 | $ua->env_proxy; | |||||||||
| 39 | |||||||||||
| 40 | 0 | eval 'require Term::ProgressBar'; | |||||||||
| 41 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | print $title, "\n"; | |||||||||
| 43 | 0 | my $response = $ua->get($url); | |||||||||
| 44 | 0 | ▶ | if ($response->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | return $response->decoded_content; | |||||||||
| 46 | } | ||||||||||
| 47 | else { | ||||||||||
| 48 | 0 | die 'Download failed: '.$response->status_line."\n"; | |||||||||
| 49 | } | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | 0 | my $bar = Term::ProgressBar->new({ | |||||||||
| 53 | name => $title, | ||||||||||
| 54 | count => 1024, | ||||||||||
| 55 | ETA => 'linear', | ||||||||||
| 56 | }); | ||||||||||
| 57 | |||||||||||
| 58 | 0 | my $content; | |||||||||
| 59 | 0 | my $output = 0; | |||||||||
| 60 | 0 | my $target_is_set = 0; | |||||||||
| 61 | 0 | my $next_so_far = 0; | |||||||||
| 62 | $ua->get( | ||||||||||
| 63 | $url, | ||||||||||
| 64 | 0 | ▶ | ":content_cb" => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||
| 65 | 0 | my ($chunk, $response, $proto) = @_; | |||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | if (!$target_is_set) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | ▶ | if (my $cl = $response->content_length) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | $bar->target($cl); | |||||||||
| 70 | 0 | $target_is_set = 1; | |||||||||
| 71 | } | ||||||||||
| 72 | else { | ||||||||||
| 73 | 0 | $bar->target($output + 2 * length $chunk); | |||||||||
| 74 | } | ||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 0 | $output += length $chunk; | |||||||||
| 78 | 0 | $content .= $chunk; | |||||||||
| 79 | |||||||||||
| 80 | 0 | ▶ | if ($output >= $next_so_far) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | $next_so_far = $bar->update($output); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | #$bar->target($output); | ||||||||||
| 85 | #$bar->update($output); | ||||||||||
| 86 | }, | ||||||||||
| 87 | 0 | ); | |||||||||
| 88 | |||||||||||
| 89 | 0 | return $content; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 40 | 1; | |||||||||
| 93 | |||||||||||
| 94 | =encoding utf8 | ||||||||||
| 95 | |||||||||||
| 96 | =head1 NAME | ||||||||||
| 97 | |||||||||||
| 98 | App::Grok::Common - Common functions used in grok | ||||||||||
| 99 | |||||||||||
| 100 | =head1 SYNOPSIS | ||||||||||
| 101 | |||||||||||
| 102 | use strict; | ||||||||||
| 103 | use warnings; | ||||||||||
| 104 | use App::Grok::Common qw<:ALL>; | ||||||||||
| 105 | |||||||||||
| 106 | # download a file, with a progress bar | ||||||||||
| 107 | my $url = 'http://foo.bar/baz'; | ||||||||||
| 108 | my $content = download('My file', $url); | ||||||||||
| 109 | |||||||||||
| 110 | =head1 DESCRIPTION | ||||||||||
| 111 | |||||||||||
| 112 | This module provides common utility functions used in App::Grok. | ||||||||||
| 113 | |||||||||||
| 114 | =head1 FUNCTIONS | ||||||||||
| 115 | |||||||||||
| 116 | =head2 C<download> | ||||||||||
| 117 | |||||||||||
| 118 | Downloads a file from the web and returns the contents. Prints a progress bar | ||||||||||
| 119 | (if L<Term::ProgressBar|Term::ProgressBar> is installed) as while doing so. | ||||||||||
| 120 | It takes two arguments, a title string and the url. Returns the downloaded | ||||||||||
| 121 | content. | ||||||||||
| 122 | |||||||||||
| 123 | =head2 C<data_dir> | ||||||||||
| 124 | |||||||||||
| 125 | Creates (if necessary) and then returns the name of the directory where grok | ||||||||||
| 126 | stores its data (e.g. F<~/.grok>). | ||||||||||
| 127 | |||||||||||
| 128 | =cut | ||||||||||