Catmandu/Importer.pm

filter: Statementstmt 96.6% 145 / 150 Branchbran 87.5% 42 / 48 Conditioncond 90.9% 20 / 22 MC/DCmcdc 84.6% 11 / 13 Subroutinesub 91.3% 21 / 23 Podpod 0.0% 0 / 2 totaltotal 92.6% 239 / 258 CC 36 SCAR 18.2
CC
36
Cov
94%
_build_file
11.0
_build_fh
10.1
_http_request
9.0
CRAP 6.2
« Catmandu/Path.pm ↑ Summary Catmandu/Emit.pm »
1 package Catmandu::Importer;
2  
3 35 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 35 our $VERSION = '1.2025';
6  
7 35 ▶use Catmandu::Util qw(io is_value is_string is_array_ref is_hash_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use Catmandu::Util::Path qw(as_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use URI::Template ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 35 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 35 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 35 with 'Catmandu::Logger';
17 35 with 'Catmandu::Iterable';
18 35 with 'Catmandu::IterableOnce';
19 35 with 'Catmandu::Fixable';
20 35 with 'Catmandu::Serializer';
21  
22 83 ▶around generator => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
23 83 my ($orig, $self) = @_;
24  
25 83 my $generator = $orig->($self);
26  
27 83 ▶ if (my $fixer = $self->_fixer) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fixer = $self->_fixer) 2 81 83
28 2 $generator = $fixer->fix($generator);
29 }
30  
31 83 ▶ if (defined(my $path = $self->data_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $path = $self->data_path)) 2 81 83
32 2 my $getter = as_path($path)->getter;
33 10 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
34 10 state @buf;
35 10 while (1) {
36 14 ▶ return shift @buf if @buf;
Branch: 1 branch
BranchTrueFalseTotal
if @buf 8 6 14
37 6 ▶ @buf = @{$getter->($generator->() // return)};
Condition100%2/2
&$generator() // (return)
l!l
42
MC/DC100%1/1
&$generator() // (return)
&$generator()
Truth table100%2/2
A
A= &$generator()
Aresult
11
00
38 4 next;
39 }
40 2 };
41 }
42  
43 81 $generator;
44 35 };
45  
46 35 has file => (is => 'lazy', init_arg => undef);
47 35 has _file_template =>
48 (is => 'ro', predicate => 'has_file', init_arg => 'file');
49 35 has variables => (is => 'ro', predicate => 1);
50 35 has fh => (is => 'ro', lazy => 1, builder => 1);
51 35 has encoding => (is => 'ro', builder => 1);
52 35 has data_path => (is => 'ro');
53 35 has user_agent => (is => 'ro');
54 35 has http_method => (is => 'lazy');
55 35 has http_headers => (is => 'lazy');
56 35 has http_agent => (is => 'ro', predicate => 1);
57 35 has http_max_redirect => (is => 'ro', predicate => 1);
58 108 ▶has http_timeout => (is => 'ro', default => sub {180}); # LWP default
Subroutine __ANON__: called CC=1 SCAR=0.0
59 108 ▶has http_verify_hostname => (is => 'ro', default => sub {1});
Subroutine __ANON__: called CC=1 SCAR=0.0
60 35 has http_retry => (is => 'ro', predicate => 1);
61 35 has http_timing => (is => 'ro', predicate => 1);
62 35 has http_body => (is => 'ro', predicate => 1);
63 35 has _http_client => (
64 is => 'ro',
65 lazy => 1,
66 builder => '_build_http_client',
67 init_arg => 'user_agent'
68 );
69 35 has _http_timing_tries => (is => 'lazy');
70 35 has ignore_404 => (is => 'ro');
71  
72 100 ▶sub _build_encoding {
Subroutine _build_encoding: called CC=1 SCAR=0.0
73 100 ':utf8';
74 }
75  
76 67 ▶sub _build_file {
Subroutine _build_file: called CC=11 SCAR=24.0
77 67 my ($self) = @_;
78 67 ▶ return \*STDIN unless $self->has_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_file 0 67 67
79 67 my $file = $self->_file_template;
80 67 ▶ if (is_string($file) && $self->has_variables) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_string($file) and $self->has_variables) 6 61 67
Condition100%3/3
is_string($file) and $self->has_variables
!ll&&!rl&&r
32296
MC/DC100%2/2
is_string($file) and $self->has_variables
is_string($file) $self->has_variables
Truth table100%3/3
A and B
A= is_string($file)B= $self->has_variables
ABresult
0X0
100
111
81 6 my $template = URI::Template->new($file);
82 6 my $vars = $self->variables;
83 6 ▶ if (is_value($vars)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_value($vars)) 3 3 6
84 3 $vars = [split ',', $vars];
85 }
86 6 ▶ if (is_array_ref($vars)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array_ref($vars)) 4 2 6
87 4 my @keys = $template->variables;
88 4 my @vals = @$vars;
89 4 $vars = {};
90 4 ▶ $vars->{shift @keys} = shift @vals while @keys && @vals;
Branch: 1 branch
BranchTrueFalseTotal
@keys and @vals and do { $$vars{shift @keys} = shift @vals } 6 4 10
Condition66%2/3
@keys and @vals
!ll&&!rl&&r
406
MC/DC50%1/2
@keys and @vals
@keys @vals
Truth table66%2/3
A and B
A= @keysB= @vals
ABresult
0X0
100
111
91 }
92 6 $file = $template->process_to_string(%$vars);
93 }
94 67 ▶ if (is_string($file) && $file !~ m!^https?://! && !-r $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_string($file) and not $file =~ m[^https?://] and not -r $file) 1 66 67
Condition100%6/6
is_string($file) and not $file =~ m[^https?://] and not -r $file
!ll&&!rl&&r
45211
is_string($file) and not $file =~ m[^https?://]
!ll&&!rl&&r
321322
MC/DC100%3/3
is_string($file) and not $file =~ m[^https?://] and not -r $file
is_string($file) not $file =~ m[^https?://] not -r $file
Truth table100%4/4
A and not(B) and not(C)
A= is_string($file)B= not $file =~ m[^https?://]C= not -r $file
ABCresult
0XX0
10X0
1100
1111
95 1 Catmandu::BadArg->throw("file '$file' doesn't exist");
96 }
97 66 $file;
98 }
99  
100 64 ▶sub _build_fh {
Subroutine _build_fh: called CC=10 SCAR=23.2
101 64 my ($self) = @_;
102  
103 64 my $file = $self->file;
104  
105 # get remote content
106 63 ▶ if (is_string($file) && $file =~ m!^https?://!) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_string($file) and $file =~ m[^https?://]) 13 50 63
Condition100%3/3
is_string($file) and $file =~ m[^https?://]
!ll&&!rl&&r
292113
MC/DC100%2/2
is_string($file) and $file =~ m[^https?://]
is_string($file) $file =~ m[^https?://]
Truth table100%3/3
A and B
A= is_string($file)B= $file =~ m[^https?://]
ABresult
0X0
100
111
107 13 my $body;
108 13 ▶ if ($self->has_http_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_http_body) 3 10 13
109 3 $body = $self->http_body;
110  
111 3 ▶ if (ref $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body) 0 3 3
112 0 $body = $self->serialize($body);
113 }
114  
115 3 ▶ if ($self->has_variables) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_variables) 3 0 3
116 3 my $vars = $self->variables;
117 3 ▶ if (is_hash_ref($vars)) { # named variables
Branch: 1 branch
BranchTrueFalseTotal
if (is_hash_ref($vars)) { } 1 2 3
118 1 for my $key (keys %$vars) {
119 1 my $var = $vars->{$key};
120 1 $body =~ s/{$key}/$var/;
121 }
122 }
123 else { # positional variables
124 2 ▶ if (is_value($vars)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_value($vars)) 2 0 2
125 2 $vars = [split ',', $vars];
126 }
127 2 for my $var (@$vars) {
128 6 $body =~ s/{\w+}/$var/;
129 }
130 }
131 }
132 }
133  
134 13 my $content = $self->_http_request(
135 $self->http_method, $file, $self->http_headers,
136 $body, $self->_http_timing_tries,
137 );
138  
139 8 return io(\$content, mode => 'r', binmode => $_[0]->encoding);
140 }
141  
142 50 io($file, mode => 'r', binmode => $_[0]->encoding);
143 }
144  
145 13 ▶sub _build_http_headers {
Subroutine _build_http_headers: called CC=1 SCAR=0.0
146 13 [];
147 }
148  
149 10 ▶sub _build_http_method {
Subroutine _build_http_method: called CC=1 SCAR=0.0
150 10 'GET';
151 }
152  
153 13 ▶sub _build__http_timing_tries {
Subroutine _build__http_timing_tries: called CC=3 SCAR=11.0
154 13 my ($self) = @_;
155  
156 13 ▶ if ($self->has_http_timing) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_http_timing) { } 2 11 13
elsif ($self->has_http_retry) { } 2 9 11
157 2 my @timing_tries = $self->http_timing =~ /(\d+(?:\.\d+)*)/g;
158 2 return \@timing_tries;
159 }
160 elsif ($self->has_http_retry) {
161 2 my @timing_tries = (1) x $self->http_retry;
162 2 return \@timing_tries;
163 }
164 9 return;
165 }
166  
167 2 ▶sub _build_http_client {
Subroutine _build_http_client: called CC=3 SCAR=11.1
168 2 my ($self) = @_;
169 2 my $ua = LWP::UserAgent->new;
170 2 $ua->timeout($self->http_timeout);
171 2 ▶ $ua->agent($self->http_agent) if $self->has_http_agent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_http_agent 0 2 2
172 2 ▶ $ua->max_redirect($self->http_max_redirect)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_http_max_redirect 0 2 2
173 if $self->has_http_max_redirect;
174 2 $ua->ssl_opts(verify_hostname => $self->http_verify_hostname);
175 2 $ua->protocols_allowed([qw(http https)]);
176 2 $ua->env_proxy;
177 2 $ua;
178 }
179  
180 13 ▶sub _http_request {
Subroutine _http_request: called CC=9 SCAR=22.0
181 13 my ($self, $method, $url, $headers, $body, $timing_tries) = @_;
182  
183 13 my $client = $self->_http_client;
184  
185 13 ▶ my $req = HTTP::Request->new($method, $url, $headers || []);
Condition50%1/2
$headers || []
l!l
130
MC/DC0%0/1
$headers || []
$headers
Truth table50%1/2
A
A= $headers
Aresult
11
00
186 13 ▶ $req->content($body) if defined $body;
Branch: 1 branch
BranchTrueFalseTotal
if defined $body 3 10 13
187  
188 13 my $res = $client->request($req);
189  
190 13 ▶ if ($res->code =~ /^408|500|502|503|504$/ && $timing_tries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code =~ /^408|500|502|503|504$/ and $timing_tries) 4 9 13
Condition100%3/3
$res->code =~ /^408|500|502|503|504$/ and $timing_tries
!ll&&!rl&&r
814
MC/DC100%2/2
$res->code =~ /^408|500|502|503|504$/ and $timing_tries
$res->code =~ /^408|500|502|503|504$/ $timing_tries
Truth table100%3/3
A and B
A= $res->code =~ /^408|500|502|503|504$/B= $timing_tries
ABresult
0X0
100
111
191 4 my @tries = @$timing_tries;
192 4 ▶ while (my $sleep = shift @tries) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sleep = shift @tries) 6 2 8
193 6 sleep $sleep;
194 6 $res = $client->request($req->clone);
195 6 ▶ last if $res->code !~ /^408|500|502|503|504$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code =~ /^408|500|502|503|504$/ 2 4 6
196 }
197 }
198  
199 13 my $res_body = $res->decoded_content;
200  
201 13 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 5 8 13
202 5 my $res_headers = [];
203 5 for my $header ($res->header_field_names) {
204 18 my $val = $res->header($header);
205 18 push @$res_headers, $header, $val;
206 }
207 Catmandu::HTTPError->throw(
208 {
209 5 code => $res->code,
210 message => $res->status_line,
211 url => $url,
212 method => $method,
213 request_headers => $headers,
214 request_body => $body,
215 response_headers => $res_headers,
216 response_body => $res_body,
217 }
218 );
219 }
220  
221 8 $res_body;
222 }
223  
224 0 ▶sub readline {
Subroutine readline: not called CC=1 SCAR=6.9
Pod undocumented
225 0 warnings::warnif("deprecated",
226 "readline is deprecated, fh->getline instead");
227 0 $_[0]->fh->getline;
228 }
229  
230 0 ▶sub readall {
Subroutine readall: not called CC=1 SCAR=6.9
Pod undocumented
231 0 warnings::warnif("deprecated",
232 "readall is deprecated, join('',fh->getlines) instead");
233 0 join '', $_[0]->fh->getlines;
234 }
235  
236 35 1;
237  
238 __END__
« Catmandu/Path.pm ↑ Summary Catmandu/Emit.pm »