blib/lib/Net/Nakamap.pm

filter: Statementstmt 95.6% 66 / 69 Branchbran 72.2% 13 / 18 Conditioncond 50.0% 5 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 92.8% 13 / 14 Podpod 100% 4 / 4 totaltotal 84.3% 102 / 121 CC 15 SCAR 14.2
CC
15
Cov
87%
post
8.1
get
5.2
auth_code
3.3
CRAP 4.1
↑ Summary
1 package Net::Nakamap;
2 6 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = '0.03';
9  
10 has ua => (
11 is => 'rw',
12 lazy => 1,
13 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
14 1 return LWP::UserAgent->new(),
15 },
16 6 );
17  
18 6 has client_id => ( is => 'rw', isa => 'Str' );
19 6 has client_secret => ( is => 'rw', isa => 'Str' );
20  
21 has token => (
22 is => 'rw',
23 isa => 'Str',
24 lazy => 1,
25 0 ▶ default => sub { '' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 6 );
27  
28 has auth_ep => (
29 is => 'rw',
30 isa => 'Str',
31 lazy => 1,
32 1 ▶ default => sub { 'https://nakamap.com' },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 6 );
34  
35 has api_ep => (
36 is => 'rw',
37 isa => 'Str',
38 lazy => 1,
39 7 ▶ default => sub { 'https://thanks.nakamap.com' },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 6 );
41  
42 has last_error => (
43 is => 'rw',
44 isa => 'Str',
45 lazy => 1,
46 4 ▶ default => sub { '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 6 );
48  
49 1 ▶sub auth_uri {
Subroutine auth_uri: called CC=2 SCAR=7.0
Pod documented
50 1 my ($self, $params) = @_;
51  
52 1 my $uri = URI->new($self->auth_ep . '/dialog/oauth');
53 $uri->query_form(
54 client_id => $self->client_id,
55 response_type => $params->{response_type},
56 scope => $params->{scope},
57 1 ▶ ($params->{redirect_uri} ? (redirect_uri => $params->{redirect_uri}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$params->{'redirect_uri'} ? : 0 1 1
58 );
59  
60 1 return $uri;
61 }
62  
63 1 ▶sub auth_code {
Subroutine auth_code: called CC=3 SCAR=12.0
Pod documented
64 1 my ($self, $params) = @_;
65  
66 1 my $uri = URI->new($self->api_ep . '/oauth/access_token');
67 $uri->query_form(
68 client_id => $self->client_id,
69 client_secret => $self->client_secret,
70 grant_type => 'authorization_code',
71 code => $params->{code},
72 1 ▶ ($params->{redirect_uri} ? (redirect_uri => $params->{redirect_uri}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$params->{'redirect_uri'} ? : 0 1 1
73 );
74  
75 1 my $res = $self->ua->post($uri);
76  
77 1 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 1 0 1
78 1 return decode_json($res->content);
79 }
80 else {
81 0 $self->last_error($res->content);
82 0 return undef;
83 }
84 }
85  
86 2 ▶sub get {
Subroutine get: called CC=5 SCAR=16.6
Pod documented
87 2 my ($self, $path, $params) = @_;
88  
89 2 ▶ $params ||= {};
Condition50%1/2
$params ||= {}
l!l
02
MC/DC0%0/1
$params ||= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
90  
91 2 ▶ my $token = $params->{token} || $self->token;
Condition33%1/3
$params->{'token'} || $self->token
l!l&&r!l&&!r
020
MC/DC0%0/2
$params->{'token'} || $self->token
$params->{'token'} $self->token
Truth table33%1/3
A || B
A= $params->{'token'}B= $self->token
ABresult
1X1
011
000
92  
93 2 my $uri = URI->new($self->api_ep . $path);
94 2 ▶ $uri->query_form(
Branch: 1 branch
BranchTrueFalseTotal
$token ? : 2 0 2
95 ( $token ? ( token => $token ) : () ),
96 %$params,
97 );
98  
99 2 my $res = $self->ua->get($uri);
100  
101 2 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 1 1 2
102 1 return decode_json($res->content);
103 }
104 else {
105 1 $self->last_error($res->content);
106 1 return undef;
107 }
108 }
109  
110 4 ▶sub post {
Subroutine post: called CC=8 SCAR=20.9
Pod documented
111 4 my ($self, $path, $params, $files) = @_;
112  
113 4 ▶ $params ||= {};
Condition100%2/2
$params ||= {}
l!l
22
MC/DC100%1/1
$params ||= {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
114  
115 4 ▶ my $token = $params->{token} || $self->token;
Condition33%1/3
$params->{'token'} || $self->token
l!l&&r!l&&!r
040
MC/DC0%0/2
$params->{'token'} || $self->token
$params->{'token'} $self->token
Truth table33%1/3
A || B
A= $params->{'token'}B= $self->token
ABresult
1X1
011
000
116 4 ▶ $params->{token} = $token if $token;
Branch: 1 branch
BranchTrueFalseTotal
if $token 4 0 4
117  
118 4 my $res;
119  
120 4 ▶ if ($files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($files) { } 2 2 4
121 2 for my $name (keys %$files) {
122 2 my $file = $files->{$name};
123  
124 2 ▶ if (ref $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 1 1 2
125 1 $params->{$name} = [
126 undef,
127 'upload',
128 'Content-Type' => 'application/octet-stream',
129 'Content' => $$file,
130 ];
131 }
132 else {
133 1 $params->{$name} = [$files->{$name}];
134 }
135 }
136  
137 2 $res = $self->ua->request(
138 POST $self->api_ep . $path,
139 Content_Type => 'form-data',
140 Content => [%$params],
141 );
142 }
143 else {
144 2 my $uri = URI->new($self->api_ep . $path);
145 2 $uri->query_form(%$params);
146  
147 2 $res = $self->ua->post($uri);
148 }
149  
150 4 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 3 1 4
151 3 return decode_json($res->content);
152 }
153 else {
154 1 $self->last_error($res->content);
155 1 return undef;
156 }
157 }
158  
159 6 __PACKAGE__->meta->make_immutable();
160  
161 __END__
↑ Summary