File Coverage

blib/lib/Net/Correios/Token.pm
Criterion Covered Total %
statement 23 28 82.1
branch 4 10 40.0
condition 0 3 0.0
subroutine 6 6 100.0
pod 0 2 0.0
total 33 49 67.3


line stmt bran cond sub pod time code
1 1     1   12 use strict;
  1         1  
  1         31  
2 1     1   3 use warnings;
  1         5  
  1         33  
3 1     1   3 use Scalar::Util ();
  1         1  
  1         28  
4 1     1   3 use JSON ();
  1         2  
  1         278  
5              
6             package Net::Correios::Token;
7              
8             sub new {
9 1     1 0 3 my ($class, $parent) = @_;
10              
11 1         2 Scalar::Util::weaken($parent);
12 1         7 return bless { parent => $parent }, $class;
13             }
14              
15             sub autentica {
16 1     1 0 260 my ($self, %args) = @_;
17 1         1 my $parent = $self->{parent};
18              
19 1         2 my $endpoint_url = $parent->{base_url} . 'token/v1/autentica';
20 1         1 my $json_body;
21             # TODO: precisamos entender quais endpoints funcionam com quais tokens.
22             # muito provavelmente precisaremos de ambos ao mesmo tempo.
23 1 50       4 if ($args{contrato}) {
    50          
24 0         0 $endpoint_url .= '/contrato';
25 0         0 $json_body = JSON::encode_json({ numero => $args{contrato} });
26             }
27             elsif ($args{cartao}) {
28 0         0 $endpoint_url .= '/cartaopostagem';
29 0         0 $json_body = JSON::encode_json({ numero => $args{cartao} });
30             }
31              
32             my $res = $parent->{agent}->request(
33             'POST',
34             $endpoint_url,
35             {
36             headers => { 'Authorization' => 'Basic ' . $parent->{auth_basic} },
37 1 50       5 (defined $json_body? (content => $json_body) : ()),
38             },
39             );
40 1 50       1319 if ($parent->{debug}) {
41             $parent->_debug(
42             "curl -X POST '$endpoint_url' -H 'accept: application/json' -H 'Content-Type: application/json' -H 'Authorization: Basic $parent->{auth_basic}' " . ((defined $json_body && length($json_body)) ? ("-d '" . $json_body . "'") : ''),
43             "Response: " . $res->{status} . "\n" . $res->{content}
44 0 0 0     0 );
45             }
46 1         5 return $parent->parse_response($res);
47             }
48              
49             1;