Pithub/Base.pm

filter: Statementstmt 99.3% 146 / 147 Branchbran 96.6% 58 / 60 Conditioncond 85.7% 12 / 14 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 95.8% 23 / 24 Podpod 100% 3 / 3 totaltotal 96.1% 247 / 257 CC 39 SCAR 19.2
CC
39
Cov
98%
request
17.0
_request_for
5.0
_make_request
4.0
CRAP 6.8
↑ Summary Pithub/Repos/Stats.pm »
1 package Pithub::Base;
2 24 our $AUTHORITY = 'cpan:PLU';
3  
4 # ABSTRACT: Github v3 base class for all Pithub modules
5  
6 24 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 24 our $VERSION = '0.01041';
9  
10 24 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use HTTP::Headers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use JSON::MaybeXS qw( JSON );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 24 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 24 ▶use Pithub::Result ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 24 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 24 with 'Pithub::Result::SharedCache';
19  
20  
21 has 'auto_pagination' => (
22 350 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 24 is => 'rw',
24 );
25  
26  
27 has 'api_uri' => (
28 351 ▶ default => sub { URI->new('https://api.github.com') },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 is => 'rw',
30 trigger => sub {
31 225 ▶ my ( $self, $uri ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
32 225 $self->{api_uri} = URI->new("$uri");
33 },
34 24 );
35  
36  
37 24 has 'jsonp_callback' => (
38 clearer => 'clear_jsonp_callback',
39 is => 'rw',
40 predicate => 'has_jsonp_callback',
41 required => 0,
42 );
43  
44  
45 24 has 'per_page' => (
46 clearer => 'clear_per_page',
47 is => 'rw',
48 predicate => 'has_per_page',
49 default => 100,
50 required => 0,
51 );
52  
53  
54 24 has 'prepare_request' => (
55 clearer => 'clear_prepare_request',
56 is => 'rw',
57 predicate => 'has_prepare_request',
58 required => 0,
59 );
60  
61  
62 24 has 'repo' => (
63 clearer => 'clear_repo',
64 is => 'rw',
65 predicate => 'has_repo',
66 required => 0,
67 );
68  
69  
70 24 has 'token' => (
71 clearer => 'clear_token',
72 is => 'rw',
73 predicate => '_has_token',
74 required => 0,
75 );
76  
77  
78 24 has 'ua' => (
79 builder => '_build_ua',
80 is => 'ro',
81 lazy => 1,
82 );
83  
84  
85 24 has 'user' => (
86 clearer => 'clear_user',
87 is => 'rw',
88 predicate => 'has_user',
89 required => 0,
90 );
91  
92  
93 24 has 'utf8' => (
94 is => 'ro',
95 default => 1,
96 );
97  
98 24 has '_json' => (
99 builder => '_build__json',
100 is => 'ro',
101 lazy => 1,
102 );
103  
104 24 my @TOKEN_REQUIRED = (
105 'DELETE /user/emails',
106 'GET /user',
107 'GET /user/emails',
108 'GET /user/followers',
109 'GET /user/following',
110 'GET /user/keys',
111 'GET /user/repos',
112 'PATCH /user',
113 'POST /user/emails',
114 'POST /user/keys',
115 'POST /user/repos',
116 );
117  
118 24 my @TOKEN_REQUIRED_REGEXP = (
119 qr{^DELETE },
120 qr{^GET /gists/starred$},
121 qr{^GET /gists/[^/]+/star$},
122 qr{^GET /issues$},
123 qr{^GET /orgs/[^/]+/members/.*$},
124 qr{^GET /orgs/[^/]+/teams$},
125 qr{^GET /repos/[^/]+/[^/]+/collaborators$},
126 qr{^GET /repos/[^/]+/[^/]+/collaborators/.*$},
127 qr{^GET /repos/[^/]+/[^/]+/hooks$},
128 qr{^GET /repos/[^/]+/[^/]+/hooks/.*$},
129 qr{^GET /repos/[^/]+/[^/]+/keys$},
130 qr{^GET /repos/[^/]+/[^/]+/keys/.*$},
131 qr{^GET /teams/.*$},
132 qr{^GET /teams/[^/]+/members$},
133 qr{^GET /teams/[^/]+/members/.*$},
134 qr{^GET /teams/[^/]+/repos$},
135 qr{^GET /teams/[^/]+/repos/.*$},
136 qr{^GET /user/following/.*$},
137 qr{^GET /user/keys/.*$},
138 qr{^GET /user/orgs$},
139 qr{^GET /user/starred/[^/]+/.*$},
140 qr{^GET /user/watched$},
141 qr{^GET /user/watched/[^/]+/.*$},
142 qr{^GET /users/[^/]+/events/orgs/.*$},
143 qr{^PATCH /gists/.*$},
144 qr{^PATCH /gists/[^/]+/comments/.*$},
145 qr{^PATCH /orgs/.*$},
146 qr{^PATCH /repos/[^/]+/.*$},
147 qr{^PATCH /repos/[^/]+/[^/]+/comments/.*$},
148 qr{^PATCH /repos/[^/]+/[^/]+/git/refs/.*$},
149 qr{^PATCH /repos/[^/]+/[^/]+/hooks/.*$},
150 qr{^PATCH /repos/[^/]+/[^/]+/issues/.*$},
151 qr{^PATCH /repos/[^/]+/[^/]+/issues/comments/.*$},
152 qr{^PATCH /repos/[^/]+/[^/]+/keys/.*$},
153 qr{^PATCH /repos/[^/]+/[^/]+/labels/.*$},
154 qr{^PATCH /repos/[^/]+/[^/]+/milestones/.*$},
155 qr{^PATCH /repos/[^/]+/[^/]+/pulls/.*$},
156 qr{^PATCH /repos/[^/]+/[^/]+/releases/.*$},
157 qr{^PATCH /repos/[^/]+/[^/]+/pulls/comments/.*$},
158 qr{^PATCH /teams/.*$},
159 qr{^PATCH /user/keys/.*$},
160 qr{^PATCH /user/repos/.*$},
161 qr{^POST /repos/[^/]+/[^/]+/releases/[^/]+/assets.*$},
162 qr{^POST /gists/[^/]+/comments$},
163 qr{^POST /orgs/[^/]+/repos$},
164 qr{^POST /orgs/[^/]+/teams$},
165 qr{^POST /repos/[^/]+/[^/]+/commits/[^/]+/comments$},
166 qr{^POST /repos/[^/]+/[^/]+/downloads$},
167 qr{^POST /repos/[^/]+/[^/]+/forks},
168 qr{^POST /repos/[^/]+/[^/]+/git/blobs$},
169 qr{^POST /repos/[^/]+/[^/]+/git/commits$},
170 qr{^POST /repos/[^/]+/[^/]+/git/refs},
171 qr{^POST /repos/[^/]+/[^/]+/git/tags$},
172 qr{^POST /repos/[^/]+/[^/]+/git/trees$},
173 qr{^POST /repos/[^/]+/[^/]+/hooks$},
174 qr{^POST /repos/[^/]+/[^/]+/hooks/[^/]+/test$},
175 qr{^POST /repos/[^/]+/[^/]+/issues$},
176 qr{^POST /repos/[^/]+/[^/]+/issues/[^/]+/comments},
177 qr{^POST /repos/[^/]+/[^/]+/issues/[^/]+/labels$},
178 qr{^POST /repos/[^/]+/[^/]+/keys$},
179 qr{^POST /repos/[^/]+/[^/]+/labels$},
180 qr{^POST /repos/[^/]+/[^/]+/milestones$},
181 qr{^POST /repos/[^/]+/[^/]+/pulls$},
182 qr{^POST /repos/[^/]+/[^/]+/releases$},
183 qr{^POST /repos/[^/]+/[^/]+/pulls/[^/]+/comments$},
184 qr{^POST /repos/[^/]+/[^/]+/pulls/[^/]+/requested_reviewers$},
185 qr{^PUT /gists/[^/]+/star$},
186 qr{^PUT /orgs/[^/]+/public_members/.*$},
187 qr{^PUT /repos/[^/]+/[^/]+/collaborators/.*$},
188 qr{^PUT /repos/[^/]+/[^/]+/issues/[^/]+/labels$},
189 qr{^PUT /repos/[^/]+/[^/]+/pulls/[^/]+/merge$},
190 qr{^PUT /teams/[^/]+/members/.*$},
191 qr{^PUT /teams/[^/]+/memberships/.*$},
192 qr{^PUT /teams/[^/]+/repos/.*$},
193 qr{^PUT /user/following/.*$},
194 qr{^PUT /user/starred/[^/]+/.*$},
195 qr{^PUT /user/watched/[^/]+/.*$},
196 );
197  
198  
199 sub request {
200 686 ▶ my ( $self, %args ) = @_;
Subroutine request: called CC=17 SCAR=28.3
Pod documented
201  
202 my $method = delete $args{method}
203 686 ▶ || croak 'Missing mandatory key in parameters: method';
Condition66%2/3
delete $args{'method'} || croak('Missing mandatory key in parameters: method')
l!l&&r!l&&!r
68510
MC/DC0%0/2
delete $args{'method'} || croak('Missing mandatory key in parameters: method')
delete $args{'method'} croak('Missing mandatory key in parameters: method')
Truth table33%1/3
A || B
A= delete $args{'method'}B= croak('Missing mandatory key in parameters: method')
ABresult
1X1
011
000
204 my $path = delete $args{path}
205 685 ▶ || croak 'Missing mandatory key in parameters: path';
Condition66%2/3
delete $args{'path'} || croak('Missing mandatory key in parameters: path')
l!l&&r!l&&!r
68410
MC/DC0%0/2
delete $args{'path'} || croak('Missing mandatory key in parameters: path')
delete $args{'path'} croak('Missing mandatory key in parameters: path')
Truth table33%1/3
A || B
A= delete $args{'path'}B= croak('Missing mandatory key in parameters: path')
ABresult
1X1
011
000
206 684 my $data = delete $args{data};
207 684 my $options = delete $args{options};
208 684 my $params = delete $args{params};
209  
210 684 ▶ croak "Invalid method: $method"
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ eq $method), ("DELETE", "GET", "PATCH", "POST", "PUT")) 1 683 684
211 unless grep $_ eq $method, qw(DELETE GET PATCH POST PUT);
212  
213 683 my $uri = $self->_uri_for($path);
214  
215 683 ▶ if ( my $host = delete $args{host} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = delete $args{"host"}) 3 680 683
216 3 $uri->host($host);
217 }
218  
219 683 ▶ if ( my $query = delete $args{query} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $query = delete $args{"query"}) 6 677 683
220 6 $uri->query_form(%$query);
221 }
222  
223 683 my $request = $self->_request_for( $method, $uri, $data );
224  
225 683 ▶ if ( my $headers = delete $args{headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $headers = delete $args{"headers"}) 3 680 683
226 3 foreach my $header ( keys %$headers ) {
227 3 $request->header( $header, $headers->{$header} );
228 }
229 }
230  
231 683 ▶ if ( $self->_token_required( $method, $path )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_token_required($method, $path) and not $self->has_token($request)) 112 571 683
Condition100%3/3
$self->_token_required($method, $path) and not $self->has_token($request)
!ll&&!rl&&r
326245112
MC/DC100%2/2
$self->_token_required($method, $path) and not $self->has_token($request)
$self->_token_required($method, $path) not $self->has_token($request)
Truth table100%3/3
A and not(B)
A= $self->_token_required($method, $path)B= not $self->has_token($request)
ABresult
0X0
100
111
232 && !$self->has_token($request) ) {
233 112 croak sprintf 'Access token required for: %s %s (%s)', $method,
234 $path, $uri;
235 }
236  
237 571 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 227 344 571
238 227 ▶ croak 'The key options must be a hashref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options eq "HASH" 1 226 227
239 unless ref $options eq 'HASH';
240 croak
241 'The key prepare_request in the options hashref must be a coderef'
242 if $options->{prepare_request}
243 226 ▶ && ref $options->{prepare_request} ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"prepare_request"} and ref $options->{"prepare_request"} ne "CODE" 1 225 226
Condition100%3/3
$options->{"prepare_request"} and ref $options->{"prepare_request"} ne "CODE"
!ll&&!rl&&r
12241
MC/DC100%2/2
$options->{"prepare_request"} and ref $options->{"prepare_request"} ne "CODE"
$options->{"prepare_request"} ref $options->{"prepare_request"} ne "CODE"
Truth table100%3/3
A and B
A= $options->{"prepare_request"}B= ref $options->{"prepare_request"} ne "CODE"
ABresult
0X0
100
111
244  
245 225 ▶ if ( $options->{prepare_request} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"prepare_request"}) 224 1 225
246 224 $options->{prepare_request}->($request);
247 }
248 }
249  
250 569 ▶ if ($params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params) 9 560 569
251 9 ▶ croak 'The key params must be a hashref' unless ref $params eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params eq "HASH" 1 8 9
252 8 my %query = ( $request->uri->query_form, %$params );
253 8 $request->uri->query_form(%query);
254 }
255  
256 568 my $response = $self->_make_request($request);
257  
258 return Pithub::Result->new(
259 auto_pagination => $self->auto_pagination,
260 response => $response,
261 utf8 => $self->utf8,
262 21 ▶ _request => sub { $self->request(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
263 568 );
264 }
265  
266 sub _make_request {
267 568 ▶ my ( $self, $request ) = @_;
Subroutine _make_request: called CC=4 SCAR=13.9
268  
269 568 my $cache_key = $request->uri->as_string;
270 568 ▶ if ( my $cached_response = $self->shared_cache->get($cache_key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cached_response = $self->shared_cache->get($cache_key)) 234 334 568
271  
272 # Add the If-None-Match header from the cache's ETag
273 # and make the request
274 234 $request->header(
275 'If-None-Match' => $cached_response->header('ETag') );
276 234 my $response = $self->ua->request($request);
277  
278 # Got 304 Not Modified, cache is still valid
279 234 ▶ return $cached_response if ( $response->code || 0 ) == 304;
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code || 0) == 304 0 234 234
Condition100%2/2
$response->code || 0
l!l
6228
MC/DC100%1/1
$response->code || 0
$response->code
Truth table100%2/2
A
A= $response->code
Aresult
11
00
280  
281 # The response changed, cache it and return it.
282 234 $self->shared_cache->set( $cache_key, $response );
283 234 return $response;
284 }
285  
286 334 my $response = $self->ua->request($request);
287 334 $self->shared_cache->set( $cache_key, $response );
288 334 return $response;
289 }
290  
291  
292 sub has_token {
293 1264 ▶ my ( $self, $request ) = @_;
Subroutine has_token: called CC=4 SCAR=13.9
Pod documented
294  
295 # If we have one specified in the object, return true
296 1264 ▶ return 1 if $self->_has_token;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_token 845 419 1264
297  
298 # If no request object here, we don't have a token
299 419 ▶ return 0 unless $request;
Branch: 1 branch
BranchTrueFalseTotal
unless $request 306 113 419
300  
301 113 ▶ return 1 if $request->header('Authorization');
Branch: 1 branch
BranchTrueFalseTotal
if $request->header("Authorization") 1 112 113
302 112 return 0;
303 }
304  
305  
306 sub rate_limit {
307 0 ▶ return shift->request( method => 'GET', path => '/rate_limit' );
Subroutine rate_limit: not called CC=1 SCAR=6.9
Pod documented
308 }
309  
310 sub _build__json {
311 96 ▶ my ($self) = @_;
Subroutine _build__json: called CC=1 SCAR=0.0
312 96 return JSON->new->utf8( $self->utf8 );
313 }
314  
315 sub _build_ua {
316 27 ▶ my ($self) = @_;
Subroutine _build_ua: called CC=1 SCAR=0.0
317 27 return LWP::UserAgent->new;
318 }
319  
320 sub _get_user_repo_args {
321 476 ▶ my ( $self, $args ) = @_;
Subroutine _get_user_repo_args: called CC=3 SCAR=11.0
322 476 ▶ $args->{user} = $self->user unless defined $args->{user};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"user"} 260 216 476
323 476 ▶ $args->{repo} = $self->repo unless defined $args->{repo};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"repo"} 282 194 476
324 476 return $args;
325 }
326  
327 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
328 sub _create_instance {
329 224 ▶ my ( $self, $class, @args ) = @_;
Subroutine _create_instance: called CC=4 SCAR=13.9
330  
331 224 my %args = (
332 api_uri => $self->api_uri,
333 auto_pagination => $self->auto_pagination,
334 ua => $self->ua,
335 utf8 => $self->utf8,
336 @args,
337 );
338  
339 224 for my $attr (qw(repo token user per_page jsonp_callback prepare_request))
340 {
341 # Allow overrides to set attributes to undef
342 1344 ▶ next if exists $args{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$attr} 3 1341 1344
343  
344 1341 my $has_attr = "has_$attr";
345 1341 ▶ $args{$attr} = $self->$attr if $self->$has_attr;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$has_attr 691 650 1341
346 }
347  
348 224 return $class->new(%args);
349 }
350 ## use critic
351  
352 sub _request_for {
353 683 ▶ my ( $self, $method, $uri, $data ) = @_;
Subroutine _request_for: called CC=5 SCAR=16.1
354  
355 683 my $headers = HTTP::Headers->new;
356  
357 683 ▶ if ( $self->has_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_token) 451 232 683
358 451 $headers->header(
359 'Authorization' => sprintf( 'token %s', $self->token ) );
360 }
361  
362 683 my $request = HTTP::Request->new( $method, $uri, $headers );
363  
364 683 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 304 379 683
365 304 ▶ $data = $self->_json->encode($data) if ref $data;
Branch: 1 branch
BranchTrueFalseTotal
if ref $data 302 2 304
366 304 $request->content($data);
367 }
368  
369 683 $request->header( 'Content-Length' => length $request->content );
370  
371 683 ▶ if ( $self->has_prepare_request ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_prepare_request) 204 479 683
372 204 $self->prepare_request->($request);
373 }
374  
375 683 return $request;
376 }
377  
378 24 my %TOKEN_REQUIRED = map { ( $_ => 1 ) } @TOKEN_REQUIRED;
379  
380 sub _token_required {
381 683 ▶ my ( $self, $method, $path ) = @_;
Subroutine _token_required: called CC=4 SCAR=13.9
382  
383 683 my $key = "${method} ${path}";
384  
385 683 ▶ return 1 if $TOKEN_REQUIRED{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $TOKEN_REQUIRED{$key} 30 653 683
386  
387 653 foreach my $regexp (@TOKEN_REQUIRED_REGEXP) {
388 34159 ▶ return 1 if $key =~ /$regexp/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /$regexp/ 327 33832 34159
389 }
390  
391 326 return 0;
392 }
393  
394 sub _uri_for {
395 683 ▶ my ( $self, $path ) = @_;
Subroutine _uri_for: called CC=3 SCAR=11.0
396  
397 683 my $uri = $self->api_uri->clone;
398 683 my $base_path = $uri->path;
399 683 $path =~ s/^$base_path//;
400 683 my @parts;
401 683 push @parts, split qr{/+}, $uri->path;
402 683 push @parts, split qr{/+}, $path;
403 683 $uri->path( join '/', grep { $_ } @parts );
404  
405 683 ▶ if ( $self->has_per_page ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_per_page) 683 0 683
406 683 my %query = ( $uri->query_form, per_page => $self->per_page );
407 683 $uri->query_form(%query);
408 }
409  
410 683 ▶ if ( $self->has_jsonp_callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_jsonp_callback) 203 480 683
411 203 my %query = ( $uri->query_form, callback => $self->jsonp_callback );
412 203 $uri->query_form(%query);
413 }
414  
415 683 return $uri;
416 }
417  
418 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
419 sub _validate_user_repo_args {
420 471 ▶ my ( $self, $args ) = @_;
Subroutine _validate_user_repo_args: called CC=3 SCAR=11.0
421 471 $args = $self->_get_user_repo_args($args);
422 471 ▶ croak 'Missing key in parameters: user' unless $args->{user};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"user"} 21 450 471
423 450 ▶ croak 'Missing key in parameters: repo' unless $args->{repo};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"repo"} 21 429 450
424 }
425 ## use critic
426  
427 24 1;
428  
429 __END__
↑ Summary Pithub/Repos/Stats.pm »