Correios.pm

filter: Statementstmt 86.1% 62 / 72 Branchbran 40.9% 9 / 22 Conditioncond 35.2% 6 / 17 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 14 / 14 Podpod 60.0% 3 / 5 totaltotal 69.0% 96 / 139 CC 18 SCAR 31.1
CC
18
Cov
69%
access_token
59.1
parse_response
4.8
new
4.0
CRAP 22.5
« Correios/Prepostagens.pm ↑ Summary Correios/Preco.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.003';
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 my $sub = sub {
33 6 ▶ my ($self, %args) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
34 6 my $internal_key = 'endpoint_' . $method;
35 6 ▶ if (!$self->{$internal_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$internal_key}) 6 0 6
36 6 my $module = 'Net/Correios/' . $libs{$method} . '.pm';
37 6 require $module;
38 6 my $class = 'Net::Correios::' . $libs{$method};
39 6 $self->{$internal_key} = $class->new( $self );
40 }
41 6 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 sub new {
49 11 ▶ my ($class, %args) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
50 Carp::croak 'Net::Correios::new - "username" and "password" are required'
51 11 ▶ unless $args{username} && $args{password};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"username"} and $args{"password"} 3 8 11
Condition100%3/3
$args{"username"} and $args{"password"}
!ll&&!rl&&r
218
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 8 my $auth_basic = MIME::Base64::encode_base64($args{username} . ':' . $args{password}, '');
53 return bless {
54 contrato => $args{contrato},
55 cartao => $args{cartao},
56 sandbox => !!$args{sandbox},
57 base_url => 'https://api' . (('hom')x!!$args{sandbox}) . '.correios.com.br/',
58 auth_basic => $auth_basic,
59 agent => HTTP::Tiny->new(
60 default_headers => {
61 'Accept' => 'application/json',
62 'Content-Type' => 'application/json',
63 },
64 verify_SSL => 1,
65 8 ▶ timeout => $args{timeout} || 5,
Condition50%1/2
$args{'timeout'} || 5
l!l
08
MC/DC0%0/1
$args{'timeout'} || 5
$args{'timeout'}
Truth table50%1/2
A
A= $args{'timeout'}
Aresult
11
00
66 ),
67 }, $class;
68 }
69  
70 2 ▶sub is_sandbox { $_[0]->{sandbox} }
Subroutine is_sandbox: called CC=1 SCAR=0.0
Pod documented
71  
72 sub access_token {
73 10 ▶ my ($self, $token_type) = @_;
Subroutine access_token: called CC=11 SCAR=40.8
Pod documented
74 10 ▶ 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 10 10
Condition33%2/6
defined $token_type and $token_type ne "cartao" and $token_type ne "contrato"
!ll&&!rl&&r
1000
defined $token_type and $token_type ne "cartao"
!ll&&!rl&&r
0100
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
75 if defined $token_type && $token_type ne 'cartao' && $token_type ne 'contrato';
76  
77 10 ▶ my $storage_key = 'token' . (defined $token_type ? '_' . $token_type : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $token_type ? : 10 0 10
78 10 ▶ return $self->{$storage_key} if defined $self->{$storage_key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$storage_key} 10 0 10
79  
80 my $token_res = $self->token->autentica(
81 0 ▶ (defined $token_type ? ($token_type => $self->{$token_type}) : ())
Branch: 1 branch
BranchTrueFalseTotal
defined $token_type ? : 0 0 0
82 );
83  
84 0 $self->{$storage_key} = $token_res->{token};
85 0 ▶ 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 0 0
Condition0%0/6
$token_res->{"cartaoPostagem"} and !$self->{"cartao"} || $self->{"cartao"} eq $token_res->{"cartaoPostagem"}{"numero"}
!ll&&!rl&&r
000
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 table0%0/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
86 0 $self->{dr} = $token_res->{cartaoPostagem}{dr};
87 0 ▶ $self->{contrato} = $token_res->{cartaoPostagem}{contrato} if !$self->{contrato};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"contrato"} 0 0 0
88 }
89 0 return $self->{$storage_key};
90 }
91  
92 sub make_request {
93 8 ▶ my ($self, $token_type, $method, $url, $args) = @_;
Subroutine make_request: called CC=2 SCAR=6.9
Pod undocumented
94 8 ▶ $args = {} if !defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 3 5 8
95 8 $url = $self->{base_url} . $url;
96 8 my $token = $self->access_token($token_type);
97 8 $args->{headers}{Authorization} = 'Bearer ' . $token;
98 8 my $res = $self->{agent}->request($method, $url, $args);
99 8 return $res;
100 }
101  
102 sub parse_response {
103 9 ▶ my ($self, $res) = @_;
Subroutine parse_response: called CC=3 SCAR=15.7
Pod undocumented
104 9 ▶ if ($res->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'success'}) { } 9 0 9
105 9 my $data = JSON::decode_json($res->{content});
106 9 return $data;
107 }
108 else {
109 0 my $error = $res->{status} . ' ' . $res->{reason};
110 0 ▶ $error .= ":\n" . $res->{content} if length $res->{content};
Branch: 1 branch
BranchTrueFalseTotal
if length $res->{"content"} 0 0 0
111 0 $error .= "\n " . $res->{url};
112 0 Carp::croak($error);
113 }
114 }
115  
116 8 1;
117 __END__
« Correios/Prepostagens.pm ↑ Summary Correios/Preco.pm »