blib/lib/Data/Mirror.pm

filter: Statementstmt 92.8% 117 / 126 Branchbran 55.8% 19 / 34 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 96.9% 32 / 33 Podpod 70.0% 7 / 10 totaltotal 82.8% 179 / 216 CC 20 SCAR 12.1
CC
20
Cov
83%
mirror_file
10.2
mirror_xml
6.0
stale
4.2
CRAP 3.4
↑ Summary
1 package Data::Mirror;
2 # ABSTRACT: a simple way to efficiently retrieve data from the World Wide Web.
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Basename qw(dirname basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::XDG;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use List::Util qw(any max);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use LWP::Protocol::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Text::CSV_XS qw(csv);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use open qw(:std :utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use vars qw($VERSION %EXPORT_TAGS $TTL_SECONDS $UA $JSON $CSV $XDG);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 $VERSION = '0.09';
27  
28 1 $EXPORT_TAGS{'all'} = [qw(
29 mirror_str
30 mirror_csv
31 mirror_fh
32 mirror_file
33 mirror_json
34 mirror_xml
35 mirror_yaml
36 )];
37  
38 1 Exporter::export_ok_tags('all');
39  
40  
41 #
42 # global TTL, used if the $ttl method argument to the mirror_* methods isn't
43 # specified
44 #
45 1 $TTL_SECONDS = 300;
46  
47  
48 1 ▶$UA = LWP::UserAgent->new('agent' => sprintf(
Condition50%1/2
$Data::Mirror::VERSION || 'dev'
l!l
10
MC/DC0%0/1
$Data::Mirror::VERSION || 'dev'
$Data::Mirror::VERSION
Truth table50%1/2
A
A= $Data::Mirror::VERSION
Aresult
11
00
49 '%s/%s, LWP::UserAgent %s, Perl %s',
50 __PACKAGE__, $VERSION || 'dev',
51 $LWP::UserAgent::VERSION,
52 $^V,
53 ));
54  
55  
56 1 $JSON = JSON::XS->new->utf8;
57  
58  
59 1 $CSV = Text::CSV_XS->new ({
60 'binary' => 1,
61 });
62  
63  
64  
65 1 $XDG = File::XDG->new(path_class => 'File::Spec', name => __PACKAGE__);
66  
67  
68 6 ▶sub mirror_file {
Subroutine mirror_file: called CC=9 SCAR=23.2
Pod documented
69 6 my ($url, $ttl) = @_;
70  
71 6 ▶ $ttl = $TTL_SECONDS unless (defined($ttl));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ttl 6 0 6
72  
73 6 my $file = filename($url);
74  
75 6 ▶ return $file unless (stale($url));
Branch: 1 branch
BranchTrueFalseTotal
unless stale($url) 2 4 6
76  
77 4 my $dir = dirname($file);
78 4 ▶ croak(sprintf("Unable to create '%s'", $dir)) if (!-d $dir && !make_path($dir, { mode => 0700 }));
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir or make_path($dir, {"mode", 448}) 0 4 4
Condition66%2/3
-d $dir or make_path($dir, {"mode", 448})
l!l&&r!l&&!r
310
MC/DC0%0/2
-d $dir or make_path($dir, {"mode", 448})
-d $dir make_path($dir, {"mode", 448})
Truth table66%2/3
A or B
A= -d $dirB= make_path($dir, {"mode", 448})
ABresult
1X1
011
000
79  
80 #
81 # update the local file
82 #
83 4 my $result = $UA->mirror($url, $file);
84  
85 4 ▶ if (any { $_ == $result->code } (304, 200)) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ == $result->code; } , 304, 200)) 4 0 4
86 #
87 # if the response had the Expires: header, use that, otherwise use
88 # the later of the current mtime or now
89 #
90 4 my $expires;
91  
92 4 ▶ if ($result->header('expires')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->header('expires')) { } 4 0 4
93 4 $expires = str2time($result->header('expires'));
94  
95 } else {
96 0 $expires = max(stat($file)->mtime, time());
97  
98 }
99  
100 4 ▶ utime($expires, $expires, $file) if (-e $file);
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 4 0 4
101 }
102  
103 4 ▶ carp($result->status_line) if ($result->code >= 400);
Branch: 1 branch
BranchTrueFalseTotal
if $result->code >= 400 0 4 4
104  
105 4 return $file;
106 }
107  
108  
109 2 ▶sub mirror_str {
Subroutine mirror_str: called CC=2 SCAR=7.6
Pod documented
110  
111 2 my $file = mirror_file(@_);
112  
113 2 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 2 0 2
114 2 return encode('UTF-8', read_file($file, 'binmode' => ':utf8'));
115 }
116  
117 0 return undef;
118 }
119  
120  
121 2 ▶sub mirror_fh {
Subroutine mirror_fh: called CC=2 SCAR=7.2
Pod documented
122  
123 2 my $file = mirror_file(@_);
124  
125 2 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 2 0 2
126 2 my $fh = IO::File->new($file);
127  
128 2 $fh->binmode(':utf8');
129  
130 2 return $fh;
131 }
132  
133 0 return undef;
134 }
135  
136  
137 0 ▶sub mirror_xml {
Subroutine mirror_xml: not called CC=2 SCAR=17.9
Pod documented
138  
139 0 my $file = mirror_file(@_);
140  
141 0 ▶ return XML::LibXML->load_xml('location' => $file) if ($file);
Branch: 1 branch
BranchTrueFalseTotal
if $file 0 0 0
142  
143 0 return undef;
144 }
145  
146  
147 1 ▶sub mirror_json {
Subroutine mirror_json: called CC=2 SCAR=8.1
Pod documented
148  
149 1 my $str = mirror_str(@_);
150  
151 1 ▶ return $JSON->decode($str) if ($str);
Branch: 1 branch
BranchTrueFalseTotal
if $str 1 0 1
152  
153 0 return undef;
154 }
155  
156  
157 1 ▶sub mirror_yaml {
Subroutine mirror_yaml: called CC=2 SCAR=8.1
Pod documented
158  
159 1 my $file = mirror_file(@_);
160  
161 1 ▶ return YAML::XS::LoadFile($file) if ($file);
Branch: 1 branch
BranchTrueFalseTotal
if $file 1 0 1
162  
163 0 return undef;
164 }
165  
166  
167 1 ▶sub mirror_csv {
Subroutine mirror_csv: called CC=3 SCAR=11.1
Pod documented
168  
169 1 my $fh = mirror_fh(@_);
170  
171 1 ▶ if ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 1 0 1
172 1 my @rows;
173  
174 1 ▶ while (my $row = $CSV->getline($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $Data::Mirror::CSV->getline($fh)) 5 1 6
175 5 push(@rows, $row);
176 }
177  
178 1 $fh->close;
179  
180 1 return \@rows;
181 }
182  
183 0 return undef;
184 }
185  
186  
187 16 ▶sub filename {
Subroutine filename: called CC=2 SCAR=7.2
Pod undocumented
188 16 my $url = shift;
189  
190 #
191 # the local filename is based on the hash of the URL
192 #
193 16 ▶ return File::Spec->catfile(
Branch: 1 branch
BranchTrueFalseTotal
$url->isa('URI') ? : 0 16 16
194 $XDG->cache_home,
195 sha256_hex($url->isa('URI') ? $url->canonical->as_string : $url).q{.dat},
196 );
197 }
198  
199  
200 2 ▶sub mirrored { -e filename(@_) }
Subroutine mirrored: called CC=1 SCAR=0.0
Pod undocumented
201  
202  
203 7 ▶sub stale {
Subroutine stale: called CC=4 SCAR=14.5
Pod undocumented
204 7 my ($url, $ttl) = @_;
205  
206 7 my $file = filename($url);
207  
208 7 ▶ return 1 unless (-e $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 4 3 7
209  
210 3 ▶ return 1 unless stat($file)->mtime > time() - ($ttl || $TTL_SECONDS);
Branch: 1 branch
BranchTrueFalseTotal
unless &Data::Mirror::stat($file)->mtime > time - ($ttl || $Data::Mirror::TTL_SECONDS) 0 3 3
Condition33%1/3
$ttl || $Data::Mirror::TTL_SECONDS
l!l&&r!l&&!r
030
MC/DC0%0/2
$ttl || $Data::Mirror::TTL_SECONDS
$ttl $Data::Mirror::TTL_SECONDS
Truth table33%1/3
A || B
A= $ttlB= $Data::Mirror::TTL_SECONDS
ABresult
1X1
011
000
211  
212 3 return undef;
213 }
214  
215  
216 1 1;
217  
218 __END__
↑ Summary