blib/lib/POE/Component/Client/SimpleFTP/Utils.pm

filter: Statementstmt 100% 52 / 52 Branchbran 89.2% 25 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 97.0% 97 / 100 CC 16 SCAR 10.8
CC
16
Cov
96%
feat_parser
5.0
_check_code
4.0
mdtm_parser
4.0
CRAP 2.9
« Build ↑ Summary
1 #
2 # This file is part of POE-Component-Client-SimpleFTP
3 #
4 # This software is copyright (c) 2011 by Apocalypse.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 3 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10 package POE::Component::Client::SimpleFTP::Utils;
11 BEGIN {
12 3 ▶ $POE::Component::Client::SimpleFTP::Utils::VERSION = '0.003';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 }
14 BEGIN {
15 3 ▶ $POE::Component::Client::SimpleFTP::Utils::AUTHORITY = 'cpan:APOCAL';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 }
17  
18 # ABSTRACT: Miscellaneous FTP utility functions
19  
20 3 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 our @EXPORT_OK = qw(
22 code_preliminary code_success code_intermediate code_failure code_tls
23 EOL
24 mdtm_parser feat_parser
25 );
26 3 our %EXPORT_TAGS = (
27 'code' => [
28 qw( code_preliminary code_success code_intermediate code_failure code_tls ),
29 ],
30 );
31  
32  
33  
34  
35  
36  
37 # helper sub to validate a code before doing the actual comparison
38 sub _check_code {
39 31 ▶ return if ! defined $_[0];
Subroutine _check_code: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 5 26 31
40 26 ▶ return if length( $_[0] ) != 3;
Branch: 1 branch
BranchTrueFalseTotal
if length $_[0] != 3 15 11 26
41 11 ▶ return if $_[0] !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\d+$/ 0 11 11
42 11 return 1;
43 }
44  
45 # helper subs to figure out what a code is
46 6 ▶sub code_preliminary { return if ! _check_code( $_[0] ); return substr( $_[0], 0, 1 ) == 1 }
Subroutine code_preliminary: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless _check_code($_[0]) 4 2 6
47 6 ▶sub code_success { return if ! _check_code( $_[0] ); return substr( $_[0], 0, 1 ) == 2 }
Subroutine code_success: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless _check_code($_[0]) 4 2 6
48 6 ▶sub code_intermediate { return if ! _check_code( $_[0] ); return substr( $_[0], 0, 1 ) == 3 }
Subroutine code_intermediate: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless _check_code($_[0]) 4 2 6
49 7 ▶sub code_failure { return if ! _check_code( $_[0] ); return $_[0] =~ /^[45]/ }
Subroutine code_failure: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless _check_code($_[0]) 4 3 7
50 6 ▶sub code_tls { return if ! _check_code( $_[0] ); return substr( $_[0], 0, 1 ) == 6 }
Subroutine code_tls: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless _check_code($_[0]) 4 2 6
51  
52  
53 sub EOL () { "\015\012" }
54  
55  
56 sub mdtm_parser {
57 8 ▶ my $mdtm = shift;
Subroutine mdtm_parser: called CC=4 SCAR=13.9
Pod documented
58  
59 # check to see if we received microseconds
60 8 my $microseconds;
61 8 ▶ if ( $mdtm =~ /^(\d+)\.(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mdtm =~ /^(\d+)\.(\d+)$/) 2 6 8
62 2 $mdtm = $1;
63 2 $microseconds = $2;
64 }
65  
66 8 require DateTime::Format::Strptime;
67 8 my $strp = DateTime::Format::Strptime->new(
68 # RFC 3659 pattern: YYYYMMDDHHMMSS.sss
69 pattern => "%Y%m%d%H%M%S",
70 on_error => 'undef',
71 );
72 8 my $dt = $strp->parse_datetime( $mdtm );
73 8 ▶ if ( defined $dt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dt) { } 3 5 8
74 3 ▶ if ( defined $microseconds ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $microseconds) 2 1 3
75 # add it to the object!
76 2 $dt->set_nanosecond( $microseconds * 1000 );
77 }
78 3 return $dt;
79 } else {
80 5 return;
81 }
82 }
83  
84  
85 sub feat_parser {
86 8 ▶ my $feat = shift;
Subroutine feat_parser: called CC=5 SCAR=16.2
Pod documented
87  
88 # validation
89 8 ▶ return () if ! defined $feat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $feat 0 8 8
90 8 ▶ return () if ! length( $feat );
Branch: 1 branch
BranchTrueFalseTotal
unless length $feat 0 8 8
91  
92 # it should be a string with newlines in it separating the FEAT replies
93 8 my @data = split( "\n", $feat );
94 8 ▶ return () if scalar @data <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @data <= 1 4 4 8
95  
96 # remove the first/last elements as they are informational text
97 4 shift @data;
98 4 pop @data;
99  
100 # remove any whitespace
101 4 foreach my $f ( @data ) {
102 7 $f =~ s/^\s+//;
103 7 $f =~ s/\s+$//;
104 }
105  
106 # all done!
107 4 return @data;
108 }
109  
110 3 1;
111  
112  
113 __END__
114 =pod
115  
116 =for :stopwords Apocalypse
117  
118 =encoding utf-8
119  
120 =head1 NAME
121  
122 POE::Component::Client::SimpleFTP::Utils - Miscellaneous FTP utility functions
123  
124 =head1 VERSION
125  
126 This document describes v0.003 of POE::Component::Client::SimpleFTP::Utils - released May 04, 2011 as part of POE-Component-Client-SimpleFTP.
127  
128 =head1 SYNOPSIS
129  
130 use POE::Component::Client::SimpleFTP::Utils qw( :code );
131  
132 # in an event handler
133 my $code = shift;
134 if ( code_success( $code ) ) {
135 print "FTP command OK\n";
136 } else {
137 warn "FTP command error!";
138 }
139  
140 =head1 DESCRIPTION
141  
142 This module provides the various utility functions for use in your FTP application. You can import any sub listed in the POD or those tags:
143  
144 =head2 code
145  
146 Imports all of the code_* subs
147  
148 =head1 FUNCTIONS
149  
150 =head2 code_preliminary
151  
152 Tests whether the code is a 1yz code ( Positive Preliminary reply ) and returns a boolean value.
153  
154 =head2 code_success
155  
156 Tests whether the code is a 2yz code ( Positive Completion reply ) and returns a boolean value.
157  
158 =head2 code_intermediate
159  
160 Tests whether the code is a 3yz code ( Positive Intermediate reply ) and returns a boolean value.
161  
162 =head2 code_failure
163  
164 Tests whether the code is a 4yz or 5yz code ( Transient/Permanent Negative Completion reply ) and returns a boolean value.
165  
166 =head2 code_tls
167  
168 Tests whether the code is a 6yz code ( Protected reply ) and returns a boolean value.
169  
170 =head2 EOL
171  
172 Returns the end-of-line terminator as specified in RFC 959
173  
174 =head2 mdtm_parser
175  
176 Returns a L<DateTime> object representing the modification timestamp of a file. Useful for parsing L<POE::Component::Client::SimpleFTP/mdtm> replies!
177  
178 NOTE: The MDTM format does not supply a timezone, you have to process that yourself!
179  
180 On an error returns undef.
181  
182 =head2 feat_parser
183  
184 Returns an array of FEAT capabilities present on the server. Useful for parsing L<POE::Component::Client::SimpleFTP/feat> replies!
185  
186 On an error returns an empty array.
187  
188 =head1 SEE ALSO
189  
190 Please see those modules/websites for more information related to this module.
191  
192 =over 4
193  
194 =item *
195  
196 L<POE::Component::Client::SimpleFTP|POE::Component::Client::SimpleFTP>
197  
198 =back
199  
200 =head1 AUTHOR
201  
202 Apocalypse <APOCAL@cpan.org>
203  
204 =head1 COPYRIGHT AND LICENSE
205  
206 This software is copyright (c) 2011 by Apocalypse.
207  
208 This is free software; you can redistribute it and/or modify it under
209 the same terms as the Perl 5 programming language system itself.
210  
211 The full text of the license can be found in the LICENSE file included with this distribution.
212  
213 =head1 DISCLAIMER OF WARRANTY
214  
215 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
216 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
217 WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
218 PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
219 EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
220 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221 PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
222 SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME
223 THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
224  
225 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
226 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
227 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
228 TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR
229 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
230 SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
231 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
232 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
233 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
234 DAMAGES.
235  
236 =cut
237  
« Build ↑ Summary