| 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
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 | 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
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | ▶ | return wantarray ? @list_address : $list_address[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
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 | 2 | ▶ | 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 | 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2 | ▶ | return wantarray ? @result : $result[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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/ //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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 127 | if ( !@$address_ref ); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | return 1; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 2 | 1; | |||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||