Gett/User.pm

filter: Statementstmt 40.6% 26 / 64 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 36.3% 4 / 11 Podpod 100% 2 / 2 totaltotal 26.4% 32 / 121 CC 16 SCAR 31.1
CC
16
Cov
26%
__ANON__
42.0
login
42.0
refresh
12.0
CRAP 22.4
« Gett.pm ↑ Summary Gett/Share.pm »
1 package Net::API::Gett::User;
2  
3 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use MooX::Types::MooseLike::Base qw(Int Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Net::API::Gett::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our $VERSION = '1.06';
10  
11 =head1 NAME
12  
13 Net::API::Gett::User - Gett User object
14  
15 =head1 PURPOSE
16  
17 This class encapsulates Gett service user functions. You normally shouldn't instanstiate
18 this class on its own as the library will create and return this object when appropriate.
19  
20 =head1 ATTRIBUTES
21  
22 Here are the attributes of this class. They are read only.
23  
24 =over
25  
26 =item api_key
27  
28 Scalar string. C<has_api_key> predicate.
29  
30 =back
31  
32 =cut
33  
34 5 has 'api_key' => (
35 is => 'ro',
36 predicate => 'has_api_key',
37 isa => Str,
38 );
39  
40 =over
41  
42 =item email
43  
44 Scalar string. C<has_email> predicate.
45  
46 =back
47  
48 =cut
49  
50 has 'email' => (
51 is => 'ro',
52 predicate => 'has_email',
53 0 ▶ isa => sub { die "$_[0] is not email" unless $_[0] =~ /.+@.+/ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /.+\@.+/ 0 0 0
54 5 );
55  
56 =over
57  
58 =item password
59  
60 Scalar string. C<has_password> predicate.
61  
62 =back
63  
64 =cut
65  
66 5 has 'password' => (
67 is => 'ro',
68 predicate => 'has_password',
69 isa => Str,
70 );
71  
72 =over
73  
74 =item access_token
75  
76 Scalar string. Populated by C<login> call. C<has_access_token()> predicate.
77  
78 =back
79  
80 =cut
81  
82 5 has 'access_token' => (
83 is => 'rw',
84 writer => '_set_access_token',
85 predicate => 'has_access_token',
86 isa => Str,
87 );
88  
89 =over
90  
91 =item access_token_expiration
92  
93 Scalar integer. Unix epoch seconds until an access token is no longer valid which is
94 currently 24 hours (86400 seconds) from token acquisition.
95 This value is suitable for use in a call to C<localtime()>.
96  
97 =back
98  
99 =cut
100  
101 5 has 'access_token_expiration' => (
102 is => 'rw',
103 writer => '_set_access_token_expiration',
104 isa => Int,
105 );
106  
107 =over
108  
109 =item refresh_token
110  
111 Scalar string. Populated by C<login> call. Can be used to generate a new valid
112 access token without reusing an email/password login method. C<has_refresh_token()>
113 predicate.
114  
115 =back
116  
117 =cut
118  
119 5 has 'refresh_token' => (
120 is => 'rw',
121 writer => '_set_refresh_token',
122 predicate => 'has_refresh_token',
123 isa => Str,
124 );
125  
126 =over
127  
128 =item request
129  
130 This is a L<Net::API::Gett::Request> object. Defaults to a new instance of that class.
131  
132 =back
133  
134 =cut
135  
136 has 'request' => (
137 is => 'rw',
138 0 ▶ isa => sub { die "$_[0] is not Net::API::Gett::Request" unless ref($_[0]) =~ /Request/ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref($_[0]) =~ /Request/ 0 0 0
139 0 ▶ default => sub { Net::API::Gett::Request->new() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 5 lazy => 1,
141 );
142  
143 =over
144  
145 =item userid
146  
147 Scalar string.
148  
149 =item fullname
150  
151 Scalar string.
152  
153 =item email
154  
155 Scalar string.
156  
157 =item storage_used
158  
159 Scalar integer. In bytes.
160  
161 =item storage_limit
162  
163 Scalar integer. In bytes.
164  
165 =back
166  
167  
168 =cut
169  
170 5 has 'userid' => (
171 is => 'rw',
172 writer => '_set_userid',
173 isa => Str
174 );
175  
176 5 has 'fullname' => (
177 is => 'rw',
178 writer => '_set_fullname',
179 isa => Str,
180 );
181  
182 5 has 'storage_used' => (
183 is => 'rw',
184 isa => Int,
185 writer => '_set_storage_used',
186 );
187  
188 5 has 'storage_limit' => (
189 is => 'rw',
190 isa => Int,
191 writer => '_set_storage_limit',
192 );
193  
194 =head1 METHODS
195  
196 =over
197  
198 =item login()
199  
200 This method populates the C<access_token>, C<refresh_token> and C<user> attributes. It usually
201 doesn't need to be explicitly called since methods which require an access token will automatically
202 (and lazily) attempt to log in to the API and get one.
203  
204 Returns a perl hash representation of the JSON output for L<https://open.ge.tt/1/users/login>.
205  
206 =back
207  
208 =cut
209  
210 0 ▶around BUILDARGS => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
211 0 my $orig = shift;
212 0 my $class = shift;
213  
214 0 my %params = @_;
215  
216 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"refresh_token"} or $params{"access_token"} or $params{"api_key"} and $params{"email"} and $params{"password"}) 0 0 0
Condition0%0/12
$params{"refresh_token"} or $params{"access_token"} or $params{"api_key"} and $params{"email"} and $params{"password"}
l!l&&r!l&&!r
000
$params{"refresh_token"} or $params{"access_token"}
l!l&&r!l&&!r
000
$params{"api_key"} and $params{"email"} and $params{"password"}
!ll&&!rl&&r
000
$params{"api_key"} and $params{"email"}
!ll&&!rl&&r
000
MC/DC0%0/5
$params{"refresh_token"} or $params{"access_token"} or $params{"api_key"} and $params{"email"} and $params{"password"}
$params{"refresh_token"} $params{"access_token"} $params{"api_key"} $params{"email"} $params{"password"}
Truth table0%0/6
A or B or C and D and E
A= $params{"refresh_token"}B= $params{"access_token"}C= $params{"api_key"}D= $params{"email"}E= $params{"password"}
ABCDEresult
1XXXX1
01XXX1
001111
000XX0
0010X0
001100
217 $params{refresh_token}
218 || $params{access_token}
219 || ( $params{api_key} && $params{email} && $params{password} ) )
220 {
221 0 die(
222 "api_key, email and password are needed to create ",
223 "Net::API::Gett::User object. Or you can use refresh_token ",
224 "or access_token rather than api_key, email and password.\n",
225 );
226 }
227  
228 0 return $class->$orig(@_);
229 5 };
230  
231 0 ▶sub login {
Subroutine login: not called CC=6 SCAR=37.4
Pod documented
232 0 my $self = shift;
233  
234 0 my %hr;
235 0 ▶ if ( $self->has_refresh_token ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_refresh_token) { } 0 0 0
elsif ($self->has_api_key and $self->has_email and $self->has_password) { } 0 0 0
236 0 $hr{'refreshtoken'} = $self->refresh_token;
237 }
238 ▶ elsif ( $self->has_api_key && $self->has_email && $self->has_password ) {
Condition0%0/6
$self->has_api_key and $self->has_email and $self->has_password
!ll&&!rl&&r
000
$self->has_api_key and $self->has_email
!ll&&!rl&&r
000
MC/DC0%0/3
$self->has_api_key and $self->has_email and $self->has_password
$self->has_api_key $self->has_email $self->has_password
Truth table0%0/4
A and B and C
A= $self->has_api_keyB= $self->has_emailC= $self->has_password
ABCresult
0XX0
10X0
1100
1111
239 0 @hr{'apikey', 'email', 'password'} = (
240 $self->api_key,
241 $self->email,
242 $self->password);
243 }
244 else {
245 0 croak "I need either an api_key, email, and password or a refresh token to login";
246 }
247  
248 0 my $response = $self->request->post('/users/login', \%hr);
249  
250 # $response is a hashref
251 # see https://open.ge.tt/1/doc/rest#users/login for response keys
252  
253 0 ▶ if ( $response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) { } 0 0 0
254 0 $self->_set_access_token( $response->{'accesstoken'} );
255 0 $self->_set_access_token_expiration( time + $response->{'expires'} );
256 0 $self->_set_refresh_token( $response->{'refreshtoken'} );
257 0 $self->_set_attrs( $response->{'user'} );
258 0 return $self;
259 }
260 else {
261 0 croak("No response from user->login");
262 }
263 }
264  
265 =over
266  
267 =item refresh()
268  
269 Refreshes user data.
270  
271 =back
272  
273 =cut
274  
275 0 ▶sub refresh {
Subroutine refresh: not called CC=3 SCAR=24.8
Pod documented
276 0 my $self = shift;
277  
278 0 ▶ $self->login unless $self->has_access_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_access_token 0 0 0
279  
280 0 my $endpoint = "/users/me?accesstoken=" . $self->access_token;
281  
282 0 my $response = $self->request->get($endpoint);
283  
284 0 ▶ if ( $response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) { } 0 0 0
285 0 $self->_set_attrs($response);
286 0 return $self;
287 }
288 else {
289 0 croak("No response from user->refresh");
290 }
291 }
292  
293 0 ▶sub _set_attrs {
Subroutine _set_attrs: not called CC=2 SCAR=17.9
294 0 my $self = shift;
295 0 my $uref = shift; # hashref https://open.ge.tt/1/doc/rest#users/me
296  
297 0 ▶ return undef unless ref($uref) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $uref eq "HASH" 0 0 0
298  
299 0 $self->_set_userid($uref->{'userid'});
300 0 $self->_set_fullname($uref->{'fullname'});
301 0 $self->_set_storage_used($uref->{'storage'}->{'used'});
302 0 $self->_set_storage_limit($uref->{'storage'}->{'limit'}),
303 }
304  
305 =head1 SEE ALSO
306  
307 L<Net::API::Gett>
308  
309 =cut
310  
311 5 1;
312  
« Gett.pm ↑ Summary Gett/Share.pm »