Transpose/EmailValid.pm

filter: Statementstmt 98.1% 53 / 54 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 12 / 13 Podpod 100% 5 / 5 totaltotal 95.0% 76 / 80 CC 6 SCAR 6.1
CC
6
Cov
95%
suggestion
3.0
is_valid
3.0
email
2.0
CRAP 1.8
« Transpose/Iterator/Scalar.pm ↑ Summary Transpose/Field.pm »
1 package Data::Transpose::EmailValid;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Email::Valid;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 extends 'Data::Transpose::Validator::Base';
8 6 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Data::Transpose::EmailValid - Perl extension to check if a mail is valid (with some autocorrection)
14  
15 =head1 SYNOPSIS
16  
17 use Data::Transpose::EmailValid;
18  
19 my $email = Data::Transpose::EmailValid->new;
20  
21 ok($email->is_valid("user@domain.tld"), "Mail is valid");
22  
23 ok(!$email->is_valid("user_e;@domain.tld"), "Mail is not valid");
24  
25 warn $email->reason; # output the reason of the failure
26  
27 =head1 DESCRIPTION
28  
29 This module check if the mail is valid, using the L<Email::Valid>
30 module. It also provides some additional methods.
31  
32 =head2 AUTO CORRECTION
33  
34 This validator corrects common mistakes automatically:
35  
36 =over 4
37  
38 =item
39  
40 C<.ocm> instead of C<.com> as top level domain for C<aol.com>,
41 C<gmail.com>, C<hotmail.com> and C<yahoo.com>, e.g. C<tp@gmail.ocm>.
42  
43 =item
44  
45 Double dots before top level domain, e.g. C<tp@linuxia..de>.
46  
47 =back
48  
49 Please suggest further auto correction examples to us.
50  
51 =head1 METHODS
52  
53 =head2 new
54  
55 Constructor. It doesn't accept any arguments.
56  
57 =cut
58  
59 has _email_valid => (is => 'ro',
60 isa => Object,
61 46 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 46 return Email::Valid->new(
63 -fudge => 1,
64 -mxcheck => 1,
65 );
66 6 });
67  
68 6 has input => (is => 'rwp',
69 isa => Maybe[Str]);
70  
71 6 has output => (is => 'rwp',
72 isa => Maybe[Str]);
73  
74  
75 =head2 input
76  
77 Accessor to the input email string.
78  
79 =head2 output
80  
81 Accessor to the output email string.
82  
83 =head2 reset_all
84  
85 Clear all the internal data
86  
87 =cut
88  
89  
90 31 ▶sub reset_all {
Subroutine reset_all: called CC=1 SCAR=0.0
Pod documented
91 31 my $self = shift;
92 31 $self->reset_errors;
93 31 $self->_set_input(undef);
94 31 $self->_set_output(undef);
95 }
96  
97 =head2 $obj->is_valid($emailstring);
98  
99 Returns the email passed if valid, false underwise.
100  
101 =cut
102  
103  
104 31 ▶sub is_valid {
Subroutine is_valid: called CC=3 SCAR=11.0
Pod documented
105 31 ▶ return if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 31 31
106  
107 31 my ($self, $email) = @_;
108  
109 # overwrite old data
110 31 $self->reset_all;
111  
112 31 $self->_set_input($email);
113  
114 # correct common typos # Maybe add an option for this?
115 31 $email = $self->_autocorrect;
116  
117 # do validation
118 31 $email = $self->_email_valid->address($email);
119 31 ▶ unless ($email) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email) 12 19 31
120 12 $self->error($self->_email_valid->details);
121 12 return;
122 }
123  
124 19 $self->_set_output($email);
125 19 return $email;
126 }
127  
128 =head2 $obj->email
129  
130 Returns the last checked email.
131  
132 =cut
133  
134 0 ▶sub email { shift->output }
Subroutine email: not called CC=1 SCAR=6.9
Pod documented
135  
136 =head2 $obj->reason
137  
138 Returns the reason of the failure of the last check, false if it was
139 successful.
140  
141 =cut
142  
143  
144 6 ▶sub reason { shift->error }
Subroutine reason: called CC=1 SCAR=0.0
Pod documented
145  
146 =head2 $obj->suggestion
147  
148 This module implements some basic autocorrection. Calling ->suggestion
149 after a successfull test, will return the suggested value if the input
150 was different from the output, false otherwise.
151  
152 =cut
153  
154 8 ▶sub suggestion {
Subroutine suggestion: called CC=3 SCAR=11.0
Pod documented
155 8 my ($self) = @_;
156 8 ▶ return if $self->error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->error 0 8 8
157  
158 8 ▶ if ($self->input ne $self->output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->input ne $self->output) 4 4 8
159 4 return $self->output;
160 }
161  
162 4 return;
163 }
164  
165  
166 31 ▶sub _autocorrect {
Subroutine _autocorrect: called CC=2 SCAR=6.9
167 31 my $self = shift;
168 31 my $email = $self->input;
169 # trim
170 31 $email =~ s/^\s+//;
171 31 $email =~ s/\s+$//;
172 # .ocm -> .com
173 31 foreach (qw/aol gmail hotmail yahoo/) {
174 124 $email =~ s/\b$_\.ocm$/$_.com/;
175 }
176 # double dots in domain part
177 31 $email =~ s/\.\.(\w+)$/.$1/;
178  
179 # setting the error breaks the retrocompatibility
180 # $self->error("typo?");
181 31 return $email;
182 }
183  
184 =head1 AUTHOR
185  
186 Uwe Voelker <uwe@uwevoelker.de>
187  
188 =head1 LICENSE AND COPYRIGHT
189  
190 Copyright 2012-2016 Uwe Voelker <uwe@uwevoelker.de>.
191  
192 This program is free software; you can redistribute it and/or modify it
193 under the terms of either: the GNU General Public License as published
194 by the Free Software Foundation; or the Artistic License.
195  
196 See http://dev.perl.org/licenses/ for more information.
197  
198 =cut
199  
200  
201 6 1;
202  
« Transpose/Iterator/Scalar.pm ↑ Summary Transpose/Field.pm »