File Coverage

bin/idoit-query.pl
Criterion Covered Total %
statement 32 58 55.1
branch 4 16 25.0
condition 2 3 66.6
subroutine 8 12 66.6
pod n/a
total 46 89 51.6


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2             #
3 1     1   372474 use strict;
  1         3  
  1         47  
4 1     1   6 use warnings;
  1         2  
  1         75  
5 1     1   19 use 5.010;
  1         4  
6              
7 1     1   897 use Getopt::Long;
  1         16286  
  1         6  
8 1     1   863 use Pod::Usage;
  1         55129  
  1         197  
9 1     1   825 use WebService::IdoitAPI::Object;
  1         5  
  1         53  
10              
11 1     1   42 use version; our $VERSION = '0.4.6'; # VERSION
  1         4  
  1         15  
12              
13             sub get_answer {
14 0     0   0 my ($config,$api,$query) = @_;
15              
16 0         0 my $answer = $api->request($query);
17              
18 0         0 return $answer;
19             } # get_answer()
20              
21             sub get_query {
22 2     2   748 my ($config) = @_;
23 2         4 my ($json,$jsontext,$query);
24              
25 2 100 66     17 if (exists $config->{opt}->{file}
26             and my $fname = $config->{opt}->{file}) {
27 1         8 local $/ = undef;
28 1 50       4 if ($fname eq '-') {
29 0         0 $jsontext = ;
30             }
31             else {
32 1 50       69 open(my $fh,'<',$fname)
33             or die "Can't open JSON query file '$fname': $!";
34 1         98 $jsontext = <$fh>;
35 1         36 close($fh);
36             }
37             }
38             else {
39 1         2 $jsontext = $ARGV[0];
40             }
41 2         32 $json = JSON->new();
42 2         41 $query = $json->decode($jsontext);
43 2         24 return $query;
44             } # get_query();
45              
46             sub initialize {
47 0     0     my $config = {};
48 0           my $opt = {};
49 0           GetOptions($opt, qw(
50             config|c=s
51             file|f=s
52             pretty!
53             help|h|? man version|V
54             ));
55 0 0         pod2usage(-exitstatus => 0, -input => \*DATA) if $opt->{help};
56             pod2usage(-exitstatus => 0, -input => \*DATA,
57 0 0         -verbose => 2) if $opt->{man};
58             pod2usage(-exitstatus => 0, -input => \*DATA,
59 0 0         -verbose => 99, -sections => 'VERSION') if $opt->{version};
60              
61 0           $config = WebService::IdoitAPI::read_config($opt->{config});
62              
63 0           $config->{opt} = $opt;
64              
65 0           return $config;
66             } # initialize()
67              
68             sub print_json {
69 0     0     my ($config,$data) = @_;
70              
71 0           my $json = JSON->new();
72              
73 0 0         if ( $config->{opt}->{pretty} ) {
74 0           print $json->canonical->pretty->encode($data);
75             }
76             else {
77 0           print $json->encode($data);
78             }
79             } # print_json()
80              
81             sub main {
82 0     0     my ($api,$config,$data,$query);
83              
84 0           $config = initialize();
85 0           $api = WebService::IdoitAPI->new($config);
86 0           $query = get_query($config);
87 0           $data = get_answer($config,$api,$query);
88 0 0         if ($data->{is_success}) {
89 0           print_json($config,$data->{content});
90             }
91             else {
92 0           die "i-doit did not return success\n";
93             }
94             } # main()
95              
96             main() if not caller();
97              
98             1;
99              
100             __END__