File Coverage

blib/lib/App/Spoor/ErrorEntryParser.pm
Criterion Covered Total %
statement 32 32 100.0
branch 8 8 100.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 48 48 100.0


line stmt bran cond sub pod time code
1             package App::Spoor::ErrorEntryParser;
2              
3 1     1   655346 use v5.10;
  1         10  
4 1     1   6 use strict;
  1         3  
  1         23  
5 1     1   5 use warnings;
  1         8  
  1         62  
6              
7             =head1 NAME
8              
9             App::Spoor::ErrorEntryParser
10              
11             =head1 VERSION
12              
13             Version 0.01
14              
15             =cut
16              
17             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             sub parse {
55 1     1   672 use DateTime::Format::Strptime;
  1         55074  
  1         6  
56 1     1   80 use JSON;
  1         2  
  1         10  
57              
58 10     10 1 3491 my $log_entry = shift;
59 10         36 my $date_parser = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d %H:%M:%S %z', on_error => 'croak');
60 10         13021 my %response;
61             my $data_ref;
62 10         0 my $timestamp;
63 10         0 my $forward_type;
64              
65 10 100       82 if ($log_entry =~ /
66             \A
67             \[(?<timestamp>[^\]]+)\]\s
68             info\s
69             \[spoor_forward_added\]\s
70             (?<data>{.+})
71             \Z
72             /x) {
73 1     1   708 $data_ref = from_json($+{data});
  1         406  
  1         180  
  8         42  
74 8         259 $timestamp = $date_parser->parse_datetime($+{timestamp})->epoch();
75              
76 8 100       6242 if ($data_ref->{args}{fwdopt} eq 'system') {
    100          
77 2         5 $forward_type = 'system_user';
78             } elsif ($data_ref->{args}{fwdopt} eq 'pipe') {
79 2         7 $forward_type = 'pipe';
80             } else {
81 4         9 $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 100       78 log_time => $timestamp,
91             status => 'success'
92             );
93             } else {
94 2         7 %response = (
95             type => 'error',
96             event => 'unrecognised',
97             );
98             }
99              
100 10         403 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; # End of App::Spoor::ErrorEntryParser