Spoor/ErrorEntryParser.pm

filter: Statementstmt 100% 31 / 31 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 100% 46 / 46 CC 5 SCAR 11.0
CC
5
Cov
100%
parse
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.0
« Spoor/TransmissionFormatter.pm ↑ Summary Spoor/ParsedFileManager.pm »
1 package App::Spoor::ErrorEntryParser;
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  
7 =head1 NAME
8  
9 App::Spoor::ErrorEntryParser
10  
11 =head1 VERSION
12  
13 Version 0.01
14  
15 =cut
16  
17 1 our $VERSION = '0.01';
18  
19 =head1 SYNOPSIS
20  
21 This package contains the necessary functionality to parse CPanel error log entries.
22  
23 =head1 SUBROUTINES/METHODS
24  
25 =head2 parse
26  
27 This subroutine accepts a single line from a CPanel error log (as a string) and returns a reference to a hash
28 representation of that entry.
29  
30 The hash representation contains the following elements:
31  
32 =over 2
33  
34 =item * type: This is hardcoded to 'error'
35  
36 =item * event: A description of the event that the entry refers to - can be one of forward_added_partial_recipient, unrecognised.
37  
38 =item * log_time: A DateTime instance representing the time of the log entry. It is not set if the event is 'unrecognised'.
39  
40 =item * context: The context within which the operation is being performed can be either 'mailbox' or 'domain'. It is not set if the event is 'unrecognised'.
41  
42 =item * forward_type: Can be one of 'system_user', 'pipe' or 'email'. It is not set if the event is 'unrecognised'.
43  
44 =item * forward_to: The recipient of the forwarded email. It is not set if the event is 'unrecognised'.
45  
46 =item * email: The mailbox that the forward is being applied to. It is not set if the event is 'unrecognised'.
47  
48 =item * status: The status of the request is hardcoded to success. It is not set if the event is 'unrecognised'.
49  
50 =back
51  
52 =cut
53  
54 10 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.1
Pod documented
55 1 ▶ use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶ use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 10 my $log_entry = shift;
59 10 my $date_parser = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d %H:%M:%S %z', on_error => 'croak');
60 10 my %response;
61 my $data_ref;
62 10 my $timestamp;
63 10 my $forward_type;
64  
65 10 ▶ if ($log_entry =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($log_entry =~ / \A \[(?<timestamp>[^\]]+)\]\s info\s \[spoor_forward_added\]\s (?<data>{.+}) \Z /x) { } 8 2 10
66 \A
67 \[(?<timestamp>[^\]]+)\]\s
68 info\s
69 \[spoor_forward_added\]\s
70 (?<data>{.+})
71 \Z
72 /x) {
73 8 $data_ref = from_json($+{data});
74 8 $timestamp = $date_parser->parse_datetime($+{timestamp})->epoch();
75  
76 8 ▶ if ($data_ref->{args}{fwdopt} eq 'system') {
Branch: 2 branches
BranchTrueFalseTotal
if ($data_ref->{'args'}{'fwdopt'} eq 'system') { } 2 6 8
elsif ($data_ref->{'args'}{'fwdopt'} eq 'pipe') { } 2 4 6
77 2 $forward_type = 'system_user';
78 } elsif ($data_ref->{args}{fwdopt} eq 'pipe') {
79 2 $forward_type = 'pipe';
80 } else {
81 4 $forward_type = 'email';
82 }
83 %response = (
84 type => 'error',
85 event => 'forward_added_partial_recipient',
86 context => ($data_ref->{args}{domain} eq '' ? 'mailbox' : 'domain'),
87 forward_type =>$forward_type,
88 forward_to => $data_ref->{result}[0]{forward},
89 email => $data_ref->{result}[0]{email},
90 8 ▶ log_time => $timestamp,
Branch: 1 branch
BranchTrueFalseTotal
$data_ref->{'args'}{'domain'} eq '' ? : 2 6 8
91 status => 'success'
92 );
93 } else {
94 2 %response = (
95 type => 'error',
96 event => 'unrecognised',
97 );
98 }
99  
100 10 return \%response;
101 }
102  
103 =head1 AUTHOR
104  
105 Rory McKinley, C<< <rorymckinley at capefox.co> >>
106  
107 =head1 BUGS
108  
109 Please report any bugs or feature requests to C<bug-. at rt.cpan.org>, or through
110 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=.>. I will be notified, and then you'll
111 automatically be notified of progress on your bug as I make changes.
112  
113  
114 =head1 SUPPORT
115  
116 You can find documentation for this module with the perldoc command.
117  
118 perldoc App::Spoor::ErrorEntryParser
119  
120  
121 You can also look for information at:
122  
123 =over 4
124  
125 =item * RT: CPAN's request tracker (report bugs here)
126  
127 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=.>
128  
129 =item * AnnoCPAN: Annotated CPAN documentation
130  
131 L<http://annocpan.org/dist/.>
132  
133 =item * CPAN Ratings
134  
135 L<https://cpanratings.perl.org/d/.>
136  
137 =item * Search CPAN
138  
139 L<https://metacpan.org/release/.>
140  
141 =back
142  
143 =head1 LICENSE AND COPYRIGHT
144  
145 Copyright 2019 Rory McKinley.
146  
147 This program is free software; you can redistribute it and/or modify it
148 under the terms of the the Artistic License (2.0). You may obtain a
149 copy of the full license at:
150  
151 L<http://www.perlfoundation.org/artistic_license_2_0>
152  
153 Any use, modification, and distribution of the Standard or Modified
154 Versions is governed by this Artistic License. By using, modifying or
155 distributing the Package, you accept this license. Do not use, modify,
156 or distribute the Package, if you do not accept this license.
157  
158 If your Modified Version has been derived from a Modified Version made
159 by someone other than you, you are nevertheless required to ensure that
160 your Modified Version complies with the requirements of this license.
161  
162 This license does not grant you the right to use any trademark, service
163 mark, tradename, or logo of the Copyright Holder.
164  
165 This license includes the non-exclusive, worldwide, free-of-charge
166 patent license to make, have made, use, offer to sell, sell, import and
167 otherwise transfer the Package with respect to any patent claims
168 licensable by the Copyright Holder that are necessarily infringed by the
169 Package. If you institute patent litigation (including a cross-claim or
170 counterclaim) against any party alleging that the Package constitutes
171 direct or contributory patent infringement, then this Artistic License
172 to you shall terminate on the date that such litigation is filed.
173  
174 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
175 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
176 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
177 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
178 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
179 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
180 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
181 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
182  
183  
184 =cut
185  
186 1 1; # End of App::Spoor::ErrorEntryParser
« Spoor/TransmissionFormatter.pm ↑ Summary Spoor/ParsedFileManager.pm »