File Coverage

blib/lib/WWW/Correios/CEP.pm
Criterion Covered Total %
statement 67 69 97.1
branch 19 28 67.8
condition 2 5 40.0
subroutine 10 11 90.9
pod 2 2 100.0
total 100 115 86.9


line stmt bran cond sub pod time code
1             package WWW::Correios::CEP;
2 2     2   142384 use strict;
  2         14  
  2         63  
3 2     2   10 use warnings;
  2         4  
  2         57  
4              
5 2     2   1446 use LWP::UserAgent;
  2         94262  
  2         81  
6 2     2   1541 use JSON;
  2         20681  
  2         13  
7              
8             our $VERSION = 1.042;
9              
10 2     2   1342 use Encode;
  2         29053  
  2         164  
11 2     2   17 use utf8;
  2         5  
  2         15  
12              
13             sub new {
14 2     2 1 198 my ( $class, $params ) = @_;
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://buscacepinter.correios.com.br/app/endereco/carrega-cep-endereco.php',
26              
27             _post_content => defined $params->{post_content}
28             ? $params->{post_content}
29 2 50 50     85 : '?pagina=%2Fapp%2Fendereco%2Findex.php&cepaux=&mensagem_alerta=&tipoCEP=LOG&endereco='
    100          
    50          
30             };
31              
32             $this->{_lwp_options}{timeout} = $params->{timeout}
33 2 100       12 if defined $params->{timeout};
34              
35 2         10 return bless $this, $class;
36             }
37              
38             sub find {
39 3     3 1 3087 my ( $this, $cep, $as_html_tree ) = @_;
40              
41 3         16 my @list_address = $this->_extractAddress( $cep, $as_html_tree );
42 3 50       17 $list_address[0]{address_count} = @list_address unless wantarray;
43              
44 3 50       21 return wantarray ? @list_address : $list_address[0];
45             }
46              
47             sub _extractAddress {
48 3     3   9 my ( $this, $cep, $as_html_tree ) = @_;
49              
50 3         8 my @result = ();
51              
52 3         23 $cep =~ s/[^\d]//go;
53 3         19 $cep = sprintf( '%08d', $cep );
54              
55 3 50 33     41 if ( $cep =~ /^00/o || $cep =~ /(\d)\1{7}/ ) {
56 0         0 $result[0]->{status} = "Error: Invalid CEP number ($cep)";
57             }
58             else {
59 3 100       19 if ( !defined $this->{_lwp_ua} ) {
60              
61 2         5 my $ua = LWP::UserAgent->new( %{ $this->{_lwp_options} } );
  2         27  
62 2         6203 $ua->agent( $this->{_user_agent} );
63 2         225 $ua->timeout( $this->{_lwp_options}{timeout} );
64 2         46 $this->{_lwp_ua} = $ua;
65             }
66 3         15 my $ua = $this->{_lwp_ua};
67              
68 3         17 my $url = $this->{_post_url} . $this->{_post_content} . $cep;
69 3         28 my $req = HTTP::Request->new( GET => $url );
70              
71 3         16059 eval {
72             local $SIG{ALRM} =
73 3     0   119 sub { die "Can't connect to server [alarm timeout]\n" };
  0         0  
74 3         40 alarm( $this->{_lwp_options}{timeout} + 1 );
75              
76             # Pass request to the user agent and get a response back
77 3         25 my $res = $ua->request($req);
78              
79             # Check the outcome of the response
80              
81 3 100       4086908 if ( $res->is_success ) {
82 2         34 $this->_parseJSON( \@result, $res->content, $as_html_tree );
83             }
84             else {
85 1         18 $result[0]->{status} = "Error: " . $res->status_line;
86             }
87             };
88 3         95 alarm(0);
89 3 50       56 die $@ if ($@);
90             }
91              
92 3 50       20 return wantarray ? @result : $result[0];
93             }
94              
95             sub _parseJSON {
96 2     2   36 my ( $this, $address_ref, $json, $as_html_tree ) = @_;
97              
98 2         15 my $obj = from_json($json);
99              
100 2 50       105 for my $p ( @{ $obj->{dados} || [] } ) {
  2         10  
101              
102 2 100       6 if ($as_html_tree) {
103 1         4 push( @$address_ref, $p );
104             }
105             else {
106 1         2 my $address = {};
107              
108 1         3 $address->{street} = $p->{logradouroDNEC};
109 1         3 $address->{neighborhood} = $p->{bairro};
110             $address->{cep} =
111 1         4 substr( $p->{cep}, 0, 5 ) . '-' . substr( $p->{cep}, 5, 3 );
112              
113 1         3 $address->{location} = $p->{localidade};
114 1         2 $address->{uf} = $p->{uf};
115              
116 1         3 $address->{status} = $p->{situacao};
117              
118 1         2 $address->{raw} = $p;
119              
120 1         3 push( @$address_ref, $address );
121             }
122             }
123              
124 2 50       7 $address_ref->[0]->{status} = 'Error: Address not found'
125             if ( !@$address_ref );
126              
127 2         69 return 1;
128             }
129              
130             1;
131             __END__