blib/lib/Net/Moip/V2.pm

filter: Statementstmt 87.8% 58 / 66 Branchbran 75.0% 9 / 12 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.2% 13 / 16 Podpod 83.3% 5 / 6 totaltotal 79.0% 87 / 110 SCAR 22.5
CC
9
Cov
82%
request_access_token
4.1
build_authorization_url
3.2
__ANON__
3.0
CRAP 9.5
↑ Summary blib/lib/Net/Moip/V2/Endpoint.pm »
1 package Net::Moip::V2;
2  
3 2 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Net::Moip::V2::Endpoint;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = "0.03";
13  
14 2 my $JSON = JSON->new->utf8;
15  
16 has 'ua', is => 'ro', default => sub {
17 2 ▶ Furl->new(
Subroutine __ANON__: called CC=1 SCAR=0.0
18 agent => "Net-Moip-V2/$VERSION",
19 timeout => 15,
20 max_redirects => 3,
21 # <perigrin> "SSL Wants a read first" I think is suggesting you
22 # haven't read OpenSSL a bedtime story in too long and perhaps
23 # it's feeling neglected and lonely?
24 # see also: https://metacpan.org/pod/IO::Socket::SSL#SNI-Support
25 # https://metacpan.org/pod/Furl#FAQ
26 # https://rt.cpan.org/Public/Bug/Display.html?id=86684
27 ssl_opts => {
28 SSL_verify_mode => SSL_VERIFY_PEER(),
29 # forcing version yields better error message:
30 SSL_version => 'TLSv1_2',
31 },
32 );
33 2 };
34  
35 2 has 'token', is => 'ro', required => 1;
36  
37 2 has 'key', is => 'ro', required => 1;
38  
39 2 has 'client_id', is => 'ro';
40  
41 2 has 'client_secret', is => 'ro';
42  
43 2 has 'access_token', is => 'rw';
44  
45  
46  
47 2 has 'api_url', (
48 is => 'ro',
49 writer => '_set_api_url',
50 default => 'https://api.moip.com.br/v2'
51 );
52  
53 2 has 'oauth_url', (
54 is => 'ro',
55 writer => '_set_oauth_url',
56 default => 'https://connect.moip.com.br'
57 );
58  
59  
60 has 'sandbox', (
61 is => 'rw',
62 default => 0,
63 trigger => sub {
64 4 ▶ my ($self, $sandbox) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
65 4 ▶ $self->_set_api_url( $sandbox
Branch: 1 branch
BranchTrueFalseTotal
$sandbox ? : 3 1 4
66 ? 'https://sandbox.moip.com.br/v2'
67 : 'https://api.moip.com.br/v2'
68 );
69 4 ▶ $self->_set_oauth_url( $sandbox
Branch: 1 branch
BranchTrueFalseTotal
$sandbox ? : 3 1 4
70 ? 'https://connect-sandbox.moip.com.br'
71 : 'https://connect.moip.com.br'
72 );
73 }
74 2 );
75  
76 has '_basic_auth_token', is => 'ro', lazy => 1, default => sub {
77 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
78 1 'Basic '.MIME::Base64::encode_base64( $self->token .':'. $self->key, '');
79 2 };
80  
81 sub build_authorization_url {
82 1 ▶ my ($self, $redirect_uri, $scope) = @_;
Subroutine build_authorization_url: called CC=3 SCAR=11.6
Pod documented
83 1 ▶ die 'Method signature is: build_authorization_url($redirect_uri, $scope)'
Branch: 1 branch
BranchTrueFalseTotal
unless $redirect_uri and $scope 0 1 1
Condition33%1/3
$redirect_uri and $scope
!ll&&!rl&&r
001
MC/DC0%0/2
$redirect_uri and $scope
$redirect_uri $scope
Truth table33%1/3
A and B
A= $redirect_uriB= $scope
ABresult
0X0
100
111
84 unless $redirect_uri && $scope;
85  
86 1 my $url = URI->new($self->oauth_url.'/oauth/authorize');
87 $url->query_form(
88 response_type => 'code',
89 client_id => $self->client_id,
90 redirect_uri => $redirect_uri,
91 1 scope => join(',', map { uc } @$scope)
92 );
93 1 $url;
94 }
95  
96 sub request_access_token {
97 2 ▶ my ($self, $redirect_uri, $code) = @_;
Subroutine request_access_token: called CC=4 SCAR=14.2
Pod documented
98 2 ▶ die 'Method signature is: request_access_token($redirect_uri, $code)'
Branch: 1 branch
BranchTrueFalseTotal
unless $redirect_uri and $code 0 2 2
Condition33%1/3
$redirect_uri and $code
!ll&&!rl&&r
002
MC/DC0%0/2
$redirect_uri and $code
$redirect_uri $code
Truth table33%1/3
A and B
A= $redirect_uriB= $code
ABresult
0X0
100
111
99 unless $redirect_uri && $code;
100  
101 2 my $uri = URI->new($self->oauth_url.'/oauth/token');
102 2 $uri->query_form(
103 client_id => $self->client_id,
104 client_secret => $self->client_secret,
105 grant_type => 'authorization_code',
106 redirect_uri => $redirect_uri,
107 code => $code
108 );
109  
110 2 my ($url, $body) = $uri->as_string =~ /(.*?)\?(.*)/;
111 2 my $res = $self->ua->post($url, [
112 'Content-Type' => 'application/x-www-form-urlencoded',
113 'Authentication' => $self->_basic_auth_token,
114 'Cache-Control:' => 'no-cache'
115 ], $body);
116  
117 2 ▶ return { error => $res->status_line } unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 1 1 2
118 1 my $data = $JSON->decode($res->content);
119 }
120  
121  
122  
123  
124 sub endpoint {
125 5 ▶ my ($self, $path) = @_;
Subroutine endpoint: called CC=2 SCAR=7.0
Pod documented
126 5 ▶ die "Syntax: moip->endpoint(<name>)" unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 5 5
127 Net::Moip::V2::Endpoint->new(
128 path => $path,
129 5 map { $_ => $self->$_ } qw/ ua api_url token key client_id client_secret /
130 );
131 }
132  
133 sub get {
134 0 ▶ my $self = shift;
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
135 0 my $endpoint = shift;
136 0 $self->endpoint($endpoint)->get(@_);
137 }
138  
139 sub post {
140 0 ▶ my $self = shift;
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
141 0 my $endpoint = shift;
142 0 $self->endpoint($endpoint)->post(@_);
143 }
144  
145  
146 sub decode_json {
147 0 ▶ my $self = shift;
Subroutine decode_json: not called CC=1 SCAR=6.9
Pod undocumented
148 0 $JSON->decode($_[0]);
149 }
150  
151  
152  
153  
154 2 1;
155 __END__
↑ Summary blib/lib/Net/Moip/V2/Endpoint.pm »