lib/Net/Async/Spotify/Token.pm

filter: Statementstmt 33.3% 15 / 45 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 30.7% 4 / 13 Podpod 88.8% 8 / 9 totaltotal 29.0% 27 / 93 CC 14 SCAR 22.1
CC
14
Cov
22%
new
12.0
renew
12.0
access_token
12.0
CRAP 9.1
« lib/Net/Async/Spotify/Util.pm ↑ Summary script/spotify-cli.pl »
1 package Net::Async::Spotify::Token;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.002'; # VERSION
7 2 our $AUTHORITY = 'cpan:VNEALV'; # AUTHORITY
8  
9 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =encoding utf8
13  
14 =head1 NAME
15  
16 Net::Async::Spotify::Token - Representation for Spotify Token Object
17  
18 =head1 SYNOPSIS
19  
20 use Net::Async::Spotify::Token;
21  
22 my $token = Net::Async::Spotify::Token->new(
23 access_token => "NgCXRK...MzYjw",
24 token_type => "Bearer",
25 scope => "user-read-private user-read-email",
26 expires_in => 3600,
27 refresh_token => "NgAagA...Um_SHo",
28 );
29  
30 my $time_obtained = $token->updated_at->epoch;
31 my $auth_header = $token->header_string;
32 # Bearer NgCXRK...MzYjw
33  
34 my $new_token = {access_token => 'NEW...ONE',}; # can have the reset of params.
35 $token->renew($new_token);
36 my $new_time = $token->updated_at->epoch;
37  
38 =head1 DESCRIPTION
39  
40 Class representing Spotify Token Object. Adds some functionality to Token object where it's easier to deal with.
41 More details about Token itself, L<RFC-6749|http://tools.ietf.org/html/rfc6749#section-4.1>.
42  
43 =head1 PARAMETERS
44  
45 =over 4
46  
47 =item access_token
48  
49 Spotify App User access_token
50  
51 =item refresh_token
52  
53 Spotify App User refresh_token
54  
55 =item token_type
56  
57 Spotify Token type, usually it's set to `Bearer` when used with Access Token.
58 However it's also set to `Basic` when used with Authentication.
59  
60 =item expires_in
61  
62 Token validity in seconds from obtained time. set in C<updated_at>
63  
64 =item scope
65  
66 Spotify App User token allowed scope list
67 L<https://developer.spotify.com/documentation/general/guides/scopes/>
68  
69 =back
70  
71 =head1 METHODS
72  
73 =cut
74  
75 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
76 0 my ( $class, %args ) = @_;
77  
78 0 my $self = bless \%args, $class;
79  
80 0 ▶ $self->access_token($self->{access_token}) if exists $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"access_token"} 0 0 0
81 0 ▶ $self->scope($self->{scope}) if exists $self->{scope};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"scope"} 0 0 0
82  
83 0 return $self;
84 }
85  
86 =head2 renew
87  
88 call this method while passing a new Token hash, to update the current Token Object.
89 Accepts the same hash that you'd pass it in C<new()>.
90  
91 =cut
92  
93 0 ▶sub renew {
Subroutine renew: not called CC=3 SCAR=24.8
Pod documented
94 0 my ( $self, $args ) = @_;
95 0 for my $key ( keys %$args ) {
96 0 ▶ $self->$key($args->{$key}) if $self->can($key);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($key) 0 0 0
97 }
98 }
99  
100 =head2 access_token
101  
102 Mutator for Spotify User's Access Token, every time its set C<updated_at> field will be set to the current time.
103 It will return the current set Spotify Access Token.
104  
105 =cut
106  
107 0 ▶sub access_token {
Subroutine access_token: not called CC=3 SCAR=24.8
Pod documented
108 0 my ( $self, $token ) = @_;
109 0 ▶ if ( defined $token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token) 0 0 0
110 0 $self->{access_token} = $token;
111 0 $self->{updated_at} = Time::Moment->now;
112 }
113  
114 # By design we will allow a Token object to exists without an access_token
115 # Mainly because we want to use its default expires_in field.
116 # so just warn if tried to ues empty access_token
117 0 ▶ $log->warn('Attempting to use a not yet set access_token') unless defined $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"access_token"} 0 0 0
118  
119 0 return $self->{access_token};
120 }
121  
122 =head2 updated_at
123  
124 it will return L<Time::Moment> Object, indicating Spotify Access Token last set time.
125  
126 =cut
127  
128 0 ▶sub updated_at { shift->{updated_at} }
Subroutine updated_at: not called CC=1 SCAR=6.9
Pod documented
129  
130 =head2 refresh_token
131  
132 set and get, or just get the current Spotify user's Refresh Token
133  
134 =cut
135  
136 0 ▶sub refresh_token {
Subroutine refresh_token: not called CC=2 SCAR=17.9
Pod documented
137 0 my ( $self, $token ) = @_;
138  
139 0 ▶ $self->{refresh_token} = $token if defined $token;
Branch: 1 branch
BranchTrueFalseTotal
if defined $token 0 0 0
140  
141 0 return $self->{refresh_token};
142 }
143  
144 =head2 token_type
145  
146 set and get, or just get the Token type, default is set to `Bearer`
147  
148 =cut
149  
150 0 ▶sub token_type {
Subroutine token_type: not called CC=3 SCAR=24.8
Pod documented
151 0 my ( $self, $type ) = @_;
152  
153 0 ▶ $self->{token_type} = $type if defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 0 0 0
154 # Set it to default if not set.
155 0 ▶ return $self->{token_type} //= 'Bearer';
Condition0%0/2
$self->{"token_type"} //= "Bearer"
l!l
00
MC/DC0%0/1
$self->{"token_type"} //= "Bearer"
$self->{"token_type"}
Truth table0%0/2
A
A= $self->{"token_type"}
Aresult
11
00
156 }
157  
158 =head2 expires_in
159  
160 set and get, or just get the Token expires_in filed. Default is set to 3600 (seconds)
161  
162 =cut
163  
164 0 ▶sub expires_in {
Subroutine expires_in: not called CC=3 SCAR=24.8
Pod documented
165 0 my ( $self, $expiry ) = @_;
166  
167 0 ▶ $self->{expires_in} = $expiry if defined $expiry;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expiry 0 0 0
168 # Set it to default if not set
169 0 ▶ return $self->{expires_in} //= 3600;
Condition0%0/2
$self->{"expires_in"} //= 3600
l!l
00
MC/DC0%0/1
$self->{"expires_in"} //= 3600
$self->{"expires_in"}
Truth table0%0/2
A
A= $self->{"expires_in"}
Aresult
11
00
170 }
171  
172 =head2 scope
173  
174 set and get, or just get the configured Spotify Scopes for the Token.
175 Accepts a space space separated list of Scopes, or an Array reference of Scopes.
176 Returns an Array reference of Spotify Scopes set to Token.
177 L<https://developer.spotify.com/documentation/general/guides/scopes/>
178  
179 =cut
180  
181 0 ▶sub scope {
Subroutine scope: not called CC=3 SCAR=24.8
Pod documented
182 0 my ( $self, $scopes ) = @_;
183  
184 0 ▶ if ( defined $scopes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scopes) 0 0 0
185 0 ▶ $self->{scope} = ref($scopes) eq 'ARRAY' ? $scopes : [ split ' ', $scopes ];
Branch: 1 branch
BranchTrueFalseTotal
ref $scopes eq 'ARRAY' ? : 0 0 0
186 }
187  
188 0 return $self->{scope};
189 }
190  
191 =head2 header_string
192  
193 Returns a String, containing the Token type and the Access Token, space separated.
194 Needed for Authorization header.
195  
196 =cut
197  
198 0 ▶sub header_string {
Subroutine header_string: not called CC=1 SCAR=6.9
Pod documented
199 0 my $self = shift;
200 0 return join ' ', $self->token_type, $self->access_token;
201 }
202  
203 2 1;
« lib/Net/Async/Spotify/Util.pm ↑ Summary script/spotify-cli.pl »