| 1 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use MIME::Base64 (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use JSON (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | $Carp::Internal{ 'Net::Correios' }++; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | package Net::Correios; | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 7 | 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 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # create lazy accessors. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 7 | foreach my $method (keys %libs) { | |||||||||||||||||||||||||||||||||||||||||
| 30 | 63 | $Carp::Internal{ 'Net::Correios::' . $libs{$method} }++; | |||||||||||||||||||||||||||||||||||||||||
| 31 | my $sub = sub { | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 5 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 5 | my $internal_key = 'endpoint_' . $method; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 5 | ▶ | if (!$self->{$internal_key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 5 | my $module = 'Net/Correios/' . $libs{$method} . '.pm'; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 5 | require $module; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 5 | my $class = 'Net::Correios::' . $libs{$method}; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | $self->{$internal_key} = $class->new( $self ); | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | return $self->{$internal_key}; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 63 | }; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 7 | ▶ | { no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 63 | *{"Net\::Correios\::$method"} = $sub; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 10 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | Carp::croak 'Net::Correios::new - "username" and "password" are required' | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 10 | ▶ | unless $args{username} && $args{password}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args{"username"} and $args{"password"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 7 | my $auth_basic = MIME::Base64::encode_base64($args{username} . ':' . $args{password}, ''); | |||||||||||||||||||||||||||||||||||||||||
| 52 | return bless { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | contrato => $args{contrato}, | ||||||||||||||||||||||||||||||||||||||||||
| 54 | cartao => $args{cartao}, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | sandbox => !!$args{sandbox}, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | base_url => 'https://api' . (('hom')x!!$args{sandbox}) . '.correios.com.br/', | ||||||||||||||||||||||||||||||||||||||||||
| 57 | auth_basic => $auth_basic, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | agent => HTTP::Tiny->new( | ||||||||||||||||||||||||||||||||||||||||||
| 59 | default_headers => { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 'Accept' => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 'Content-Type' => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | verify_SSL => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 7 | ▶ | timeout => $args{timeout} || 5, | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'timeout'} || 5
MC/DC0%0/1
$args{'timeout'} || 5
$args{'timeout'}
Truth table50%1/2
A
A= $args{'timeout'}
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 66 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | sub is_sandbox { $_[0]->{sandbox} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_sandbox: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | sub access_token { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | ▶ | my ($self, $token_type) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
access_token: called CC=11 SCAR=40.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | die 'token deve ser "cartao", "contrato" ou undef' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $token_type and $token_type ne "cartao" and $token_type ne "contrato"
defined $token_type and $token_type ne "cartao"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | if defined $token_type && $token_type ne 'cartao' && $token_type ne 'contrato'; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 8 | ▶ | my $storage_key = 'token' . (defined $token_type ? '_' . $token_type : ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 8 | ▶ | return $self->{$storage_key} if defined $self->{$storage_key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | my $token_res = $self->token->autentica( | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | (defined $token_type ? ($token_type => $self->{$token_type}) : ()) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | $self->{$storage_key} = $token_res->{token}; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ($token_res->{cartaoPostagem} && (!$self->{cartao} || $self->{cartao} eq $token_res->{cartaoPostagem}{numero})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$token_res->{"cartaoPostagem"} and !$self->{"cartao"} || $self->{"cartao"} eq $token_res->{"cartaoPostagem"}{"numero"}
not $self->{"cartao"} or $self->{"cartao"} eq $token_res->{"cartaoPostagem"}{"numero"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $self->{dr} = $token_res->{cartaoPostagem}{dr}; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $self->{contrato} = $token_res->{cartaoPostagem}{contrato} if !$self->{contrato}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | return $self->{$storage_key}; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | sub make_request { | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 7 | ▶ | my ($self, $token_type, $method, $url, $args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
make_request: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 7 | ▶ | $args = {} if !defined $args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 7 | $url = $self->{base_url} . $url; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 7 | my $token = $self->access_token($token_type); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 7 | $args->{headers}{Authorization} = 'Bearer ' . $token; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 7 | my $res = $self->{agent}->request($method, $url, $args); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 7 | return $res; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | sub parse_response { | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 8 | ▶ | my ($self, $res) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_response: called CC=3 SCAR=15.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 8 | ▶ | if ($res->{success}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 8 | my $data = JSON::decode_json($res->{content}); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 8 | return $data; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my $error = $res->{status} . ' ' . $res->{reason}; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $error .= ":\n" . $res->{content} if length $res->{content}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | $error .= "\n " . $res->{url}; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | Carp::croak($error); | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||||||||||||||||||||||||