GitHub/V3/Query.pm

filter: Statementstmt 28.5% 104 / 364 Branchbran 3.8% 6 / 158 Conditioncond 11.1% 8 / 72 MC/DCmcdc 7.3% 3 / 41 Subroutinesub 38.6% 17 / 44 Podpod 83.3% 10 / 12 totaltotal 21.4% 148 / 691 CC 111 SCAR 62.4
CC
111
Cov
20%
query
1722.0
__ANON__
239.1
iterate
182.0
CRAP 512.2
↑ Summary GitHub/V4.pm »
1 package Net::GitHub::V3::Query;
2  
3 1 our $VERSION = '1.02';
4 1 our $AUTHORITY = 'cpan:FAYLAND';
5  
6 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Types::Standard qw(Int Str Bool InstanceOf Object HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Cache::LRU;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Net::GitHub::V3::ResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # configurable args
23  
24 # Authentication
25 1 has 'login' => ( is => 'rw', isa => Str, predicate => 'has_login' );
26 1 has 'pass' => ( is => 'rw', isa => Str, predicate => 'has_pass' );
27 1 has 'otp' => ( is => 'rw', isa => Str, predicate => 'has_otp' );
28 1 has 'access_token' => ( is => 'rw', isa => Str, predicate => 'has_access_token' );
29  
30 # return raw unparsed JSON
31 1 has 'raw_string' => (is => 'rw', isa => Bool, default => 0);
32 1 has 'raw_response' => (is => 'rw', isa => Bool, default => 0);
33  
34 1 has 'api_url' => (is => 'ro', default => 'https://api.github.com');
35 1 has 'api_throttle' => ( is => 'rw', isa => Bool, default => 1 );
36  
37 1 has 'upload_url' => (is => 'ro', default => 'https://uploads.github.com');
38  
39 # pagination
40 1 has 'next_url' => ( is => 'rw', isa => Str, predicate => 'has_next_page', clearer => 'clear_next_url' );
41 1 has 'last_url' => ( is => 'rw', isa => Str, predicate => 'has_last_page', clearer => 'clear_last_url' );
42 1 has 'first_url' => ( is => 'rw', isa => Str, predicate => 'has_first_page', clearer => 'clear_first_url' );
43 1 has 'prev_url' => ( is => 'rw', isa => Str, predicate => 'has_prev_page', clearer => 'clear_prev_url' );
44 1 has 'per_page' => ( is => 'rw', isa => Str, default => 100 );
45 1 has 'total_pages' => ( is => 'rw', isa => Str, default => 0 );
46  
47 # deprecation
48 1 has 'deprecation_url' => ( is => 'rw', isa => Str );
49 1 has 'alternate_url' => ( is => 'rw', isa => Str );
50  
51 # Error handle
52 1 has 'RaiseError' => ( is => 'rw', isa => Bool, default => 1 );
53  
54 # Rate limits
55 # has 'rate_limit' => ( is => 'rw', isa => Int, default => sub { shift->update_rate_limit('rate_limit') } );
56 # has 'rate_limit_remaining' => ( is => 'rw', isa => Int, default => sub { shift->update_rate_limit('rate_limit_remaining') } );
57 # has 'rate_limit_reset' => ( is => 'rw', isa => Str, default => sub { shift->update_rate_limit('rate_limit_reset') } );
58 0 ▶has 'rate_limit' => ( is => 'rw', isa => Int, default => sub { 0 } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
59 0 ▶has 'rate_limit_remaining' => ( is => 'rw', isa => Int, default => sub { 0 } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
60 0 ▶has 'rate_limit_reset' => ( is => 'rw', isa => Str, default => sub { 0 } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
61  
62 # optional
63 1 has 'u' => (is => 'rw', isa => Str);
64 1 has 'repo' => (is => 'rw', isa => Str);
65  
66 # accept version
67 1 has 'accept_version' => (is => 'rw', isa => Str, default => '');
68  
69 1 has 'is_main_module' => (is => 'ro', isa => Bool, default => 0);
70  
71 sub update_rate_limit {
72 0 ▶ my ( $self, $what ) = @_;
Subroutine update_rate_limit: not called CC=1 SCAR=6.9
Pod documented
73  
74 # If someone calls rate_limit before an API query happens, force these fields to update before giving back a response.
75 # Per github: Accessing this endpoint does not count against your REST API rate limit.
76 # https://developer.github.com/v3/rate_limit/
77 0 my $content = $self->query('/rate_limit');
78  
79 0 return $self->{$what};
80 }
81  
82 sub set_default_user_repo {
83 0 ▶ my ($self, $user, $repo) = @_;
Subroutine set_default_user_repo: not called CC=6 SCAR=37.4
Pod undocumented
84  
85 0 $self->u($user);
86 0 $self->repo($repo);
87  
88 # need apply to all sub modules
89 0 ▶ if ($self->is_main_module) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_main_module) 0 0 0
90 0 ▶ if ($self->is_repos_init) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_repos_init) 0 0 0
91 0 $self->repos->u($user); $self->repos->repo($repo);
92 }
93 0 ▶ if ($self->is_issue_init) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_issue_init) 0 0 0
94 0 $self->issue->u($user); $self->issue->repo($repo);
95 }
96 0 ▶ if ($self->is_pull_request_init) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_pull_request_init) 0 0 0
97 0 $self->pull_request->u($user); $self->pull_request->repo($repo);
98 }
99 0 ▶ if ($self->is_git_data_init) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_git_data_init) 0 0 0
100 0 $self->git_data->u($user); $self->git_data->repo($repo);
101 }
102 }
103  
104 0 return $self;
105 }
106  
107 sub args_to_pass {
108 0 ▶ my $self = shift;
Subroutine args_to_pass: not called CC=3 SCAR=24.8
Pod undocumented
109 0 my $ret;
110 0 foreach my $col ('login', 'pass', 'otp', 'access_token', 'raw_string', 'raw_response', 'api_url', 'api_throttle', 'u', 'repo', 'next_url', 'last_url', 'first_url', 'prev_url', 'per_page', 'ua') {
111 0 my $v = $self->$col;
112 0 ▶ $ret->{$col} = $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
113 }
114 0 return $ret;
115 }
116  
117 has 'ua' => (
118 isa => InstanceOf['LWP::UserAgent'],
119 is => 'ro',
120 lazy => 1,
121 default => sub {
122 0 ▶ LWP::UserAgent->new(
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 agent => "perl-net-github/$VERSION",
124 cookie_jar => {},
125 keep_alive => 4,
126 timeout => 60,
127 );
128 },
129 1 );
130  
131 has 'json' => (
132 is => 'ro',
133 isa => Object, # InstanceOf['JSON::MaybeXS'],
134 lazy => 1,
135 default => sub {
136 0 ▶ return JSON::MaybeXS->new( utf8 => 1 );
Subroutine __ANON__: not called CC=1 SCAR=6.9
137 }
138 1 );
139  
140 has 'cache' => (
141 isa => InstanceOf['Cache::LRU'],
142 is => 'rw',
143 lazy => 1,
144 default => sub {
145 0 ▶ Cache::LRU->new(
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 size => 200
147 );
148 }
149 1 );
150  
151 # per-page pagination
152  
153 has 'result_sets' => (
154 isa => HashRef,
155 is => 'ro',
156 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 1 );
158  
159 sub next {
160 0 ▶ my $self = shift;
Subroutine next: not called CC=7 SCAR=40.3
Pod documented
161 0 my ($url) = @_;
162 0 my $result_set;
163 0 ▶ $result_set = $self->result_sets->{$url} or do {
Branch: 1 branch
BranchTrueFalseTotal
$result_set = $self->result_sets->{$url} or do { $result_set = "Net::GitHub::V3::ResultSet"->new("url", $url); $self->result_sets->{$url} = $result_set } 0 0 0
164 0 $result_set = Net::GitHub::V3::ResultSet->new( url => $url );
165 0 $self->result_sets->{$url} = $result_set;
166 };
167 0 my $results = $result_set->results;
168 0 my $cursor = $result_set->cursor;
169 0 ▶ if ( $cursor > $#$results ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor > $results->$#*) 0 0 0
170 0 ▶ return if $result_set->done;
Branch: 1 branch
BranchTrueFalseTotal
if $result_set->done 0 0 0
171 0 ▶ my $next_url = $result_set->next_url || $result_set->url;
Condition0%0/3
$result_set->next_url || $result_set->url
l!l&&r!l&&!r
000
MC/DC0%0/2
$result_set->next_url || $result_set->url
$result_set->next_url $result_set->url
Truth table0%0/3
A || B
A= $result_set->next_urlB= $result_set->url
ABresult
1X1
011
000
172 0 my $new_result = $self->query($next_url);
173 0 ▶ $result_set->results(ref $new_result eq 'ARRAY' ?
Branch: 1 branch
BranchTrueFalseTotal
ref $new_result eq 'ARRAY' ? : 0 0 0
174 $new_result :
175 [$new_result]
176 );
177 0 $result_set->cursor(0);
178 0 ▶ if ($self->has_next_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_next_page) { } 0 0 0
179 0 $result_set->next_url($self->next_url);
180 }
181 else {
182 0 $result_set->done(1);
183 }
184 }
185 0 my $result = $result_set->results->[$result_set->cursor];
186 0 $result_set->cursor($result_set->cursor + 1);
187 0 return $result;
188 }
189  
190  
191 sub close {
192 0 ▶ my $self = shift;
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
193 0 my ($url) = @_;
194 0 delete $self->result_sets->{$url};
195 0 return;
196 }
197  
198  
199 sub query {
200 0 ▶ my $self = shift;
Subroutine query: not called CC=41 SCAR=74.5
Pod documented
201  
202 # fix ARGV, not sure if it's the good idea
203 0 my @args = @_;
204 0 ▶ if (@args == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1) { } 0 0 0
elsif (@args > 1 and not grep({$args[0] eq $_;} 'GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE')) { } 0 0 0
205 0 unshift @args, 'GET'; # method by default
206 0 ▶ } elsif (@args > 1 and not (grep { $args[0] eq $_ } ('GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE')) ) {
Condition0%0/3
@args > 1 and not grep({$args[0] eq $_;} "GET", "POST", "PUT", "PATCH", "HEAD", "DELETE")
!ll&&!rl&&r
000
MC/DC0%0/2
@args > 1 and not grep({$args[0] eq $_;} "GET", "POST", "PUT", "PATCH", "HEAD", "DELETE")
@args > 1 not grep({$args[0] eq $_;} "GET", "POST", "PUT", "PATCH", "HEAD", "DELETE")
Truth table0%0/3
A and not(B)
A= @args > 1B= not grep({$args[0] eq $_;} "GET", "POST", "PUT", "PATCH", "HEAD", "DELETE")
ABresult
0X0
100
111
207 0 unshift @args, 'POST'; # if POST content
208 }
209 0 my $request_method = shift @args;
210 0 my $url = shift @args;
211 0 my $data = shift @args;
212  
213 0 my $ua = $self->ua;
214  
215 ## always go with login:pass or access_token (for private repos)
216 0 ▶ if ($self->has_access_token) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_access_token) { } 0 0 0
elsif ($self->has_login and $self->has_pass) { } 0 0 0
217 0 $ua->default_header('Authorization', "token " . $self->access_token);
218 ▶ } elsif ($self->has_login and $self->has_pass) {
Condition0%0/3
$self->has_login and $self->has_pass
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_login and $self->has_pass
$self->has_login $self->has_pass
Truth table0%0/3
A and B
A= $self->has_loginB= $self->has_pass
ABresult
0X0
100
111
219 0 my $auth_basic = $self->login . ':' . $self->pass;
220 0 $ua->default_header('Authorization', 'Basic ' . encode_base64($auth_basic));
221 0 ▶ if ($self->has_otp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_otp) 0 0 0
222 0 $ua->default_header('X-GitHub-OTP', $self->otp);
223 }
224 }
225  
226 0 ▶ $url = $self->api_url . $url unless $url =~ /^https\:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /^https\:/ 0 0 0
227 0 ▶ if ($request_method eq 'GET') {
Branch: 1 branch
BranchTrueFalseTotal
if ($request_method eq "GET") 0 0 0
228 0 ▶ if ($url !~ /per_page=\d/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ /per_page=\d/) 0 0 0
229 ## auto add per_page in url for GET no matter it supports or not
230 0 my $uri = URI->new($url);
231 0 my %query_form = $uri->query_form;
232 0 ▶ $query_form{per_page} ||= $self->per_page;
Condition0%0/3
$query_form{"per_page"} ||= $self->per_page
l!l&&r!l&&!r
000
MC/DC0%0/2
$query_form{"per_page"} ||= $self->per_page
$query_form{"per_page"} $self->per_page
Truth table0%0/3
A ||= B
A= $query_form{"per_page"}B= $self->per_page
ABresult
1X1
011
000
233 0 $uri->query_form(%query_form);
234 0 $url = $uri->as_string;
235 }
236 0 ▶ if ($data and ref $data eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data and ref $data eq "HASH") 0 0 0
Condition0%0/3
$data and ref $data eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$data and ref $data eq "HASH"
$data ref $data eq "HASH"
Truth table0%0/3
A and B
A= $dataB= ref $data eq "HASH"
ABresult
0X0
100
111
237 0 my $uri = URI->new($url);
238 0 my %query_form = $uri->query_form;
239 0 $uri->query_form(%$data);
240 0 $url = $uri->as_string;
241 }
242 }
243  
244 0 ▶ print STDERR ">>> $request_method $url\n" if $ENV{NG_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NG_DEBUG"} 0 0 0
245 0 my $req = HTTP::Request->new( $request_method, $url );
246 0 $req->accept_decodable;
247 0 ▶ if ($request_method ne 'GET' and $data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request_method ne "GET" and $data) 0 0 0
Condition0%0/3
$request_method ne "GET" and $data
!ll&&!rl&&r
000
MC/DC0%0/2
$request_method ne "GET" and $data
$request_method ne "GET" $data
Truth table0%0/3
A and B
A= $request_method ne "GET"B= $data
ABresult
0X0
100
111
248 0 my $json = $self->json->encode($data);
249 0 ▶ print STDERR ">>> $json\n" if $ENV{NG_DEBUG} and $ENV{NG_DEBUG} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NG_DEBUG"} and $ENV{"NG_DEBUG"} > 1 0 0 0
Condition0%0/3
$ENV{"NG_DEBUG"} and $ENV{"NG_DEBUG"} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"NG_DEBUG"} and $ENV{"NG_DEBUG"} > 1
$ENV{"NG_DEBUG"} $ENV{"NG_DEBUG"} > 1
Truth table0%0/3
A and B
A= $ENV{"NG_DEBUG"}B= $ENV{"NG_DEBUG"} > 1
ABresult
0X0
100
111
250 0 $req->content($json);
251 }
252 0 $req->header( 'Content-Length' => length $req->content );
253  
254 # if preview API, specify a custom media type to Accept header
255 # https://developer.github.com/v3/media/
256 0 ▶ $req->header( 'Accept' => sprintf("application/vnd.github.%s.param+json", $self->accept_version) )
Branch: 1 branch
BranchTrueFalseTotal
if $self->accept_version 0 0 0
257 if $self->accept_version;
258  
259 0 my $res = $self->_make_request($req);
260  
261 # get the rate limit information from the http response headers
262 0 $self->rate_limit( $res->header('x-ratelimit-limit') );
263 0 $self->rate_limit_remaining( $res->header('x-ratelimit-remaining') );
264 0 $self->rate_limit_reset( $res->header('x-ratelimit-reset') );
265  
266 # Slow down if we're approaching the rate limit
267 # By the way GitHub mistakes days for minutes in their documentation --
268 # the rate limit is per minute, not per day.
269 0 ▶ if ( $self->api_throttle ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->api_throttle) 0 0 0
270 0 ▶ sleep 2 if (($self->rate_limit_remaining || 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rate_limit_remaining || 0) < ($self->rate_limit || 60) / 2 0 0 0
Condition0%0/4
$self->rate_limit_remaining || 0
l!l
00
$self->rate_limit || 60
l!l
00
MC/DC0%0/1
$self->rate_limit_remaining || 0
$self->rate_limit_remaining
MC/DC0%0/1
$self->rate_limit || 60
$self->rate_limit
Truth table0%0/2
A
A= $self->rate_limit_remaining
Aresult
11
00
Truth table0%0/2
A
A= $self->rate_limit
Aresult
11
00
271 < ($self->rate_limit || 60) / 2);
272 }
273  
274 0 ▶ print STDERR "<<< " . $res->decoded_content . "\n" if $ENV{NG_DEBUG} and $ENV{NG_DEBUG} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"NG_DEBUG"} and $ENV{"NG_DEBUG"} > 1 0 0 0
Condition0%0/3
$ENV{"NG_DEBUG"} and $ENV{"NG_DEBUG"} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"NG_DEBUG"} and $ENV{"NG_DEBUG"} > 1
$ENV{"NG_DEBUG"} $ENV{"NG_DEBUG"} > 1
Truth table0%0/3
A and B
A= $ENV{"NG_DEBUG"}B= $ENV{"NG_DEBUG"} > 1
ABresult
0X0
100
111
275 0 ▶ return $res if $self->raw_response;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw_response 0 0 0
276 0 ▶ return $res->decoded_content if $self->raw_string;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw_string 0 0 0
277  
278 0 ▶ if ($res->header('Content-Type') and $res->header('Content-Type') =~ 'application/json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->header('Content-Type') and $res->header('Content-Type') =~ m[application/json]) { } 0 0 0
Condition0%0/3
$res->header("Content-Type") and $res->header("Content-Type") =~ m[application/json]
!ll&&!rl&&r
000
MC/DC0%0/2
$res->header("Content-Type") and $res->header("Content-Type") =~ m[application/json]
$res->header("Content-Type") $res->header("Content-Type") =~ m[application/json]
Truth table0%0/3
A and B
A= $res->header("Content-Type")B= $res->header("Content-Type") =~ m[application/json]
ABresult
0X0
100
111
279 0 my $json = $res->decoded_content;
280 0 $data = eval { $self->json->decode($json) };
281 0 ▶ unless ($data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
282 # We tolerate bad JSON for errors,
283 # otherwise we just rethrow the JSON parsing problem.
284 0 ▶ die unless $res->is_error;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_error 0 0 0
285 0 $data = { message => $res->message };
286 }
287 } else {
288 0 $data = { message => $res->message };
289 }
290  
291 0 ▶ if ( $self->RaiseError ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->RaiseError) 0 0 0
292 # check for 'Client Errors'
293 0 ▶ if (not $res->is_success and ref $data eq 'HASH' and exists $data->{message}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res->is_success and ref $data eq "HASH" and exists $data->{"message"}) 0 0 0
Condition0%0/6
not $res->is_success and ref $data eq "HASH" and exists $data->{"message"}
!ll&&!rl&&r
000
not $res->is_success and ref $data eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
not $res->is_success and ref $data eq "HASH" and exists $data->{"message"}
not $res->is_success ref $data eq "HASH" exists $data->{"message"}
Truth table0%0/4
not(A) and B and C
A= not $res->is_successB= ref $data eq "HASH"C= exists $data->{"message"}
ABCresult
0XX0
10X0
1100
1111
294 0 my $message = $data->{message};
295  
296 # Include any additional error information that was returned by the API
297 0 ▶ if (exists $data->{errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"errors"}) 0 0 0
298 $message .= ': '.join(' - ',
299 0 map { $_->{message} }
300 0 grep { exists $_->{message} }
301 0 @{ $data->{errors} });
302 }
303 0 croak $message;
304 }
305 }
306  
307 0 $self->_clear_pagination;
308 0 ▶ if ($res->header('link')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->header("link")) 0 0 0
309 0 my @rel_strs = split ',', $res->header('link');
310 0 $self->_extract_link_url(\@rel_strs);
311 }
312  
313 ## be smarter
314 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
315 0 ▶ return @$data if ref $data eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "ARRAY" 0 0 0
316 0 ▶ return %$data if ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "HASH" 0 0 0
317 }
318  
319 0 return $data;
320 }
321  
322 sub set_next_page {
323 0 ▶ my ($self, $page) = @_;
Subroutine set_next_page: not called CC=4 SCAR=30.0
Pod documented
324  
325 0 ▶ if( ! looks_like_number($page) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($page)) 0 0 0
326 0 croak "Trying to set_next_page to $page, and not a number\n";
327 }
328  
329 0 ▶ if( $page > $self->total_page && $page > 0 ){
Branch: 1 branch
BranchTrueFalseTotal
if ($page > $self->total_page and $page > 0) 0 0 0
Condition0%0/3
$page > $self->total_page and $page > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$page > $self->total_page and $page > 0
$page > $self->total_page $page > 0
Truth table0%0/3
A and B
A= $page > $self->total_pageB= $page > 0
ABresult
0X0
100
111
330 0 return 0;
331 }
332  
333 0 my $temp_url = $self->next_url;
334 0 $temp_url =~ s/([&?])page=[0-9]+([&?]*)/$1page=$page$2/;
335  
336 0 $self->next_url( $temp_url );
337  
338 0 return 1;
339 }
340  
341 sub next_page {
342 0 ▶ my $self = shift;
Subroutine next_page: not called CC=1 SCAR=6.9
Pod documented
343 0 return $self->query($self->next_url);
344 }
345  
346 sub prev_page {
347 0 ▶ my $self = shift;
Subroutine prev_page: not called CC=1 SCAR=6.9
Pod documented
348 0 return $self->query($self->prev_url);
349 }
350  
351 sub first_page {
352 0 ▶ my $self = shift;
Subroutine first_page: not called CC=1 SCAR=6.9
Pod documented
353 0 return $self->query($self->first_url);
354 }
355  
356 sub last_page {
357 0 ▶ my $self = shift;
Subroutine last_page: not called CC=1 SCAR=6.9
Pod documented
358 0 return $self->query($self->last_url);
359 }
360  
361 sub _clear_pagination {
362 0 ▶ my $self = shift;
Subroutine _clear_pagination: not called CC=2 SCAR=17.9
363 0 foreach my $page (qw/first last prev next/) {
364 0 my $clearer = 'clear_' . $page . '_url';
365 0 $self->$clearer;
366 }
367 0 return 1;
368 }
369  
370 sub iterate {
371 0 ▶ my ( $self, $method, $args, $callback ) = @_;
Subroutine iterate: not called CC=13 SCAR=52.0
Pod documented
372  
373 0 ▶ die "This is a method class" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
374 0 ▶ die "Need a method name as second argument" unless defined $method && $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method and $self->can($method) 0 0 0
Condition0%0/3
defined $method and $self->can($method)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $method and $self->can($method)
defined $method $self->can($method)
Truth table0%0/3
A and B
A= defined $methodB= $self->can($method)
ABresult
0X0
100
111
375  
376 0 ▶ die "Missing a callback function as third argument" unless ref $callback eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 0 0
377  
378 0 my @list_args; # 3rd argument
379 0 ▶ if ( ref $args eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args eq 'ARRAY') { } 0 0 0
elsif (ref $args eq 'HASH') { } 0 0 0
380 0 @list_args = @$args;
381 } elsif ( ref $args eq 'HASH' ) {
382 # used for v2 api which are passing a hash of named parameters instead of a list
383 0 @list_args = $args;
384 } else {
385 0 @list_args = $args; # can be undefined [need to preserve it instead of an empty list]
386 }
387  
388 0 my $chunk = $self->can($method)->( $self, $args );
389  
390 0 my $continue = 1;
391 0 ▶ while ( ref $chunk eq 'ARRAY' && scalar @$chunk ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $chunk eq "ARRAY" and scalar @$chunk) 0 0 0
Condition0%0/2
ref $chunk eq "ARRAY" and scalar @$chunk
!ll
00
MC/DC0%0/1
ref $chunk eq "ARRAY" and scalar @$chunk
ref $chunk eq "ARRAY"
Truth table0%0/2
A
A= ref $chunk eq "ARRAY"
Aresult
00
11
392 # process a chunk
393 0 foreach my $item ( @$chunk ) {
394 0 $continue = $callback->( $item );
395 0 ▶ last unless $continue; # user has requested to stop iterating
Branch: 1 branch
BranchTrueFalseTotal
unless $continue 0 0 0
396 }
397 0 ▶ last unless $continue; # user has requested to stop iterating
Branch: 1 branch
BranchTrueFalseTotal
unless $continue 0 0 0
398  
399 # get the next chunk
400 0 ▶ last unless $self->has_next_page;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_next_page 0 0 0
401 0 $chunk = $self->next_page;
402 }
403  
404 0 $self->_clear_pagination;
405  
406 0 return;
407 }
408  
409 sub _extract_link_url {
410 0 ▶ my ($self, $raw_strs) = @_;
Subroutine _extract_link_url: not called CC=6 SCAR=37.4
411 0 foreach my $str (@$raw_strs) {
412 0 my ($link_url, $rel) = split ';', $str;
413  
414 0 $link_url =~ s/^\s*//;
415 0 $link_url =~ s/^<//;
416 0 $link_url =~ s/>$//;
417  
418 0 ▶ if( $rel =~ m/rel="(next|last|first|prev|deprecation|alternate)"/ ){
Branch: 2 branches
BranchTrueFalseTotal
if ($rel =~ /rel="(next|last|first|prev|deprecation|alternate)"/) { } 0 0 0
elsif ($rel =~ /rel="(.*?)"/) { } 0 0 0
419 0 $rel = $1;
420 }
421 elsif( $rel=~ m/rel="(.*?)"/ ){
422 0 warn "Unexpected link rel='$1' in '$str'";
423 0 next;
424 }
425 else {
426 0 warn "Unable to process link rel in '$str'";
427 0 next;
428 }
429  
430 0 ▶ if( $rel eq 'deprecation' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($rel eq "deprecation") 0 0 0
431 0 warn "Deprecation warning: $link_url\n";
432 }
433  
434 0 my $url_attr = $rel . "_url";
435 0 $self->$url_attr($link_url);
436  
437 # Grab, and expose, some additional header information
438 0 ▶ if( $rel eq "last" ){
Branch: 1 branch
BranchTrueFalseTotal
if ($rel eq "last") 0 0 0
439 0 $link_url =~ /[\&?]page=([0-9]*)[\&?]*/;
440 0 $self->total_pages( $1 );
441 }
442 }
443  
444 0 return 1;
445 }
446  
447 sub _make_request {
448 0 ▶ my($self, $req) = @_;
Subroutine _make_request: not called CC=3 SCAR=24.8
449  
450 0 my $cached_res = $self->_get_shared_cache($req->uri);
451  
452 0 ▶ if ($cached_res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached_res) { } 0 0 0
453 0 $req->header("If-None-Match" => $cached_res->header("ETag"));
454 0 my $res = $self->ua->request($req);
455  
456 0 ▶ if ($res->code == 304) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 304) 0 0 0
457 0 return $cached_res;
458 }
459  
460 0 $self->_set_shared_cache($req->uri, $res);
461  
462 0 return $res;
463 } else {
464 0 my $res = $self->ua->request($req);
465 0 $self->_set_shared_cache( $req->uri, $res);
466 0 return $res;
467 }
468 }
469  
470 sub _get_shared_cache {
471 0 ▶ my ($self, $uri) = @_;
Subroutine _get_shared_cache: not called CC=1 SCAR=6.9
472 0 return $self->cache->get($uri);
473 }
474  
475 sub _set_shared_cache {
476 0 ▶ my($self, $uri, $response) = @_;
Subroutine _set_shared_cache: not called CC=1 SCAR=6.9
477 0 $self->cache->set($uri, $response);
478 }
479  
480 ## build methods on fly
481 sub __build_methods {
482 10 ▶ my $package = shift;
Subroutine __build_methods: called CC=9 SCAR=36.2
483 10 my %methods = @_;
484  
485 10 foreach my $m (keys %methods) {
486 204 my $v = $methods{$m};
487 204 my $url = $v->{url};
488 204 ▶ my $method = $v->{method} || 'GET';
Condition100%2/2
$v->{'method'} || 'GET'
l!l
10698
MC/DC100%1/1
$v->{'method'} || 'GET'
$v->{'method'}
Truth table100%2/2
A
A= $v->{'method'}
Aresult
11
00
489 204 ▶ my $args = $v->{args} || 0; # args for ->query
Condition100%2/2
$v->{'args'} || 0
l!l
52152
MC/DC100%1/1
$v->{'args'} || 0
$v->{'args'}
Truth table100%2/2
A
A= $v->{'args'}
Aresult
11
00
490 204 my $check_status = $v->{check_status};
491 204 my $is_u_repo = $v->{is_u_repo}; # need auto shift u/repo
492 204 my $preview_version = $v->{preview};
493 204 my $paginate = $v->{paginate};
494 204 ▶ my $version = $v->{v} || $v->{version} || 1; # version for the accessor
Condition80%4/5
$v->{'v'} || $v->{'version'} || 1
l!l
14190
$v->{"v"} or $v->{"version"}
l!l&&r!l&&!r
140190
MC/DC50%1/2
$v->{'v'} || $v->{'version'} || 1
$v->{"v"} $v->{"version"}
Truth table66%2/3
A or B
A= $v->{"v"}B= $v->{"version"}
ABresult
1X1
011
000
495  
496 # count how much %s inside u
497 204 ▶ my $n = 0; while ($url =~ /\%s/g) { $n++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /\%s/g) 408 204 612
498  
499 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
500 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
501 204 *{"${package}::${m}"} = sub {
502 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=16 SCAR=54.8
503  
504 0 my ( $u, @qargs );
505  
506 0 ▶ if ( $version == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version == 2) { } 0 0 0
507 0 my $opts = {};
508 0 ▶ if ( ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
509 0 my ( $_opts, $_qargs ) = @_;
510  
511 0 $opts = $_opts;
512 0 ▶ if ( my $ref = ref $_qargs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $_qargs) 0 0 0
513 0 ▶ @qargs = @$_qargs if $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 0 0 0
514 0 ▶ @qargs = $_qargs if $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" 0 0 0
515 }
516 } else { # backward compatibility
517 0 my $u = $url;
518 0 ▶ while ( $u =~ s{:([a-z_]+)}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u =~ s/:([a-z_]+)//) 0 0 0
519 0 my $k = $1;
520 #next if defined $opts->{$k};
521 0 $opts->{$k} = shift;
522 0 ▶ die "$k value is not a scalar value $opts->{$k}" if ref $opts->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts->{$k} 0 0 0
523 }
524  
525 0 ▶ @qargs = $args ? splice(@_, 0, $args) : ();
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 0 0 0
526 }
527 # we can now use named :parameter in the url itself
528 0 $u = "$url";
529 {
530 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.2
531 0 $u =~ s{:([a-z_]+)}{$opts->{$1}}g;
532 }
533 } else {
534 ## if is_u_repo, both ($user, $repo, @args) or (@args) should be supported
535 0 ▶ if ( ($is_u_repo or index($url, '/repos/%s/%s') > -1) and @_ < $n + $args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_u_repo || index($url, "/repos/%s/%s") != -1 and @_ < $n + $args) 0 0 0
Condition0%0/6
$is_u_repo || index($url, "/repos/%s/%s") != -1 and @_ < $n + $args
!ll&&!rl&&r
000
$is_u_repo or index($url, "/repos/%s/%s") != -1
l!l&&r!l&&!r
000
MC/DC0%0/3
$is_u_repo || index($url, "/repos/%s/%s") != -1 and @_ < $n + $args
$is_u_repo index($url, "/repos/%s/%s") != -1 @_ < $n + $args
Truth table0%0/5
A or B and C
A= $is_u_repoB= index($url, "/repos/%s/%s") != -1C= @_ < $n + $args
ABCresult
00X0
1X00
0100
1X11
0111
536 0 unshift @_, ($self->u, $self->repo);
537 }
538  
539 # make url, replace %s with real args
540 0 my @uargs = splice(@_, 0, $n);
541 0 $u = sprintf($url, @uargs);
542  
543 # args for json data POST
544 0 ▶ @qargs = $args ? splice(@_, 0, $args) : ();
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 0 0 0
545 }
546  
547 # if preview API, set preview version
548 0 ▶ $self->accept_version($preview_version) if $preview_version;
Branch: 1 branch
BranchTrueFalseTotal
if $preview_version 0 0 0
549  
550 0 ▶ if ($check_status) { # need check Response Status
Branch: 1 branch
BranchTrueFalseTotal
if ($check_status) { } 0 0 0
551 0 my $old_raw_response = $self->raw_response;
552 0 $self->raw_response(1); # need check header
553 0 my $res = $self->query($method, $u, @qargs);
554 0 $self->raw_response($old_raw_response);
555 0 ▶ return index($res->header('Status'), $check_status) > -1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
index($res->header('Status'), $check_status) != -1 ? : 0 0 0
556 } else {
557 0 return $self->query($method, $u, @qargs);
558 }
559 204 };
560 204 ▶ if ($paginate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($paginate) 58 146 204
561 # Add methods next... and close...
562 # Make method names singular (next_comments to next_comment)
563 58 $m =~ s/s$//;
564 58 ▶ my $m_name = ref $paginate ? $paginate->{name} : $m;
Branch: 1 branch
BranchTrueFalseTotal
ref $paginate ? : 2 56 58
565 58 *{"${package}::next_${m_name}"} = sub {
566 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=7 SCAR=40.3
567  
568 # count how much %s inside u
569 0 ▶ my $n = 0; while ($url =~ /\%s/g) { $n++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /\%s/g) 0 0 0
570  
571 ## if is_u_repo, both ($user, $repo, @args) or (@args) should be supported
572 0 ▶ if ( ($is_u_repo or index($url, '/repos/%s/%s') > -1) and @_ < $n + $args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_u_repo || index($url, "/repos/%s/%s") != -1 and @_ < $n + $args) 0 0 0
Condition0%0/6
$is_u_repo || index($url, "/repos/%s/%s") != -1 and @_ < $n + $args
!ll&&!rl&&r
000
$is_u_repo or index($url, "/repos/%s/%s") != -1
l!l&&r!l&&!r
000
MC/DC0%0/3
$is_u_repo || index($url, "/repos/%s/%s") != -1 and @_ < $n + $args
$is_u_repo index($url, "/repos/%s/%s") != -1 @_ < $n + $args
Truth table0%0/5
A or B and C
A= $is_u_repoB= index($url, "/repos/%s/%s") != -1C= @_ < $n + $args
ABCresult
00X0
1X00
0100
1X11
0111
573 0 unshift @_, ($self->u, $self->repo);
574 }
575  
576 # make url, replace %s with real args
577 0 ▶ my @uargs = map { defined $_ ? $_ : '' } splice(@_, 0, $n);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
578 0 my $u = sprintf($url, @uargs);
579  
580 # if preview API, set preview version
581 0 ▶ $self->accept_version($preview_version) if $preview_version;
Branch: 1 branch
BranchTrueFalseTotal
if $preview_version 0 0 0
582  
583 0 return $self->next($u);
584 58 };
585 58 *{"${package}::close_${m_name}"} = sub {
586 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=6 SCAR=37.4
587  
588 # count how much %s inside u
589 0 ▶ my $n = 0; while ($url =~ /\%s/g) { $n++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /\%s/g) 0 0 0
590  
591 ## if is_u_repo, both ($user, $repo, @args) or (@args) should be supported
592 0 ▶ if ( ($is_u_repo or index($url, '/repos/%s/%s') > -1) and @_ < $n + $args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_u_repo || index($url, "/repos/%s/%s") != -1 and @_ < $n + $args) 0 0 0
Condition0%0/6
$is_u_repo || index($url, "/repos/%s/%s") != -1 and @_ < $n + $args
!ll&&!rl&&r
000
$is_u_repo or index($url, "/repos/%s/%s") != -1
l!l&&r!l&&!r
000
MC/DC0%0/3
$is_u_repo || index($url, "/repos/%s/%s") != -1 and @_ < $n + $args
$is_u_repo index($url, "/repos/%s/%s") != -1 @_ < $n + $args
Truth table0%0/5
A or B and C
A= $is_u_repoB= index($url, "/repos/%s/%s") != -1C= @_ < $n + $args
ABCresult
00X0
1X00
0100
1X11
0111
593 0 unshift @_, ($self->u, $self->repo);
594 }
595  
596 # make url, replace %s with real args
597 0 my @uargs = splice(@_, 0, $n);
598 0 my $u = sprintf($url, @uargs);
599  
600 # if preview API, set preview version
601 0 ▶ $self->accept_version($preview_version) if $preview_version;
Branch: 1 branch
BranchTrueFalseTotal
if $preview_version 0 0 0
602  
603 0 $self->close($u);
604 58 };
605 }
606 }
607 }
608  
609 1 ▶no Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
610  
611 1 1;
612 __END__
↑ Summary GitHub/V4.pm »