| 1 | package App::Spoor::OutputFormatter; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use v5.10; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Text::CSV; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use Date::Format; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | App::Spoor::OutputFormatter | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | Version 0.08 | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1 | 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 | 2 | ▶ | sub print { | ||||||||||||||||||
|
Subroutine
print: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 70 | 2 | my $output_type = shift; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 2 | ▶ | if ($output_type eq 'report') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 73 | 1 | __print_reports(@_); | |||||||||||||||||||
| 74 | } elsif ($output_type eq 'mailbox_event') { | ||||||||||||||||||||
| 75 | 1 | __print_mailbox_events(@_); | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 1 | ▶ | sub __print_reports { | ||||||||||||||||||
|
Subroutine
__print_reports: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 80 | 1 | my $records = shift; | |||||||||||||||||||
| 81 | 1 | ▶ | my $output_handle = shift // *STDOUT; | ||||||||||||||||||
|
Condition33%1/3
shift() // *STDOUT
MC/DC0%0/2
shift() // *STDOUT
shift()
*STDOUT
Truth table33%1/3
A // B
A= shift()B= *STDOUT
| |||||||||||||||||||||
| 82 | 1 | my @transformed_record; | |||||||||||||||||||
| 83 | 1 | my @headers = ('id', 'event time', 'host', 'event type', 'mailbox address'); | |||||||||||||||||||
| 84 | 1 | my $csv = Text::CSV->new({ eol => $/ }); | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 1 | $csv->print($output_handle, \@headers); | |||||||||||||||||||
| 87 | 1 | foreach my $record_hash (@{$records}) { | |||||||||||||||||||
| 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 | ); | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 2 | $csv->print($output_handle, \@transformed_record); | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 1 | ▶ | sub __print_mailbox_events { | ||||||||||||||||||
|
Subroutine
__print_mailbox_events: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 101 | 1 | my $records = shift; | |||||||||||||||||||
| 102 | 1 | ▶ | my $output_handle = shift // *STDOUT; | ||||||||||||||||||
|
Condition33%1/3
shift() // *STDOUT
MC/DC0%0/2
shift() // *STDOUT
shift()
*STDOUT
Truth table33%1/3
A // B
A= shift()B= *STDOUT
| |||||||||||||||||||||
| 103 | 1 | my @transformed_record; | |||||||||||||||||||
| 104 | 1 | my @headers = ('id', 'event time', 'host', 'event type', 'mailbox address', 'ip'); | |||||||||||||||||||
| 105 | 1 | my $csv = Text::CSV->new({ eol => $/ }); | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 1 | $csv->print($output_handle, \@headers); | |||||||||||||||||||
| 108 | 1 | foreach my $record_hash (@{$records}) { | |||||||||||||||||||
| 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 | ); | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 2 | $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 | 1; # End of App::Spoor::OutputFormatter | |||||||||||||||||||