blib/lib/Gcis/Client.pm

filter: Statementstmt 17.8% 37 / 207 Branchbran 0.0% 0 / 112 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 31.0% 9 / 29 Podpod 50.0% 8 / 16 totaltotal 13.2% 54 / 409 SCAR 80.6
CC
66
Cov
11%
get
110.0
tables
72.0
figures
72.0
CRAP 3168.7
↑ Summary Build »
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.11';
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 has ua => sub {
19 0 ▶ my $c = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
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 start => sub {
25 0 ▶ my ($ua, $tx) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
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 sub get {
41 0 ▶ my $s = shift;
Subroutine get: not called CC=10 SCAR=47.0
Pod documented
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 sub post {
78 0 ▶ my $s = shift;
Subroutine post: not called CC=5 SCAR=34.0
Pod undocumented
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 sub delete {
93 0 ▶ my $s = shift;
Subroutine delete: not called CC=4 SCAR=30.0
Pod undocumented
94 0 my $path = shift;
95 0 my $tx = $s->ua->delete($s->url."$path");
96 0 $s->tx($tx);
97 0 my $res = $tx->success;
98 0 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
99 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
100 0 $s->error("not found : $path");
101 0 return;
102 }
103 0 $s->error($tx->error->{message});
104 0 $s->logger->error($tx->error->{message});
105 0 return;
106 };
107 0 return $res->body;
108 }
109  
110 sub put_file {
111 0 ▶ my $s = shift;
Subroutine put_file: not called CC=4 SCAR=30.0
Pod documented
112 0 my $path = shift;
113 0 my $file = shift;
114 0 my $data = file($file)->slurp;
115 0 my $tx = $s->ua->put($s->url."$path" => $data );
116 0 $s->tx($tx);
117 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
118 0 $s->error(join "\n",$tx->error->{message},$tx->res->body);
119 0 $s->logger->error($path." : ".$tx->error->{message});
120 0 $s->logger->error($tx->res->body);
121 0 return;
122 };
123 0 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
124 0 ▶ my $json = $res->json or return $res->body;
Branch: 1 branch
BranchTrueFalseTotal
my $json = $res->json or return $res->body 0 0 0
125 0 return $res->json;
126 }
127  
128 sub add_file_url {
129 0 ▶ my $s = shift;
Subroutine add_file_url: not called CC=1 SCAR=6.9
Pod documented
130 0 my $gcid = shift;
131 0 my $args = shift;
132 0 my $path = $gcid;
133 0 $path =~ s[/([^/]+)$][/files/$1];
134 0 $s->post($path => $args);
135 }
136  
137 sub post_quiet {
138 0 ▶ my $s = shift;
Subroutine post_quiet: not called CC=5 SCAR=34.0
Pod undocumented
139 0 my $path = shift;
140 0 my $data = shift;
141 0 my $tx = $s->ua->post($s->url."$path" => json => $data );
142 0 $s->tx($tx);
143 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
144 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
145 0 return;
146 };
147 0 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
148 0 ▶ my $json = $res->json or return $res->body;
Branch: 1 branch
BranchTrueFalseTotal
my $json = $res->json or return $res->body 0 0 0
149 0 return $res->json;
150 }
151  
152 sub find_credentials {
153 0 ▶ my $s = shift;
Subroutine find_credentials: not called CC=6 SCAR=37.4
Pod documented
154 0 my $home = $ENV{HOME};
155 0 ▶ die "need url to find credentials" unless $s->url;
Branch: 1 branch
BranchTrueFalseTotal
unless $s->url 0 0 0
156 0 my $conf_file = "$home/etc/Gcis.conf";
157 0 ▶ -e $conf_file or die "Missing $conf_file";
Branch: 1 branch
BranchTrueFalseTotal
-e $conf_file or die "Missing $conf_file" 0 0 0
158 0 my $conf = LoadFile($conf_file);
159 0 my @found = grep { $_->{url} eq $s->url } @$conf;
160 0 ▶ die "Multiple matches for ".$s->url." in $conf_file." if @found > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @found > 1 0 0 0
161 0 ▶ die "No matches for ".$s->url." in $conf_file." if @found < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @found < 1 0 0 0
162 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
163 0 $s->logger->info("Loaded configuration from $conf_file");
164 0 $s->key($key);
165 0 return $s;
166 }
167  
168 sub login {
169 0 ▶ my $c = shift;
Subroutine login: not called CC=3 SCAR=24.8
Pod documented
170 0 ▶ my $got = $c->get('/login') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $got = $c->get("/login") or return 0 0 0
171 0 ▶ $c->get('/login')->{login} eq 'ok' or return;
Branch: 1 branch
BranchTrueFalseTotal
$c->get("/login")->{"login"} eq "ok" or return 0 0 0
172 0 return $c;
173 }
174  
175 sub use_env {
176 0 ▶ my $c = shift;
Subroutine use_env: not called CC=2 SCAR=17.9
Pod documented
177 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
178 0 $c->url($url);
179 0 return $c;
180 }
181  
182 sub get_chapter_map {
183 0 ▶ my $c = shift;
Subroutine get_chapter_map: not called CC=5 SCAR=34.0
Pod documented
184 0 ▶ my $report = shift or die "no report";
Branch: 1 branch
BranchTrueFalseTotal
my $report = shift() or die "no report" 0 0 0
185 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
186 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
187 0 ▶ return wantarray ? %map : \%map;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
188 }
189  
190 sub tables {
191 0 ▶ my $c = shift;
Subroutine tables: not called CC=8 SCAR=42.8
Pod undocumented
192 0 my %a = @_;
193 0 ▶ my $report = $a{report} or die "no report";
Branch: 1 branch
BranchTrueFalseTotal
my $report = $a{"report"} or die "no report" 0 0 0
194 0 ▶ if (my $chapter_number = $a{chapter_number}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chapter_number = $a{"chapter_number"}) 0 0 0
195 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
196 0 $a{chapter} = $c->{_chapter_map}->{$report}->{$chapter_number};
197 }
198 0 my $tables;
199 0 ▶ if (my $chapter = $a{chapter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chapter = $a{'chapter'}) { } 0 0 0
200 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
201 } else {
202 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
203 }
204 0 ▶ return wantarray ? @$tables : $tables;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
205 }
206  
207 sub figures {
208 0 ▶ my $c = shift;
Subroutine figures: not called CC=8 SCAR=42.8
Pod undocumented
209 0 my %a = @_;
210 0 ▶ my $report = $a{report} or die "no report";
Branch: 1 branch
BranchTrueFalseTotal
my $report = $a{"report"} or die "no report" 0 0 0
211 0 ▶ if (my $chapter_number = $a{chapter_number}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chapter_number = $a{"chapter_number"}) 0 0 0
212 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
213 0 $a{chapter} = $c->{_chapter_map}->{$report}->{$chapter_number};
214 }
215 0 my $figures;
216 0 ▶ if (my $chapter = $a{chapter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chapter = $a{'chapter'}) { } 0 0 0
217 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
218 } else {
219 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
220 }
221 0 ▶ return wantarray ? @$figures : $figures;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
222 }
223  
224 sub findings {
225 0 ▶ my $c = shift;
Subroutine findings: not called CC=8 SCAR=42.8
Pod undocumented
226 0 my %a = @_;
227 0 ▶ my $report = $a{report} or die "no report";
Branch: 1 branch
BranchTrueFalseTotal
my $report = $a{"report"} or die "no report" 0 0 0
228 0 ▶ if (my $chapter_number = $a{chapter_number}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chapter_number = $a{"chapter_number"}) 0 0 0
229 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
230 0 $a{chapter} = $c->{_chapter_map}->{$report}->{$chapter_number};
231 }
232 0 my $findings;
233 0 ▶ if (my $chapter = $a{chapter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chapter = $a{'chapter'}) { } 0 0 0
234 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
235 } else {
236 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
237 }
238 0 ▶ return wantarray ? @$findings : $findings;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
239 }
240  
241 sub get_form {
242 0 ▶ my $c = shift;
Subroutine get_form: not called CC=3 SCAR=24.8
Pod undocumented
243 0 my $obj = shift;
244 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
245 0 ▶ if ($uri =~ m[/article]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[/article]u) { } 0 0 0
246 0 $uri =~ s[article][article/form/update];
247 } else {
248 # The last backslash becomes /form/update
249 0 $uri =~ s[/([^/]+)$][/form/update/$1];
250 }
251 0 return $c->get($uri);
252 }
253  
254 sub connect {
255 0 ▶ my $class = shift;
Subroutine connect: not called CC=3 SCAR=24.8
Pod documented
256 0 my %args = @_;
257  
258 0 ▶ my $url = $args{url} or die "missing url";
Branch: 1 branch
BranchTrueFalseTotal
my $url = $args{"url"} or die "missing url" 0 0 0
259 0 my $c = $class->new;
260 0 $c->url($url);
261 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
262 0 return $c;
263 }
264  
265 3 1;
266  
267 __END__
↑ Summary Build »