File Coverage

blib/lib/App/Toolforge/MixNMatch.pm
Criterion Covered Total %
statement 42 124 33.8
branch 0 26 0.0
condition 0 15 0.0
subroutine 14 21 66.6
pod 2 2 100.0
total 58 188 30.8


line stmt bran cond sub pod time code
1              
2             use strict;
3 3     3   63461 use warnings;
  3         15  
  3         70  
4 3     3   12  
  3         5  
  3         70  
5             use Error::Pure qw(err);
6 3     3   1083 use Getopt::Std;
  3         32836  
  3         56  
7 3     3   5257 use IO::Barf qw(barf);
  3         150  
  3         171  
8 3     3   1068 use JSON::XS;
  3         1576  
  3         38  
9 3     3   1822 use LWP::Simple qw(get);
  3         17334  
  3         174  
10 3     3   1137 use Perl6::Slurp qw(slurp);
  3         186690  
  3         22  
11 3     3   1749 use Readonly;
  3         4027  
  3         15  
12 3     3   105 use Toolforge::MixNMatch::Diff;
  3         6  
  3         126  
13 3     3   1209 use Toolforge::MixNMatch::Print::Catalog;
  3         21645  
  3         83  
14 3     3   1149 use Toolforge::MixNMatch::Struct::Catalog;
  3         47958  
  3         87  
15 3     3   1200 use Unicode::UTF8 qw(encode_utf8);
  3         6844  
  3         119  
16 3     3   1034  
  3         1230  
  3         3018  
17             # Constants
18             Readonly::Scalar our $URI_BASE => 'https://mix-n-match.toolforge.org/';
19             Readonly::Scalar our $URI_CATALOG_DETAIL => $URI_BASE.'api.php?query=catalog_details&catalog=%s';
20              
21             our $VERSION = 0.05;
22              
23             # Constructor.
24             my ($class, @params) = @_;
25              
26 1     1 1 70 # Create object.
27             my $self = bless {}, $class;
28              
29 1         3 # Object.
30             return $self;
31             }
32 1         3  
33             my $json_file = shift;
34              
35             my $json = slurp($json_file);
36 0     0     my $struct_hr = decode_json($json);
37             my $obj = Toolforge::MixNMatch::Struct::Catalog::struct2obj($struct_hr->{'data'});
38 0            
39 0           return $obj;
40 0           }
41              
42 0           my ($json_file1, $json_file2, $print_options) = @_;
43              
44             if (! defined $json_file1 || ! -r $json_file1) {
45             return (1, "Doesn't exist JSON file #1 for diff.");
46 0     0     }
47             if (! defined $json_file2 || ! -r $json_file2) {
48 0 0 0       return (1, "Doesn't exist JSON file #2 for diff.");
49 0           }
50              
51 0 0 0       my $opts_hr = _process_print_options($print_options);
52 0            
53             my $obj1 = _catalog_json_file_to_obj($json_file1);
54             my $obj2 = _catalog_json_file_to_obj($json_file2);
55 0            
56             my $diff = Toolforge::MixNMatch::Diff::diff($obj1, $obj2);
57 0            
58 0           my $ret = Toolforge::MixNMatch::Print::Catalog::print($diff, $opts_hr);
59             print encode_utf8($ret)."\n";
60 0            
61             return (0, undef);
62 0           }
63 0            
64             my ($catalog_id, $output_file) = @_;
65 0            
66             if (! defined $catalog_id || $catalog_id !~ m/^\d+$/ms) {
67             return (1, 'Missing or bad catalog ID.');
68             }
69 0     0     if (! defined $output_file) {
70             $output_file = $catalog_id.'.json';
71 0 0 0       }
72 0            
73             my $json = _download_catalog_detail($catalog_id);
74 0 0         barf($output_file, $json);
75 0            
76             print "Catalog with '$catalog_id' ID was saved to '$output_file'.\n";
77              
78 0           return (0, undef);
79 0           }
80              
81 0           my ($json_file_or_catalog_id, $print_options) = @_;
82              
83 0           my $json;
84             if (-r $json_file_or_catalog_id) {
85             $json = slurp($json_file_or_catalog_id);
86             } elsif ($json_file_or_catalog_id =~ m/^\d+$/ms) {
87 0     0     $json = _download_catalog_detail($json_file_or_catalog_id);
88             } else {
89 0           return (1, "Doesn't exist JSON file or catalog ID for print.");
90 0 0         }
    0          
