Google.pm

filter: Statementstmt 32.0% 17 / 53 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 33.3% 5 / 15 Podpod 20.0% 2 / 10 totaltotal 25.0% 24 / 96 CC 7 SCAR 22.2
CC
7
Cov
25%
api_query
30.0
new
6.0
refresh_access_token_silent
6.0
CRAP 9.2
↑ Summary Google/GCal.pm »
1 package API::Google;
2 1 $API::Google::VERSION = '0.09';
3 # ABSTRACT: Perl library for easy access to Google services via their API
4  
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Config::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 sub new {
14 0 ▶ my ($class, $params) = @_;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
15 0 my $h = {};
16 0 ▶ if ($params->{tokensfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'tokensfile'}) { } 0 0 0
17 0 $h->{tokensfile} = Config::JSON->new($params->{tokensfile});
18 } else {
19 0 die 'no json file specified!';
20 }
21 0 $h->{ua} = Mojo::UserAgent->new();
22 0 return bless $h, $class;
23 }
24  
25  
26  
27 sub refresh_access_token {
28 0 ▶ my ($self, $params) = @_;
Subroutine refresh_access_token: not called CC=1 SCAR=6.9
Pod undocumented
29 0 warn Dumper $params;
30 0 $params->{grant_type} = 'refresh_token';
31 0 $self->{ua}->post('https://www.googleapis.com/oauth2/v4/token' => form => $params)->res->json; # tokens
32 };
33  
34  
35 sub client_id {
36 0 ▶ shift->{tokensfile}->get('gapi/client_id');
Subroutine client_id: not called CC=1 SCAR=6.9
Pod undocumented
37 }
38  
39 sub ua {
40 0 ▶ shift->{ua};
Subroutine ua: not called CC=1 SCAR=6.9
Pod undocumented
41 }
42  
43  
44 sub client_secret {
45 0 ▶ shift->{tokensfile}->get('gapi/client_secret');
Subroutine client_secret: not called CC=1 SCAR=6.9
Pod undocumented
46 }
47  
48  
49  
50 sub refresh_access_token_silent {
51 0 ▶ my ($self, $user) = @_;
Subroutine refresh_access_token_silent: not called CC=2 SCAR=17.9
Pod documented
52 0 my $tokens = $self->refresh_access_token({
53 client_id => $self->client_id,
54 client_secret => $self->client_secret,
55 refresh_token => $self->get_refresh_token_from_storage($user)
56 });
57 0 my $res = {};
58 0 $res->{old} = $self->get_access_token_from_storage($user);
59 0 warn Dumper $tokens;
60 0 ▶ if ($tokens->{access_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens->{"access_token"}) 0 0 0
61 0 $self->set_access_token_to_storage($user, $tokens->{access_token});
62 }
63 0 $res->{new} = $self->get_access_token_from_storage($user);
64 0 return $res;
65 };
66  
67  
68 sub get_refresh_token_from_storage {
69 0 ▶ my ($self, $user) = @_;
Subroutine get_refresh_token_from_storage: not called CC=1 SCAR=6.9
Pod undocumented
70 0 $self->{tokensfile}->get('gapi/tokens/'.$user.'/refresh_token');
71 };
72  
73 sub get_access_token_from_storage {
74 0 ▶ my ($self, $user) = @_;
Subroutine get_access_token_from_storage: not called CC=1 SCAR=6.9
Pod undocumented
75 0 $self->{tokensfile}->get('gapi/tokens/'.$user.'/access_token');
76 };
77  
78 sub set_access_token_to_storage {
79 0 ▶ my ($self, $user, $token) = @_;
Subroutine set_access_token_to_storage: not called CC=1 SCAR=6.9
Pod undocumented
80 0 $self->{tokensfile}->set('gapi/tokens/'.$user.'/access_token', $token);
81 };
82  
83  
84  
85  
86 sub api_query {
87 0 ▶ my ($self, $params, $payload) = @_;
Subroutine api_query: not called CC=5 SCAR=34.0
Pod documented
88 my %headers = (
89 'Authorization' => 'Bearer '.$self->get_access_token_from_storage($params->{user})
90 0 );
91 0 my $http_method = $params->{method};
92 0 ▶ if ($http_method eq 'get' || $http_method eq 'delete') {
Branch: 2 branches
BranchTrueFalseTotal
if ($http_method eq 'get' or $http_method eq 'delete') { } 0 0 0
elsif ($http_method eq 'post' and $payload) { } 0 0 0
Condition0%0/3
$http_method eq "get" or $http_method eq "delete"
l!l&&r!l&&!r
000
MC/DC0%0/2
$http_method eq "get" or $http_method eq "delete"
$http_method eq "get" $http_method eq "delete"
Truth table0%0/3
A or B
A= $http_method eq "get"B= $http_method eq "delete"
ABresult
1X1
011
000
93 0 return $self->{ua}->$http_method($params->{route} => \%headers)->res->json;
94 ▶ } elsif (($http_method eq 'post') && $payload) {
Condition0%0/3
$http_method eq "post" and $payload
!ll&&!rl&&r
000
MC/DC0%0/2
$http_method eq "post" and $payload
$http_method eq "post" $payload
Truth table0%0/3
A and B
A= $http_method eq "post"B= $payload
ABresult
0X0
100
111
95 0 return $self->{ua}->$http_method($params->{route} => \%headers => json => $payload)->res->json;
96 } else {
97 0 die 'wrong http_method';
98 }
99 };
100  
101  
102 1 1;
103  
104 __END__
↑ Summary Google/GCal.pm »