| 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
Condition50%1/2
$params->{'lwp_options'} || {'timeout', 30}
MC/DC0%0/1
$params->{'lwp_options'} || {'timeout', 30}
$params->{'lwp_options'}
Truth table50%1/2
A
A= $params->{'lwp_options'}
| |||||||||||||||||||||||||||||
| 30 | }; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | $this->{_lwp_options}{timeout} = $params->{timeout} | ||||||||||||||||||||||||||||
| 33 | 2 | ▶ | if defined $params->{timeout}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | ▶ | return wantarray ? @list_address : $list_address[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$cep =~ /^00/o or $cep =~ /(\d)\1{7}/
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}/
| |||||||||||||||||||||||||||||
| 56 | 0 | $result[0]->{status} = "Error: Invalid CEP number ($cep)"; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||
| 59 | 3 | ▶ | if ( !defined $this->{_lwp_ua} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | ▶ | return wantarray ? @result : $result[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ($as_html_tree) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 125 | if ( !@$address_ref ); | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | return 1; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 2 | 1; | |||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||