Ordo/API.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 80 CC 2 SCAR 9.8
CC
2
Cov
0%
_build_config
6.0
_build_tz
2.0
_build_ua
2.0
CRAP 2.7
1 package App::Ordo::API;
2 use Moo;
3 use feature qw(say);
4 use Mojo::UserAgent;
5 use JSON qw(decode_json encode_json);
6 use File::Path qw(make_path);
7 use File::Copy qw(copy);
8 use File::ShareDir qw(dist_file);
9  
10 has 'ua' => (is => 'lazy');
11 has 'config_file' => (
12 is => 'ro',
13 default => sub { "$ENV{HOME}/.config/App-ordo/ordo_config.json" },
14 );
15  
16 has 'config' => (is => 'lazy');
17  
18 has 'tz' => (is => 'lazy');
19 sub _build_tz { DateTime::TimeZone::Local->TimeZone->name }
20  
21 sub _build_ua { Mojo::UserAgent->new->inactivity_timeout(10) }
22  
23 sub _build_config {
24 my $self = shift;
25  
26 unless (-f $self->config_file) {
27 make_path("$ENV{HOME}/.config/App-ordo");
28 my $default = dist_file('App-ordo', 'ordo_config.json');
29 copy($default, $self->config_file) or die "Cannot copy config: $!";
30 say "Created default config at " . $self->config_file;
31 }
32  
33 open my $fh, '<', $self->config_file or die "Cannot read config";
34 my $json = decode_json(do { local $/; <$fh> });
35 close $fh;
36  
37 return $json;
38 }
39  
40 sub call {
41 my ($self, $command, $params) = @_;
42 $params->{command} = $command;
43 $params->{token} //= $self->config->{token};
44  
45 my $tx = $self->ua->post($self->config->{api} => json => $params);
46 my $res = $tx->res->json // { success => 0, message => $tx->res->message || 'Unknown' };
47 return $res;
48 }
49  
50 1;