blib/lib/WWW/Correios/CEP.pm

filter: Statementstmt 71.8% 51 / 71 Branchbran 50.0% 14 / 28 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 81.8% 9 / 11 Podpod 100% 2 / 2 totaltotal 65.0% 78 / 120 CC 18 SCAR 22.2
CC
18
Cov
64%
_parseJSON
30.0
_extractAddress
7.6
new
6.4
CRAP 9.2
↑ 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 JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = 1.04;
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 : 'https://buscacep.correios.com.br/app/endereco/carrega-cep-endereco.php',
26  
27 _post_content => defined $params->{post_content}
28 ? $params->{post_content}
29 2 ▶ : '?pagina=%2Fapp%2Fendereco%2Findex.php&cepaux=&mensagem_alerta=&tipoCEP=LOG&endereco='
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 3 ▶ my ( $this, $cep, $as_html_tree ) = @_;
Subroutine find: called CC=3 SCAR=11.4
Pod documented
40  
41 3 my @list_address = $this->_extractAddress( $cep, $as_html_tree );
42 3 ▶ $list_address[0]{address_count} = @list_address unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 3 0 3
43  
44 3 ▶ return wantarray ? @list_address : $list_address[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
45 }
46  
47 sub _extractAddress {
48 3 ▶ my ( $this, $cep, $as_html_tree ) = @_;
Subroutine _extractAddress: called CC=7 SCAR=20.3
49  
50 3 my @result = ();
51  
52 3 $cep =~ s/[^\d]//go;
53 3 $cep = sprintf( '%08d', $cep );
54  
55 3 ▶ if ( $cep =~ /^00/o || $cep =~ /(\d)\1{7}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cep =~ /^00/o or $cep =~ /(\d)\1{7}/) { } 0 3 3
Condition33%1/3
$cep =~ /^00/o or $cep =~ /(\d)\1{7}/
l!l&&r!l&&!r
003
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 3 ▶ if ( !defined $this->{_lwp_ua} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"_lwp_ua"}) 2 1 3
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 3 my $ua = $this->{_lwp_ua};
67  
68 3 my $url = $this->{_post_url} . $this->{_post_content} . $cep;
69 3 my $req = HTTP::Request->new( GET => $url );
70  
71 3 eval {
72 local $SIG{ALRM} =
73 3 ▶ sub { die "Can't connect to server [alarm timeout]\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
74 3 alarm( $this->{_lwp_options}{timeout} + 1 );
75  
76 # Pass request to the user agent and get a response back
77 3 my $res = $ua->request($req);
78  
79 # Check the outcome of the response
80  
81 3 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 3 3
82 0 $this->_parseJSON( \@result, $res->content, $as_html_tree );
83 }
84 else {
85 3 $result[0]->{status} = "Error: " . $res->status_line;
86 }
87 };
88 3 alarm(0);
89 3 ▶ die $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
90 }
91  
92 3 ▶ return wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
93 }
94  
95 sub _parseJSON {
96 0 ▶ my ( $this, $address_ref, $json, $as_html_tree ) = @_;
Subroutine _parseJSON: not called CC=5 SCAR=34.0
97  
98 0 my $obj = from_json($json);
99  
100 0 ▶ for my $p ( @{ $obj->{dados} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$obj->{"dados"} or [] 0 0 0
101  
102 0 ▶ if ($as_html_tree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as_html_tree) { } 0 0 0
103 0 push( @$address_ref, $p );
104 }
105 else {
106 0 my $address = {};
107  
108 0 $address->{street} = $p->{logradouroDNEC};
109 0 $address->{neighborhood} = $p->{bairro};
110 $address->{cep} =
111 0 substr( $p->{cep}, 0, 5 ) . '-' . substr( $p->{cep}, 5, 3 );
112  
113 0 $address->{location} = $p->{localidade};
114 0 $address->{uf} = $p->{uf};
115  
116 0 $address->{status} = $p->{situacao};
117  
118 0 $address->{raw} = $p;
119  
120 0 push( @$address_ref, $address );
121 }
122 }
123  
124 0 ▶ $address_ref->[0]->{status} = 'Error: Address not found'
Branch: 1 branch
BranchTrueFalseTotal
unless @$address_ref 0 0 0
125 if ( !@$address_ref );
126  
127 0 return 1;
128 }
129  
130 2 1;
131 __END__
↑ Summary