blib/lib/Gcis/Client.pm

filter: Statementstmt 17.7% 37 / 208 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 31.0% 9 / 29 Podpod 56.2% 9 / 16 totaltotal 13.3% 55 / 412 CC 67 SCAR 37.3
CC
67
Cov
11%
get
110.0
tables
72.0
figures
72.0
CRAP 41.5
↑ Summary
1 package Gcis::Client;
2 3 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use YAML::XS qw/LoadFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Path::Class qw/file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Time::HiRes qw/sleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.12';
13  
14 3 has url => 'http://localhost:3000';
15 3 has 'key';
16 3 has 'error';
17 3 has 'delay' => $ENV{GCIS_API_DELAY};
18 0 ▶has ua => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
19 0 my $c = shift;
20 0 state $ua;
21 0 ▶ return $ua if $ua;
Branch: 1 branch
BranchTrueFalseTotal
if $ua 0 0 0
22 0 $ua = Mojo::UserAgent->new();
23 $ua->on(
24 0 ▶ start => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
25 0 my ($ua, $tx) = @_;
26 0 ▶ $tx->req->headers->header($c->auth_hdr) if $c->auth_hdr;
Branch: 1 branch
BranchTrueFalseTotal
if $c->auth_hdr 0 0 0
27 0 $tx->req->headers->header(Accept => $c->accept);
28 }
29 0 );
30 0 $ua->max_redirects(5);
31 0 $ua;
32 3 };
33 0 ▶has logger => sub { state $log ||= Mojo::Log->new(); };
Subroutine __ANON__: not called CC=2 SCAR=14.0
Condition0%0/3
state $log ||= "Mojo::Log"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
state $log ||= "Mojo::Log"->new
state $log "Mojo::Log"->new
Truth table0%0/3
A ||= B
A= state $logB= "Mojo::Log"->new
ABresult
1X1
011
000
34 0 ▶has json => sub { state $json ||= JSON::XS->new(); };
Subroutine __ANON__: not called CC=2 SCAR=14.0
Condition0%0/3
state $json ||= "JSON::XS"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
state $json ||= "JSON::XS"->new
state $json "JSON::XS"->new
Truth table0%0/3
A ||= B
A= state $jsonB= "JSON::XS"->new
ABresult
1X1
011
000
35 3 has accept => "application/json";
36 3 has 'tx';
37  
38 0 ▶sub auth_hdr { ($a = shift->key) ? ("Authorization" => "Basic $a") : () }
Subroutine auth_hdr: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
($a = (shift())->key) ? : 0 0 0
39  
40 0 ▶sub get {
Subroutine get: not called CC=10 SCAR=47.0
Pod documented
41 0 my $s = shift;
42 0 ▶ my $path = shift or die "missing path";
Branch: 1 branch
BranchTrueFalseTotal
my $path = shift() or die "missing path" 0 0 0
43 0 my $params = shift;
44 0 ▶ if (defined($s->delay)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->delay) 0 0 0
45 0 $s->logger->debug("sleeping for ".$s->delay.'s');
46 0 sleep $s->delay;
47 }
48 0 my $url;
49 0 ▶ if ($params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params) { } 0 0 0
50 0 $url = Mojo::URL->new($s->url);
51 0 $url->path($path);
52 0 $url->query(%$params);
53 } else {
54 0 $url = Mojo::URL->new($s->url.$path);
55 }
56 0 my $tx = $s->ua->get($url);
57 0 $s->tx($tx);
58 0 my $res = $tx->success;
59 0 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
60 0 ▶ if ($tx->res->code && $tx->res->code == 404) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->res->code and $tx->res->code == 404) 0 0 0
Condition0%0/3
$tx->res->code and $tx->res->code == 404
!ll&&!rl&&r
000
MC/DC0%0/2
$tx->res->code and $tx->res->code == 404
$tx->res->code $tx->res->code == 404
Truth table0%0/3
A and B
A= $tx->res->codeB= $tx->res->code == 404
ABresult
0X0
100
111
61 # $s->logger->debug("not found : $path");
62 0 $s->error("not found : $path");
63 0 return;
64 }
65 0 $s->error($tx->error->{message});
66 0 $s->logger->error($tx->error->{message});
67 0 return;
68 };
69 0 ▶ my $json = $res->json or do {
Branch: 1 branch
BranchTrueFalseTotal
my $json = $res->json or do { $s->logger->debug("no json from $path : " . $res->to_string); $s->error("No JSON returned from $path : " . $res->to_string); return } 0 0 0
70 0 $s->logger->debug("no json from $path : ".$res->to_string);
71 0 $s->error("No JSON returned from $path : ".$res->to_string);
72 0 return;
73 };
74 0 ▶ return wantarray && ref($json) eq 'ARRAY' ? @$json : $json;
Branch: 1 branch
BranchTrueFalseTotal
wantarray && ref $json eq 'ARRAY' ? : 0 0 0
Condition0%0/3
wantarray and ref $json eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
wantarray and ref $json eq "ARRAY"
wantarray ref $json eq "ARRAY"
Truth table0%0/3
A and B
A= wantarrayB= ref $json eq "ARRAY"
ABresult
0X0
100
111
75 }
76  
77 0 ▶sub post {
Subroutine post: not called CC=5 SCAR=34.0
Pod undocumented
78 0 my $s = shift;
79 0 ▶ my $path = shift or Carp::confess "missing path";
Branch: 1 branch
BranchTrueFalseTotal
my $path = shift() or Carp::confess("missing path") 0 0 0
80 0 my $data = shift;
81 0 my $tx = $s->ua->post($s->url."$path" => json => $data );
82 0 $s->tx($tx);
83 0 ▶ my $res = $tx->success or do {
Branch: 1 branch
BranchTrueFalseTotal
my $res = $tx->success or do { $s->logger->error("got an error $path : " . Dumper($tx->error->{"message"}) . $tx->res->body); return } 0 0 0
84 0 $s->logger->error("got an error $path : ".Dumper($tx->error->{message}).$tx->res->body);
85 0 return;
86 };
87 0 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
88 0 ▶ my $json = $res->json or return $res->body;
Branch: 1 branch
BranchTrueFalseTotal
my $json = $res->json or return $res->body 0 0 0
89 0 return $res->json;
90 }
91  
92 0 ▶sub delete {
Subroutine delete: not called CC=5 SCAR=34.0
Pod documented
93 0 my $s = shift;
94 0 my $path = shift;
95 0 my $payload = shift;
96 0 ▶ my $tx = $s->ua->delete($s->url."$path", ( $payload ? (json => $payload) : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$payload ? : 0 0 0
97 0 $s->tx($tx);
98 0 my $res = $tx->success;
99 0 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
100 0 ▶ if ($tx->res->code && $tx->res->code == 404) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->res->code and $tx->res->code == 404) 0 0 0
Condition0%0/3
$tx->res->code and $tx->res->code == 404
!ll&&!rl&&r
000
MC/DC0%0/2
$tx->res->code and $tx->res->code == 404
$tx->res->code $tx->res->code == 404
Truth table0%0/3
A and B
A= $tx->res->codeB= $tx->res->code == 404
ABresult
0X0
100
111
101 0 $s->error("not found : $path");
102 0 return;
103 }
104 0 $s->error($tx->error->{message});
105 0 $s->logger->error($tx->error->{message});
106 0 return;
107 };
108 0 return $res->body;
109 }
110  
111 0 ▶sub put_file {
Subroutine put_file: not called CC=4 SCAR=30.0
Pod documented
112 0 my $s = shift;
113 0 my $path = shift;
114 0 my $file = shift;
115 0 my $data = file($file)->slurp;
116 0 my $tx = $s->ua->put($s->url."$path" => $data );
117 0 $s->tx($tx);
118 0 ▶ my $res = $tx->success or do {
Branch: 1 branch
BranchTrueFalseTotal
my $res = $tx->success or do { $s->error(join("\n", $tx->error->{"message"}, $tx->res->body)); $s->logger->error($path . ' : ' . $tx->error->{"message"}); $s->logger->error($tx->res->body); return } 0 0 0
119 0 $s->error(join "\n",$tx->error->{message},$tx->res->body);
120 0 $s->logger->error($path." : ".$tx->error->{message});
121 0 $s->logger->error($tx->res->body);
122 0 return;
123 };
124 0 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
125 0 ▶ my $json = $res->json or return $res->body;
Branch: 1 branch
BranchTrueFalseTotal
my $json = $res->json or return $res->body 0 0 0
126 0 return $res->json;
127 }
128  
129 0 ▶sub add_file_url {
Subroutine add_file_url: not called CC=1 SCAR=6.9
Pod documented
130 0 my $s = shift;
131 0 my $gcid = shift;
132 0 my $args = shift;
133 0 my $path = $gcid;
134 0 $path =~ s[/([^/]+)$][/files/$1];
135 0 $s->post($path => $args);
136 }
137  
138 0 ▶sub post_quiet {
Subroutine post_quiet: not called CC=5 SCAR=34.0
Pod undocumented
139 0 my $s = shift;
140 0 my $path = shift;
141 0 my $data = shift;
142 0 my $tx = $s->ua->post($s->url."$path" => json => $data );
143 0 $s->tx($tx);
144 0 ▶ my $res = $tx->success or do {
Branch: 1 branch
BranchTrueFalseTotal
my $res = $tx->success or do { $s->logger->error("$path : " . $tx->error . $tx->res->body) unless $tx->res->code == 404; return } 0 0 0
145 0 ▶ $s->logger->error("$path : ".$tx->error.$tx->res->body) unless $tx->res->code == 404;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx->res->code == 404 0 0 0
146 0 return;
147 };
148 0 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
149 0 ▶ my $json = $res->json or return $res->body;
Branch: 1 branch
BranchTrueFalseTotal
my $json = $res->json or return $res->body 0 0 0
150 0 return $res->json;
151 }
152  
153 0 ▶sub find_credentials {
Subroutine find_credentials: not called CC=6 SCAR=37.4
Pod documented
154 0 my $s = shift;
155 0 my $home = $ENV{HOME};
156 0 ▶ die "need url to find credentials" unless $s->url;
Branch: 1 branch
BranchTrueFalseTotal
unless $s->url 0 0 0
157 0 my $conf_file = "$home/etc/Gcis.conf";
158 0 ▶ -e $conf_file or die "Missing $conf_file";
Branch: 1 branch
BranchTrueFalseTotal
-e $conf_file or die "Missing $conf_file" 0 0 0
159 0 my $conf = LoadFile($conf_file);
160 0 my @found = grep { $_->{url} eq $s->url } @$conf;
161 0 ▶ die "Multiple matches for ".$s->url." in $conf_file." if @found > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @found > 1 0 0 0
162 0 ▶ die "No matches for ".$s->url." in $conf_file." if @found < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @found < 1 0 0 0
163 0 ▶ my $key = $found[0]->{key} or die "no key for ".$s->url." in $conf_file";
Branch: 1 branch
BranchTrueFalseTotal
my $key = $found[0]{"key"} or die 'no key for ' . $s->url . " in $conf_file" 0 0 0
164 0 $s->logger->info("Loaded configuration from $conf_file");
165 0 $s->key($key);
166 0 return $s;
167 }
168  
169 0 ▶sub login {
Subroutine login: not called CC=3 SCAR=24.8
Pod documented
170 0 my $c = shift;
171 0 ▶ my $got = $c->get('/login') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $got = $c->get("/login") or return 0 0 0
172 0 ▶ $c->get('/login')->{login} eq 'ok' or return;
Branch: 1 branch
BranchTrueFalseTotal
$c->get("/login")->{"login"} eq "ok" or return 0 0 0
173 0 return $c;
174 }
175  
176 0 ▶sub use_env {
Subroutine use_env: not called CC=2 SCAR=17.9
Pod documented
177 0 my $c = shift;
178 0 ▶ my $url = $ENV{GCIS_API_URL} or die "please set GCIS_API_URL";
Branch: 1 branch
BranchTrueFalseTotal
my $url = $ENV{"GCIS_API_URL"} or die "please set GCIS_API_URL" 0 0 0
179 0 $c->url($url);
180 0 return $c;
181 }
182  
183 0 ▶sub get_chapter_map {
Subroutine get_chapter_map: not called CC=5 SCAR=34.0
Pod documented
184 0 my $c = shift;
185 0 ▶ my $report = shift or die "no report";
Branch: 1 branch
BranchTrueFalseTotal
my $report = shift() or die "no report" 0 0 0
186 0 ▶ my $all = $c->get("/report/$report/chapter?all=1") or die $c->url.' : '.$c->error;
Branch: 1 branch
BranchTrueFalseTotal
my $all = $c->get("/report/$report/chapter?all=1") or die $c->url . ' : ' . $c->error 0 0 0
187 0 ▶ my %map = map { $_->{number} // $_->{identifier} => $_->{identifier} } @$all;
Condition0%0/3
$_->{"number"} // $_->{"identifier"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{"number"} // $_->{"identifier"}
$_->{"number"} $_->{"identifier"}
Truth table0%0/3
A // B
A= $_->{"number"}B= $_->{"identifier"}
ABresult
1X1
011
000
188 0 ▶ return wantarray ? %map : \%map;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
189 }
190  
191 0 ▶sub tables {
Subroutine tables: not called CC=8 SCAR=42.8
Pod undocumented
192 0 my $c = shift;
193 0 my %a = @_;
194 0 ▶ my $report = $a{report} or die "no report";
Branch: 1 branch
BranchTrueFalseTotal
my $report = $a{"report"} or die "no report" 0 0 0
195 0 ▶ if (my $chapter_number = $a{chapter_number}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chapter_number = $a{"chapter_number"}) 0 0 0
196 0 ▶ $c->{_chapter_map}->{$report} //= $c->get_chapter_map($report);
Condition0%0/3
$c->{"_chapter_map"}{$report} //= $c->get_chapter_map($report)
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{"_chapter_map"}{$report} //= $c->get_chapter_map($report)
$c->{"_chapter_map"}{$report} $c->get_chapter_map($report)
Truth table0%0/3
A //= B
A= $c->{"_chapter_map"}{$report}B= $c->get_chapter_map($report)
ABresult
1X1
011
000
197 0 $a{chapter} = $c->{_chapter_map}->{$report}->{$chapter_number};
198 }
199 0 my $tables;
200 0 ▶ if (my $chapter = $a{chapter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chapter = $a{'chapter'}) { } 0 0 0
201 0 ▶ $tables = $c->get("/report/$report/chapter/$chapter/table?all=1") or die $c->error;
Branch: 1 branch
BranchTrueFalseTotal
$tables = $c->get("/report/$report/chapter/$chapter/table?all=1") or die $c->error 0 0 0
202 } else {
203 0 ▶ $tables = $c->get("/report/$report/table?all=1") or die $c->error;
Branch: 1 branch
BranchTrueFalseTotal
$tables = $c->get("/report/$report/table?all=1") or die $c->error 0 0 0
204 }
205 0 ▶ return wantarray ? @$tables : $tables;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
206 }
207  
208 0 ▶sub figures {
Subroutine figures: not called CC=8 SCAR=42.8
Pod undocumented
209 0 my $c = shift;
210 0 my %a = @_;
211 0 ▶ my $report = $a{report} or die "no report";
Branch: 1 branch
BranchTrueFalseTotal
my $report = $a{"report"} or die "no report" 0 0 0
212 0 ▶ if (my $chapter_number = $a{chapter_number}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chapter_number = $a{"chapter_number"}) 0 0 0
213 0 ▶ $c->{_chapter_map}->{$report} //= $c->get_chapter_map($report);
Condition0%0/3
$c->{"_chapter_map"}{$report} //= $c->get_chapter_map($report)
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{"_chapter_map"}{$report} //= $c->get_chapter_map($report)
$c->{"_chapter_map"}{$report} $c->get_chapter_map($report)
Truth table0%0/3
A //= B
A= $c->{"_chapter_map"}{$report}B= $c->get_chapter_map($report)
ABresult
1X1
011
000
214 0 $a{chapter} = $c->{_chapter_map}->{$report}->{$chapter_number};
215 }
216 0 my $figures;
217 0 ▶ if (my $chapter = $a{chapter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chapter = $a{'chapter'}) { } 0 0 0
218 0 ▶ $figures = $c->get("/report/$report/chapter/$chapter/figure?all=1") or die $c->error;
Branch: 1 branch
BranchTrueFalseTotal
$figures = $c->get("/report/$report/chapter/$chapter/figure?all=1") or die $c->error 0 0 0
219 } else {
220 0 ▶ $figures = $c->get("/report/$report/figure?all=1") or die $c->error;
Branch: 1 branch
BranchTrueFalseTotal
$figures = $c->get("/report/$report/figure?all=1") or die $c->error 0 0 0
221 }
222 0 ▶ return wantarray ? @$figures : $figures;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
223 }
224  
225 0 ▶sub findings {
Subroutine findings: not called CC=8 SCAR=42.8
Pod undocumented
226 0 my $c = shift;
227 0 my %a = @_;
228 0 ▶ my $report = $a{report} or die "no report";
Branch: 1 branch
BranchTrueFalseTotal
my $report = $a{"report"} or die "no report" 0 0 0
229 0 ▶ if (my $chapter_number = $a{chapter_number}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chapter_number = $a{"chapter_number"}) 0 0 0
230 0 ▶ $c->{_chapter_map}->{$report} //= $c->get_chapter_map($report);
Condition0%0/3
$c->{"_chapter_map"}{$report} //= $c->get_chapter_map($report)
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{"_chapter_map"}{$report} //= $c->get_chapter_map($report)
$c->{"_chapter_map"}{$report} $c->get_chapter_map($report)
Truth table0%0/3
A //= B
A= $c->{"_chapter_map"}{$report}B= $c->get_chapter_map($report)
ABresult
1X1
011
000
231 0 $a{chapter} = $c->{_chapter_map}->{$report}->{$chapter_number};
232 }
233 0 my $findings;
234 0 ▶ if (my $chapter = $a{chapter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chapter = $a{'chapter'}) { } 0 0 0
235 0 ▶ $findings = $c->get("/report/$report/chapter/$chapter/finding?all=1") or die $c->error;
Branch: 1 branch
BranchTrueFalseTotal
$findings = $c->get("/report/$report/chapter/$chapter/finding?all=1") or die $c->error 0 0 0
236 } else {
237 0 ▶ $findings = $c->get("/report/$report/finding?all=1") or die $c->error;
Branch: 1 branch
BranchTrueFalseTotal
$findings = $c->get("/report/$report/finding?all=1") or die $c->error 0 0 0
238 }
239 0 ▶ return wantarray ? @$findings : $findings;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
240 }
241  
242 0 ▶sub get_form {
Subroutine get_form: not called CC=3 SCAR=24.8
Pod undocumented
243 0 my $c = shift;
244 0 my $obj = shift;
245 0 ▶ my $uri = $obj->{uri} or die "no uri in ".Dumper($obj);
Branch: 1 branch
BranchTrueFalseTotal
my $uri = $obj->{"uri"} or die 'no uri in ' . Dumper($obj) 0 0 0
246 0 ▶ if ($uri =~ m[/article]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[/article]u) { } 0 0 0
247 0 $uri =~ s[article][article/form/update];
248 } else {
249 # The last backslash becomes /form/update
250 0 $uri =~ s[/([^/]+)$][/form/update/$1];
251 }
252 0 return $c->get($uri);
253 }
254  
255 0 ▶sub connect {
Subroutine connect: not called CC=3 SCAR=24.8
Pod documented
256 0 my $class = shift;
257 0 my %args = @_;
258  
259 0 ▶ my $url = $args{url} or die "missing url";
Branch: 1 branch
BranchTrueFalseTotal
my $url = $args{"url"} or die "missing url" 0 0 0
260 0 my $c = $class->new;
261 0 $c->url($url);
262 0 ▶ $c->find_credentials->login or die "Failed to log in to $url";
Branch: 1 branch
BranchTrueFalseTotal
$c->find_credentials->login or die "Failed to log in to $url" 0 0 0
263 0 return $c;
264 }
265  
266 3 1;
267  
268 __END__
↑ Summary