91 0            
92             my $opts_hr = _process_print_options($print_options);
93 0            
94             my $struct_hr = decode_json($json);
95 0           my $obj = Toolforge::MixNMatch::Struct::Catalog::struct2obj($struct_hr->{'data'});
96             my $ret = Toolforge::MixNMatch::Print::Catalog::print($obj, $opts_hr);
97             print encode_utf8($ret)."\n";
98 0            
99             return (0, undef);
100 0           }
101 0            
102 0           my $catalog_id = shift;
103 0            
104             my $uri = sprintf $URI_CATALOG_DETAIL, $catalog_id;
105 0           my $json = get($uri);
106             if (! defined $json) {
107             err "Cannot download '$uri'.";
108             }
109 0     0      
110             return $json;
111 0           }
112 0            
113 0 0         my $print_options = shift;
114 0            
115             if (! defined $print_options) {
116             return;
117 0           }
118              
119             my $opts_hr = {};
120             foreach my $print_option (split m/,/, $print_options) {
121 0     0     $opts_hr->{$print_option} = 1;
122             }
123 0 0          
124 0           return $opts_hr;
125             }
126              
127 0           # Run script.
128 0           my $self = shift;
129 0            
130             # Process arguments.
131             $self->{'_opts'} = {
132 0           'h' => 0,
133             };
134             if (! getopts('h', $self->{'_opts'}) || $self->{'_opts'}->{'h'}
135             || @ARGV < 1) {
136              
137 0     0 1   print STDERR "Usage: $0 [-h] [--version] [command] [command_args ..]\n";
138             print STDERR "\t-h\t\tPrint help.\n";
139             print STDERR "\t--version\tPrint version.\n";
140 0           print STDERR "\tcommand\t\tCommand (diff, download, print).\n\n";
141             print STDERR "\tcommand 'diff' arguments:\n";
142             print STDERR "\t\tjson_file1 - JSON file #1\n";
143 0 0 0       print STDERR "\t\tjson_file2 - JSON file #2\n";
      0        
144             print STDERR "\t\t[print_options] - Print options (type, count, year_months, users)\n";
145             print STDERR "\tcommand 'download' arguments:\n";
146 0           print STDERR "\t\tcatalog_id - Catalog ID\n";
147 0           print STDERR "\t\t[output_file] - Output file (default is catalog_id.json)\n";
148 0           print STDERR "\tcommand 'print' arguments:\n";
149 0           print STDERR "\t\tjson_file or catalog_id - Catalog ID or JSON file\n";
150 0           print STDERR "\t\t[print_options] - Print options (type, count, year_months, users)\n";
151 0           return 1;
152 0           }
153 0           my $command = shift @ARGV;
154 0           my @command_args = @ARGV;
155 0            
156 0           my ($return, $error);
157 0           if ($command eq 'diff') {
158 0           ($return, $error) = _command_diff(@command_args);
159 0           } elsif ($command eq 'download') {
160 0           ($return, $error) = _command_download(@command_args);
161             } elsif ($command eq 'print') {
162 0           ($return, $error) = _command_print(@command_args);
163 0           } else {
164             print STDERR "Command '$command' doesn't supported.\n";
165 0           return 1;
166 0 0         }
    0          
    0          
167 0           if ($return == 1) {
168             print STDERR $error."\n";
169 0           return 1;
170             }
171 0            
172             return 0;
173 0           }
174 0            
175             1;
176 0 0          
177 0            
178 0           =pod
179              
180             =encoding utf8
181 0            
182             =head1 NAME
183              
184             App::Toolforge::MixNMatch - Perl class for mix-n-match application.
185              
186             =head1 SYNOPSIS
187              
188             use App::Toolforge::MixNMatch;
189              
190             my $obj = App::Toolforge::MixNMatch->new;
191             $obj->run;
192              
193             =head1 METHODS
194              
195             =over 8
196              
197             =item C<new()>
198              
199             Constructor.
200              
201             =item C<run()>
202              
203             Run.
204              
205             =back
206              
207             =head1 ERRORS
208              
209             new():
210             From Class::Utils:
211             Unknown parameter '%s'.
212              
213             =head1 EXAMPLE
214              
215             use strict;
216             use warnings;
217              
218             use App::Toolforge::MixNMatch;
219              
220             # Run.
221             exit App::Toolforge::MixNMatch->new->run;
222              
223             # Output:
224             # Usage: ./examples/ex1.pl [-h] [--version] [command] [command_args ..]
225             # -h Print help.
226             # --version Print version.
227             # command Command (diff, download, print).
228             #
229             # command 'diff' arguments:
230             # json_file1 - JSON file #1
231             # json_file2 - JSON file #2
232             # [print_options] - Print options (type, count, year_months, users)
233             # command 'download' arguments:
234             # catalog_id - Catalog ID
235             # [output_file] - Output file (default is catalog_id.json)
236             # command 'print' arguments:
237             # json_file or catalog_id - Catalog ID or JSON file
238             # [print_options] - Print options (type, count, year_months, users)
239              
240             =head1 DEPENDENCIES
241              
242             L<Error::Pure>,
243             L<Getopt::Std>,
244             L<IO::Barf>,
245             L<JSON::XS>,
246             L<LWP::Simple>,
247             L<Perl6::Slurp>,
248             L<Readonly>,
249             L<Toolforge::MixNMatch::Diff>,
250             L<Toolforge::MixNMatch::Print::Catalog>,
251             L<Toolforge::MixNMatch::Struct::Catalog>,
252             L<Unicode::UTF8>.
253              
254             =head1 REPOSITORY
255              
256             L<https://github.com/tupinek/App-Toolforge-MixNMatch>.
257              
258             =head1 AUTHOR
259              
260             Michal Josef Špaček L<mailto:skim@cpan.org>
261              
262             L<http://skim.cz>
263              
264             =head1 LICENSE AND COPYRIGHT
265              
266             © 2020-2022 Michal Josef Špaček
267              
268             BSD 2-Clause License
269              
270             =head1 VERSION
271              
272             0.05
273              
274             =cut