File Coverage

blib/lib/WWW/ARDB/CLI.pm
Criterion Covered Total %
statement 38 40 95.0
branch 1 2 50.0
condition 1 3 33.3
subroutine 7 8 87.5
pod 1 2 50.0
total 48 55 87.2


line stmt bran cond sub pod time code
1             package WWW::ARDB::CLI;
2             our $AUTHORITY = 'cpan:GETTY';
3              
4             # ABSTRACT: Command-line interface for WWW::ARDB
5              
6 2     2   388021 use Moo;
  2         7762  
  2         11  
7 2     2   4031 use MooX::Cmd;
  2         8066  
  2         15  
8 2     2   500332 use WWW::ARDB;
  2         10  
  2         112  
9 2     2   17 use JSON::MaybeXS qw( encode_json );
  2         6  
  2         158  
10 2     2   1867 use Getopt::Long qw(:config pass_through);
  2         25343  
  2         10  
11              
12             our $VERSION = '0.002';
13              
14              
15             has debug => (
16             is => 'ro',
17             default => sub { $ENV{WWW_ARDB_DEBUG} // 0 },
18             );
19              
20              
21             has no_cache => (
22             is => 'ro',
23             default => sub { $ENV{WWW_ARDB_NO_CACHE} // 0 },
24             );
25              
26              
27             has json => (
28             is => 'ro',
29             default => sub { $ENV{WWW_ARDB_JSON} // 0 },
30             );
31              
32              
33             around BUILDARGS => sub {
34             my ($orig, $class, @args) = @_;
35              
36             my ($debug, $no_cache, $json);
37             GetOptions(
38             'debug|d' => \$debug,
39             'no-cache' => \$no_cache,
40             'json|j' => \$json,
41             );
42              
43             my $result = $class->$orig(@args);
44             $result->{debug} = $debug if $debug;
45             $result->{no_cache} = $no_cache if $no_cache;
46             $result->{json} = $json if $json;
47              
48             return $result;
49             };
50              
51             has api => (
52             is => 'lazy',
53             builder => '_build_api',
54             );
55              
56              
57             sub _build_api {
58 1     1   594 my $self = shift;
59 1         17 return WWW::ARDB->new(
60             debug => $self->debug,
61             use_cache => !$self->no_cache,
62             );
63             }
64              
65             sub execute {
66 1     1 0 1352 my ($self, $args, $chain) = @_;
67              
68 1 50 33     7 if (!@$chain || @$chain == 1) {
69 1         47 print "ardb - ARC Raiders Database CLI\n\n";
70 1         31 print "Usage: ardb [options]\n\n";
71 1         9 print "Commands:\n";
72 1         7 print " items List all items\n";
73 1         7 print " item Show item details\n";
74 1         8 print " quests List all quests\n";
75 1         7 print " quest Show quest details\n";
76 1         8 print " enemies List all ARC enemies\n";
77 1         8 print " enemy Show ARC enemy details\n";
78 1         7 print "\nGlobal Options:\n";
79 1         7 print " -d, --debug Enable debug output\n";
80 1         8 print " -j, --json Output as JSON\n";
81 1         6 print " --no-cache Disable caching\n";
82 1         8 print "\nExamples:\n";
83 1         7 print " ardb items --search guitar\n";
84 1         6 print " ardb item acoustic_guitar\n";
85 1         7 print " ardb enemies\n";
86 1         14 print " ardb enemy wasp --json\n";
87 1         12 print "\nData provided by ardb.app\n";
88             }
89             }
90              
91             sub output_json {
92 0     0 1   my ($self, $data) = @_;
93 0           print encode_json($data) . "\n";
94             }
95              
96              
97             1;
98              
99             __END__