blib/lib/WWW/Correios/CEP.pm

filter: Statementstmt 71.2% 52 / 73 Branchbran 46.4% 13 / 28 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 81.8% 9 / 11 Podpod 100% 2 / 2 totaltotal 63.9% 78 / 122 CC 17 SCAR 19.5
CC
17
Cov
63%
_parseHTML
20.0
_extractAddress
7.8
new
6.4
CRAP 7.0
↑ Summary
1 package WWW::Correios::CEP;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = 1.02;
9  
10 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub new {
14 2 ▶ my ( $class, $params ) = @_;
Subroutine new: called CC=6 SCAR=18.5
Pod documented
15  
16 my $this = {
17 _user_agent => defined $params->{user_agent}
18 ? $params->{user_agent}
19 : 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
20 _lwp_ua => undef,
21 _lwp_options => $params->{lwp_options} || { timeout => 30 },
22  
23 _post_url => defined $params->{post_url}
24 ? $params->{post_url}
25 : 'http://www.buscacep.correios.com.br/sistemas/buscacep/resultadoBuscaCepEndereco.cfm',
26  
27 _post_content => defined $params->{post_content}
28 ? $params->{post_content}
29 2 ▶ : 'tipoCEP=LOG&semelhante=N&relaxation='
Branch: 3 branches
BranchTrueFalseTotal
defined $params->{'user_agent'} ? : 0 2 2
defined $params->{'post_url'} ? : 1 1 2
defined $params->{'post_content'} ? : 0 2 2
Condition50%1/2
$params->{'lwp_options'} || {'timeout', 30}
l!l
02
MC/DC0%0/1
$params->{'lwp_options'} || {'timeout', 30}
$params->{'lwp_options'}
Truth table50%1/2
A
A= $params->{'lwp_options'}
Aresult
11
00
30 };
31  
32 $this->{_lwp_options}{timeout} = $params->{timeout}
33 2 ▶ if defined $params->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"timeout"} 1 1 2
34  
35 2 return bless $this, $class;
36 }
37  
38 sub find {
39 2 ▶ my ( $this, $cep ) = @_;
Subroutine find: called CC=3 SCAR=11.4
Pod documented
40  
41 2 my @list_address = $this->_extractAddress($cep);
42 2 ▶ $list_address[0]{address_count} = @list_address unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 2 0 2
43  
44 2 ▶ return wantarray ? @list_address : $list_address[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
45 }
46  
47 sub _extractAddress {
48 2 ▶ my ( $this, $cep ) = @_;
Subroutine _extractAddress: called CC=7 SCAR=20.5
49  
50 2 my @result = ();
51  
52 2 $cep =~ s/[^\d]//go;
53 2 $cep = sprintf( '%08d', $cep );
54  
55 2 ▶ if ( $cep =~ /^00/o || $cep =~ /(\d)\1{7}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cep =~ /^00/o or $cep =~ /(\d)\1{7}/) { } 0 2 2
Condition33%1/3
$cep =~ /^00/o or $cep =~ /(\d)\1{7}/
l!l&&r!l&&!r
002
MC/DC0%0/2
$cep =~ /^00/o or $cep =~ /(\d)\1{7}/
$cep =~ /^00/o $cep =~ /(\d)\1{7}/
Truth table33%1/3
A or B
A= $cep =~ /^00/oB= $cep =~ /(\d)\1{7}/
ABresult
1X1
011
000
56 0 $result[0]->{status} = "Error: Invalid CEP number ($cep)";
57 }
58 else {
59 2 ▶ if ( !defined $this->{_lwp_ua} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_lwp_ua"}) 2 0 2
60  
61 2 my $ua = LWP::UserAgent->new( %{ $this->{_lwp_options} } );
62 2 $ua->agent( $this->{_user_agent} );
63 2 $ua->timeout( $this->{_lwp_options}{timeout} );
64 2 $this->{_lwp_ua} = $ua;
65 }
66 2 my $ua = $this->{_lwp_ua};
67  
68 2 my $req = HTTP::Request->new( POST => $this->{_post_url} );
69 2 $req->content_type('application/x-www-form-urlencoded');
70 2 $req->content( $this->{_post_content} . $cep );
71 2 eval {
72 local $SIG{ALRM} =
73 2 ▶ sub { die "Can't connect to server [alarm timeout]\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
74 2 alarm( $this->{_lwp_options}{timeout} + 1 );
75  
76 # Pass request to the user agent and get a response back
77 2 my $res = $ua->request($req);
78  
79 # Check the outcome of the response
80 2 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 2 2
81 0 $this->_parseHTML( \@result, $res->content );
82 }
83 else {
84 2 $result[0]->{status} = "Error: " . $res->status_line;
85 }
86 };
87 2 alarm(0);
88 2 ▶ die $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
89 }
90  
91 2 ▶ return wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
92 }
93  
94 sub _parseHTML {
95 0 ▶ my ( $this, $address_ref, $html ) = @_;
Subroutine _parseHTML: not called CC=4 SCAR=30.0
96  
97 0 my $tree = HTML::TreeBuilder::XPath->new;
98 0 $html = decode( 'iso-8859-1', $html );
99 0 $html =~ s/&nbsp;//g; # <-- findvalue is keeping that
100 0 $tree->parse_content($html);
101  
102 0 my $ref =
103 $tree->findnodes('//table[contains(@class,"tmptabela")]/tr[not(th)]');
104  
105 0 ▶ while ( my $p = shift(@$ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = shift @$ref) 0 0 0
106 0 my $address = {};
107  
108 0 $address->{street} = $p->findvalue('./td[1]');
109 0 $address->{neighborhood} = $p->findvalue('./td[2]');
110 0 $address->{cep} = $p->findvalue('./td[4]');
111  
112 0 ( $address->{location}, $address->{uf} ) =
113 split qr{\s*/\s*} => $p->findvalue('./td[3]');
114  
115 0 ▶ if ( $address->{cep} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($address->{'cep'}) { } 0 0 0
116 0 $address->{status} = '';
117 }
118 else {
119 $address->{status} =
120 0 'Error: Address not found, something is wrong...';
121 }
122  
123 0 push( @$address_ref, $address );
124 }
125  
126 0 ▶ $address_ref->[0]->{status} = 'Error: Address not found'
Branch: 1 branch
BranchTrueFalseTotal
unless @$address_ref 0 0 0
127 if ( !@$address_ref );
128  
129 0 return 1;
130 }
131  
132 2 1;
133 __END__
↑ Summary