Correios.pm

filter: Statementstmt 88.0% 88 / 100 Branchbran 55.2% 21 / 38 Conditioncond 43.5% 17 / 39 MC/DCmcdc 23.8% 5 / 21 Subroutinesub 88.2% 15 / 17 Podpod 66.6% 4 / 6 totaltotal 67.8% 150 / 221 CC 35 SCAR 26.0
CC
35
Cov
71%
access_token
29.4
_authorized_request
10.1
new
6.3
CRAP 13.4
« Correios/Empresas.pm ↑ Summary Correios/Manifestacao.pm »
1 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 8 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 $Carp::Internal{ 'Net::Correios' }++;
11  
12 package Net::Correios;
13  
14 8 our $VERSION = '0.005';
15  
16 8 my %libs = (
17 token => 'Token',
18 cep => 'CEP',
19 sro => 'SRO',
20 preco => 'Preco',
21 prazo => 'Prazo',
22 empresas => 'Empresas',
23 prepostagens => 'Prepostagens',
24 faturas => 'Faturas',
25 agencias => 'Agencias',
26 manifestacao => 'Manifestacao',
27 );
28  
29 # create lazy accessors.
30 8 foreach my $method (keys %libs) {
31 80 $Carp::Internal{ 'Net::Correios::' . $libs{$method} }++;
32 9 ▶ my $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
33 9 my ($self, %args) = @_;
34 9 my $internal_key = 'endpoint_' . $method;
35 9 ▶ if (!$self->{$internal_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$internal_key}) 9 0 9
36 9 my $module = 'Net/Correios/' . $libs{$method} . '.pm';
37 9 require $module;
38 9 my $class = 'Net::Correios::' . $libs{$method};
39 9 $self->{$internal_key} = $class->new( $self );
40 }
41 9 return $self->{$internal_key};
42 80 };
43 8 ▶ { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 80 *{"Net\::Correios\::$method"} = $sub;
45 }
46 }
47  
48 17 ▶sub new {
Subroutine new: called CC=6 SCAR=18.4
Pod documented
49 17 my ($class, %args) = @_;
50 Carp::croak 'Net::Correios::new - "username" and "password" are required'
51 17 ▶ unless $args{username} && $args{password};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"username"} and $args{"password"} 3 14 17
Condition100%3/3
$args{"username"} and $args{"password"}
!ll&&!rl&&r
2114
MC/DC100%2/2
$args{"username"} and $args{"password"}
$args{"username"} $args{"password"}
Truth table100%3/3
A and B
A= $args{"username"}B= $args{"password"}
ABresult
0X0
100
111
52 14 my $auth_basic = MIME::Base64::encode_base64($args{username} . ':' . $args{password}, '');
53 return bless {
54 debug => $args{debug},
55 contrato => $args{contrato},
56 cartao => $args{cartao},
57 sandbox => !!$args{sandbox},
58 token_ttl => $args{token_ttl} // 24 * 60 * 60,
59 token_renew_before => $args{token_renew_before} // 10 * 60,
60 base_url => 'https://api' . (('hom')x!!$args{sandbox}) . '.correios.com.br/',
61 auth_basic => $auth_basic,
62 agent => HTTP::Tiny->new(
63 default_headers => {
64 'Accept' => 'application/json',
65 'Content-Type' => 'application/json',
66 },
67 verify_SSL => 1,
68 14 ▶ timeout => $args{timeout} || 5,
Condition50%3/6
$args{"token_ttl"} // 86400
l!l
014
$args{"token_renew_before"} // 600
l!l
014
$args{'timeout'} || 5
l!l
014
MC/DC0%0/1
$args{"token_ttl"} // 86400
$args{"token_ttl"}
MC/DC0%0/1
$args{"token_renew_before"} // 600
$args{"token_renew_before"}
MC/DC0%0/1
$args{'timeout'} || 5
$args{'timeout'}
Truth table50%1/2
A
A= $args{"token_ttl"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"token_renew_before"}
Aresult
11
00
Truth table50%1/2
A
A= $args{'timeout'}
Aresult
11
00
69 ),
70 }, $class;
71 }
72  
73 2 ▶sub is_sandbox { $_[0]->{sandbox} }
Subroutine is_sandbox: called CC=1 SCAR=0.0
Pod documented
74  
75 20 ▶sub access_token {
Subroutine access_token: called CC=18 SCAR=33.8
Pod documented
76 20 my ($self, $token_type) = @_;
77 20 ▶ die 'token deve ser "cartao", "contrato" ou undef'
Branch: 1 branch
BranchTrueFalseTotal
if defined $token_type and $token_type ne "cartao" and $token_type ne "contrato" 0 20 20
Condition33%2/6
defined $token_type and $token_type ne "cartao" and $token_type ne "contrato"
!ll&&!rl&&r
2000
defined $token_type and $token_type ne "cartao"
!ll&&!rl&&r
0200
MC/DC0%0/3
defined $token_type and $token_type ne "cartao" and $token_type ne "contrato"
defined $token_type $token_type ne "cartao" $token_type ne "contrato"
Truth table25%1/4
A and B and C
A= defined $token_typeB= $token_type ne "cartao"C= $token_type ne "contrato"
ABCresult
0XX0
10X0
1100
1111
78 if defined $token_type && $token_type ne 'cartao' && $token_type ne 'contrato';
79  
80 20 ▶ my $suffix = defined $token_type ? '_' . $token_type : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $token_type ? : 20 0 20
81 20 my $storage_key = 'token' . $suffix;
82 20 my $expires_key = 'token_expires' . $suffix;
83  
84 20 ▶ if (defined $self->{$storage_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$storage_key}) 18 2 20
85 18 my $expires = $self->{$expires_key};
86 18 ▶ return $self->{$storage_key} if ( !$expires || time < $expires - $self->{token_renew_before} );
Branch: 1 branch
BranchTrueFalseTotal
if not $expires or time < $expires - $self->{"token_renew_before"} 17 1 18
Condition100%3/3
not $expires or time < $expires - $self->{"token_renew_before"}
l!l&&r!l&&!r
1521
MC/DC100%2/2
not $expires or time < $expires - $self->{"token_renew_before"}
not $expires time < $expires - $self->{"token_renew_before"}
Truth table100%3/3
not(A) or B
A= not $expiresB= time < $expires - $self->{"token_renew_before"}
ABresult
1X1
011
000
87 }
88  
89 my $token_res = $self->token->autentica(
90 3 ▶ (defined $token_type ? ($token_type => $self->{$token_type}) : ())
Branch: 1 branch
BranchTrueFalseTotal
defined $token_type ? : 3 0 3
91 );
92  
93 3 $self->{$storage_key} = $token_res->{token};
94  
95 # Correios tokens are JWTs with the real expiration date in epoch seconds.
96 3 ▶ my @jwt = split /\./, ($token_res->{token} // '');
Condition50%1/2
$token_res->{"token"} // ""
l!l
30
MC/DC0%0/1
$token_res->{"token"} // ""
$token_res->{"token"}
Truth table50%1/2
A
A= $token_res->{"token"}
Aresult
11
00
97 3 my $expires;
98 3 ▶ if (@jwt == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@jwt == 3) 2 1 3
99 2 my $payload = $jwt[1];
100 2 $payload =~ tr{-_}{+/}; # JWTs use base64url
101 2 $payload .= '=' x ((4 - length($payload) % 4) % 4);
102 2 my $claims = eval { JSON::decode_json(MIME::Base64::decode_base64($payload)) };
103 2 ▶ $expires = $claims->{exp} if ref $claims eq 'HASH' && $claims->{exp};
Branch: 1 branch
BranchTrueFalseTotal
if ref $claims eq "HASH" and $claims->{"exp"} 2 0 2
Condition33%1/3
ref $claims eq "HASH" and $claims->{"exp"}
!ll&&!rl&&r
002
MC/DC0%0/2
ref $claims eq "HASH" and $claims->{"exp"}
ref $claims eq "HASH" $claims->{"exp"}
Truth table33%1/3
A and B
A= ref $claims eq "HASH"B= $claims->{"exp"}
ABresult
0X0
100
111
104 }
105 3 ▶ $self->{$expires_key} = $expires // (time + $self->{token_ttl});
Condition100%2/2
$expires // time + $self->{"token_ttl"}
l!l
21
MC/DC100%1/1
$expires // time + $self->{"token_ttl"}
$expires
Truth table100%2/2
A
A= $expires
Aresult
11
00
106  
107 3 ▶ if ($token_res->{cartaoPostagem} && (!$self->{cartao} || $self->{cartao} eq $token_res->{cartaoPostagem}{numero})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_res->{"cartaoPostagem"} and !$self->{"cartao"} || $self->{"cartao"} eq $token_res->{"cartaoPostagem"}{"numero"}) 0 3 3
Condition16%1/6
$token_res->{"cartaoPostagem"} and !$self->{"cartao"} || $self->{"cartao"} eq $token_res->{"cartaoPostagem"}{"numero"}
!ll&&!rl&&r
300
not $self->{"cartao"} or $self->{"cartao"} eq $token_res->{"cartaoPostagem"}{"numero"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$token_res->{"cartaoPostagem"} and !$self->{"cartao"} || $self->{"cartao"} eq $token_res->{"cartaoPostagem"}{"numero"}
$token_res->{"cartaoPostagem"} not $self->{"cartao"} $self->{"cartao"} eq $token_res->{"cartaoPostagem"}{"numero"}
Truth table25%1/4
A and not(B) or C
A= $token_res->{"cartaoPostagem"}B= not $self->{"cartao"}C= $self->{"cartao"} eq $token_res->{"cartaoPostagem"}{"numero"}
ABCresult
0XX0
1000
11X1
1011
108 0 $self->{dr} = $token_res->{cartaoPostagem}{dr};
109 0 ▶ $self->{contrato} = $token_res->{cartaoPostagem}{contrato} if !$self->{contrato};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"contrato"} 0 0 0
110 }
111 3 return $self->{$storage_key};
112 }
113  
114 13 ▶sub make_request {
Subroutine make_request: called CC=5 SCAR=16.2
Pod undocumented
115 13 my ($self, $token_type, $method, $url, $args) = @_;
116 13 ▶ $args = {} if !defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 8 5 13
117 13 $url = $self->{base_url} . $url;
118  
119 13 my $res = $self->_authorized_request($token_type, $method, $url, $args);
120  
121 13 ▶ return $res unless ($res->{content} // '') =~ /GTW-007|token\s+expirado/i;
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"content"} // "") =~ /GTW-007|token\s+expirado/i 11 2 13
Condition50%1/2
$res->{"content"} // ""
l!l
130
MC/DC0%0/1
$res->{"content"} // ""
$res->{"content"}
Truth table50%1/2
A
A= $res->{"content"}
Aresult
11
00
122  
123 # cached token was refused, so the request was not processed. Retry!
124 2 ▶ my $suffix = defined $token_type ? '_' . $token_type : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $token_type ? : 2 0 2
125 2 delete $self->{'token' . $suffix};
126 2 delete $self->{'token_expires' . $suffix};
127 2 return $self->_authorized_request($token_type, $method, $url, $args);
128 }
129  
130 15 ▶sub _authorized_request {
Subroutine _authorized_request: called CC=5 SCAR=23.1
131 15 my ($self, $token_type, $method, $url, $args) = @_;
132 15 my $token = $self->access_token($token_type);
133 15 $args->{headers}{Authorization} = 'Bearer ' . $token;
134 15 my $res = $self->{agent}->request($method, $url, $args);
135  
136 15 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 15 15
137 $self->_debug(
138 "curl -X $method '$url' -H 'accept: application/json' -H 'Content-Type: application/json' -H 'Authorization: Bearer $token' " . (exists $args->{content} && defined $args->{content} && length($args->{content}) ? ("-d '" . $args->{content} . "'") : ''),
139 "Response: " . $res->{status} . "\n" . $res->{content}
140 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'content'} && defined $args->{'content'} && length $args->{'content'} ? : 0 0 0
Condition0%0/6
exists $args->{"content"} and defined $args->{"content"} and length $args->{"content"}
!ll&&!rl&&r
000
exists $args->{"content"} and defined $args->{"content"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $args->{"content"} and defined $args->{"content"} and length $args->{"content"}
exists $args->{"content"} defined $args->{"content"} length $args->{"content"}
Truth table0%0/4
A and B and C
A= exists $args->{"content"}B= defined $args->{"content"}C= length $args->{"content"}
ABCresult
0XX0
10X0
1100
1111
141 }
142  
143 15 return $res;
144 }
145  
146 12 ▶sub parse_response {
Subroutine parse_response: called CC=3 SCAR=15.7
Pod undocumented
147 12 my ($self, $res) = @_;
148 12 ▶ if ($res->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'success'}) { } 12 0 12
149 12 my $data = JSON::decode_json($res->{content});
150 12 return $data;
151 }
152 else {
153 0 my $error = $res->{status} . ' ' . $res->{reason};
154 0 ▶ $error .= ":\n" . $res->{content} if length $res->{content};
Branch: 1 branch
BranchTrueFalseTotal
if length $res->{"content"} 0 0 0
155 0 $error .= "\n " . $res->{url};
156 0 Carp::croak($error);
157 }
158 }
159  
160 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
161 0 my ($self, $debug) = @_;
162 0 ▶ $self->{debug} = $debug if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
163 0 return $self->{debug};
164 }
165  
166 0 ▶sub _debug {
Subroutine _debug: not called CC=1 SCAR=6.9
167 0 my ($self, @msg) = @_;
168 0 print STDERR '[' . __PACKAGE__ . ']' . join("\n", @msg);
169 }
170  
171 8 1;
172 __END__
« Correios/Empresas.pm ↑ Summary Correios/Manifestacao.pm »