File Coverage

blib/lib/App/Spoor/OutputFormatter.pm
Criterion Covered Total %
statement 41 41 100.0
branch 3 4 75.0
condition 2 6 33.3
subroutine 9 9 100.0
pod 1 1 100.0
total 56 61 91.8


line stmt bran cond sub pod time code
1             package App::Spoor::OutputFormatter;
2              
3 1     1   201468 use v5.10;
  1         7  
4 1     1   5 use strict;
  1         2  
  1         21  
5 1     1   5 use warnings;
  1         2  
  1         24  
6 1     1   649 use JSON;
  1         9956  
  1         5  
7 1     1   172 use Text::CSV;
  1         3  
  1         37  
8 1     1   499 use Date::Format;
  1         7065  
  1         434  
9              
10             =head1 NAME
11              
12             App::Spoor::OutputFormatter
13              
14             =head1 VERSION
15              
16             Version 0.08
17              
18             =cut
19              
20             our $VERSION = '0.08';
21              
22              
23             =head1 SYNOPSIS
24              
25             This module is used to convert structured data into a formagt suitable for staorage in a file or display on the terminal.
26              
27             Quick summary of what the module does.
28              
29             Perhaps a little code snippet.
30              
31             use App::Spoor::OutputFormatter;
32              
33             my $foo = App::Spoor::OutputFormatter->new();
34             ...
35              
36             =head1 SUBROUTINES/METHODS
37              
38             =head2 print
39              
40             Returns a CSV representation of an array of report representations or mailbox event representations. It can optionally
41             write this output to a file - if no filehandle is provided, output defaults to STDOUT.
42              
43             use App::Spoor::Formatter;
44              
45             # Write reports as csv to STDOUT
46             @reports_data = (
47             { ... },
48             { ... },
49             );
50              
51             App::Spoor::Formatter::print('report', \@reports_data);
52              
53             # Write mailbox events as csv to STDOUT
54             @mailbox_events_data = (
55             { ... },
56             { ... },
57             );
58              
59             App::Spoor::Formatter::print('mailbox_event', \@mailbox_events_data);
60              
61             # Write to a file instead
62             open my $fh, '>', '/tmp/out.csv'
63              
64             App::Spoor::Formatter::print('mailbox_event', \@mailbox_events_data, $fh);
65              
66             close $fh
67             =cut
68              
69             sub print {
70 2     2 1 5447 my $output_type = shift;
71              
72 2 100       11 if ($output_type eq 'report') {
    50          
73 1         4 __print_reports(@_);
74             } elsif ($output_type eq 'mailbox_event') {
75 1         5 __print_mailbox_events(@_);
76             }
77             }
78              
79             sub __print_reports {
80 1     1   2 my $records = shift;
81 1   33     4 my $output_handle = shift // *STDOUT;
82 1         2 my @transformed_record;
83 1         5 my @headers = ('id', 'event time', 'host', 'event type', 'mailbox address');
84 1         9 my $csv = Text::CSV->new({ eol => $/ });
85              
86 1         206 $csv->print($output_handle, \@headers);
87 1         12 foreach my $record_hash (@{$records}) {
  1         3  
88             @transformed_record = (
89             $record_hash->{id},
90             time2str('%Y-%m-%d %H:%M:%S %z', $record_hash->{event_time}, 'UTC'),
91             $record_hash->{host},
92             $record_hash->{type},
93             $record_hash->{mailbox_address}
94 2         18 );
95              
96 2         565 $csv->print($output_handle, \@transformed_record);
97             }
98             }
99              
100             sub __print_mailbox_events {
101 1     1   2 my $records = shift;
102 1   33     16 my $output_handle = shift // *STDOUT;
103 1         3 my @transformed_record;
104 1         8 my @headers = ('id', 'event time', 'host', 'event type', 'mailbox address', 'ip');
105 1         12 my $csv = Text::CSV->new({ eol => $/ });
106              
107 1         138 $csv->print($output_handle, \@headers);
108 1         27 foreach my $record_hash (@{$records}) {
  1         4  
109             @transformed_record = (
110             $record_hash->{id},
111             time2str('%Y-%m-%d %H:%M:%S %z', $record_hash->{event_time}, 'UTC'),
112             $record_hash->{host},
113             $record_hash->{type},
114             $record_hash->{mailbox_address},
115             $record_hash->{ip_actor}{ip_address}
116 2         15 );
117              
118 2         453 $csv->print($output_handle, \@transformed_record);
119             }
120             }
121              
122             =head1 AUTHOR
123              
124             Rory McKinley, C<< <rorymckinley at capefox.co> >>
125              
126             =head1 BUGS
127              
128             Please report any bugs or feature requests to C<bug-. at rt.cpan.org>, or through
129             the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=.>. I will be notified, and then you'll
130             automatically be notified of progress on your bug as I make changes.
131              
132              
133              
134              
135             =head1 SUPPORT
136              
137             You can find documentation for this module with the perldoc command.
138              
139             perldoc App::Spoor::OutputFormatter
140              
141              
142             You can also look for information at:
143              
144             =over 4
145              
146             =item * RT: CPAN's request tracker (report bugs here)
147              
148             L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=.>
149              
150             =item * AnnoCPAN: Annotated CPAN documentation
151              
152             L<http://annocpan.org/dist/.>
153              
154             =item * CPAN Ratings
155              
156             L<https://cpanratings.perl.org/d/.>
157              
158             =item * Search CPAN
159              
160             L<https://metacpan.org/release/.>
161              
162             =back
163              
164             =head1 LICENSE AND COPYRIGHT
165              
166             Copyright 2019 Rory McKinley.
167              
168             This program is free software; you can redistribute it and/or modify it
169             under the terms of the the Artistic License (2.0). You may obtain a
170             copy of the full license at:
171              
172             L<http://www.perlfoundation.org/artistic_license_2_0>
173              
174             Any use, modification, and distribution of the Standard or Modified
175             Versions is governed by this Artistic License. By using, modifying or
176             distributing the Package, you accept this license. Do not use, modify,
177             or distribute the Package, if you do not accept this license.
178              
179             If your Modified Version has been derived from a Modified Version made
180             by someone other than you, you are nevertheless required to ensure that
181             your Modified Version complies with the requirements of this license.
182              
183             This license does not grant you the right to use any trademark, service
184             mark, tradename, or logo of the Copyright Holder.
185              
186             This license includes the non-exclusive, worldwide, free-of-charge
187             patent license to make, have made, use, offer to sell, sell, import and
188             otherwise transfer the Package with respect to any patent claims
189             licensable by the Copyright Holder that are necessarily infringed by the
190             Package. If you institute patent litigation (including a cross-claim or
191             counterclaim) against any party alleging that the Package constitutes
192             direct or contributory patent infringement, then this Artistic License
193             to you shall terminate on the date that such litigation is filed.
194              
195             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
196             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
197             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
198             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
199             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
200             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
201             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
202             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
203              
204              
205             =cut
206              
207             1; # End of App::Spoor::OutputFormatter