/usr/share/perl5/Pithub/Result.pm is in libpithub-perl 0.01033-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 | package Pithub::Result;
$Pithub::Result::VERSION = '0.01033';
our $AUTHORITY = 'cpan:PLU';
# ABSTRACT: Github v3 result object
use Moo;
use Array::Iterator;
use JSON::MaybeXS;
use URI;
use Carp;
sub _isa_isa_maker {
my $class = shift;
return sub {
confess "must be an instance of $class but isn't a reference" if !ref $_[0];
confess "must be an instance of $class, but is a ".ref $_[0]
unless eval { $_[0]->isa($class) };
};
}
has 'auto_pagination' => (
default => sub { 0 },
is => 'rw',
);
has 'content' => (
builder => '_build_content',
clearer => 'clear_content',
is => 'ro',
lazy => 1,
);
has 'first_page_uri' => (
builder => '_build_first_page_uri',
clearer => 'clear_first_page_uri',
is => 'ro',
lazy => 1,
);
has 'last_page_uri' => (
builder => '_build_last_page_uri',
clearer => 'clear_last_page_uri',
is => 'ro',
lazy => 1,
);
has 'next_page_uri' => (
builder => '_build_next_page_uri',
clearer => 'clear_next_page_uri',
is => 'ro',
lazy => 1,
);
has 'prev_page_uri' => (
builder => '_build_prev_page_uri',
clearer => 'clear_prev_page_uri',
is => 'ro',
lazy => 1,
);
has 'response' => (
handles => {
code => 'code',
raw_content => 'content',
request => 'request',
success => 'is_success',
},
is => 'ro',
isa => _isa_isa_maker('HTTP::Response'),
required => 1,
);
# required for next_page etc
has '_request' => (
is => 'ro',
isa => sub {
croak 'must be a coderef, but is ' . ref $_[0] unless ref $_[0] eq 'CODE'
},
required => 1,
);
# required for next
has '_iterator' => (
builder => '_build__iterator',
clearer => '_clear_iterator',
is => 'ro',
isa => _isa_isa_maker('Array::Iterator'),
lazy => 1,
);
has 'utf8' => (
is => 'ro',
default => 1,
);
has '_json' => (
builder => '_build__json',
is => 'ro',
isa => sub {
confess "$_[0] is not a suitable JSON object"
unless eval { $_[0]->can("decode") };
},
lazy => 1,
);
sub count {
my ($self) = @_;
return 0 unless $self->success;
my $content = $self->content;
if ( ref $content eq 'HASH' && scalar keys %$content == 0 ) {
return 0;
}
return $self->_iterator->getLength;
}
sub first {
my ($self) = @_;
my $content = $self->content;
if ( ref $content eq 'ARRAY' ) {
return $content->[0];
}
return $content;
}
sub first_page {
my ($self) = @_;
return unless $self->first_page_uri;
return $self->_paginate( $self->first_page_uri );
}
sub get_page {
my ( $self, $page ) = @_;
# First we need to get an URI we can work with and replace
# the page GET parameter properly with the given value. If
# we cannot get the first or last page URI, then there is
# only one page.
my $uri_str = $self->first_page_uri || $self->last_page_uri;
return unless $uri_str;
my $uri = URI->new($uri_str);
my %query = $uri->query_form;
$query{page} = $page;
my $options = {
prepare_request => sub {
my ($request) = @_;
%query = ( $request->uri->query_form, %query );
$request->uri->query_form(%query);
},
};
return $self->_request->(
method => 'GET',
path => $uri->path,
options => $options,
);
}
sub last_page {
my ($self) = @_;
return unless $self->last_page_uri;
return $self->_paginate( $self->last_page_uri );
}
sub next {
my ($self) = @_;
my $row = $self->_iterator->getNext;
return $row if $row;
if ( $self->auto_pagination ) {
my $result = $self->next_page;
return unless $result;
$self->_reset;
$self->{response} = $result->response;
return $self->_iterator->getNext;
}
return;
}
sub next_page {
my ($self) = @_;
return unless $self->next_page_uri;
return $self->_paginate( $self->next_page_uri );
}
sub prev_page {
my ($self) = @_;
return unless $self->prev_page_uri;
return $self->_paginate( $self->prev_page_uri );
}
sub etag {
my ($self) = @_;
return $self->response->header('ETag');
}
sub ratelimit {
my ($self) = @_;
return $self->response->header('X-RateLimit-Limit');
}
sub ratelimit_remaining {
my ($self) = @_;
return $self->response->header('X-RateLimit-Remaining');
}
sub _build_content {
my ($self) = @_;
if ( $self->raw_content ) {
return $self->_json->decode( $self->raw_content );
}
return {};
}
sub _build_first_page_uri {
return shift->_get_link_header('first');
}
sub _build__iterator {
my ($self) = @_;
my $content = $self->content;
$content = [$content] unless ref $content eq 'ARRAY';
return Array::Iterator->new($content);
}
sub _build_last_page_uri {
return shift->_get_link_header('last');
}
sub _build_next_page_uri {
return shift->_get_link_header('next');
}
sub _build_prev_page_uri {
return shift->_get_link_header('prev');
}
sub _build__json {
my ($self) = @_;
return JSON->new->utf8($self->utf8);
}
sub _get_link_header {
my ( $self, $type ) = @_;
return $self->{_get_link_header}{$type} if $self->{_get_link_header}{$type};
my $link = $self->response->header('Link');
return unless $link;
return unless $link =~ /(next|first|last|prev)/;
foreach my $item ( split /,/, $link ) {
my @result = $item =~ /<([^>]+)>; rel="([^"]+)"/g;
next if !$result[1] || !$result[0];
$self->{_get_link_header}{ $result[1] } = $result[0];
}
return $self->{_get_link_header}{$type};
}
sub _paginate {
my ( $self, $uri_str ) = @_;
my $uri = URI->new($uri_str);
my $options = {
prepare_request => sub {
my ($request) = @_;
my %query = ( $request->uri->query_form, $uri->query_form );
$request->uri->query_form(%query);
},
};
return $self->_request->(
method => 'GET',
path => $uri->path,
options => $options,
);
}
sub _reset {
my ($self) = @_;
$self->clear_content;
$self->clear_first_page_uri;
$self->clear_last_page_uri;
$self->clear_next_page_uri;
$self->clear_prev_page_uri;
$self->_clear_iterator;
delete $self->{_get_link_header};
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Pithub::Result - Github v3 result object
=head1 VERSION
version 0.01033
=head1 DESCRIPTION
Every method call which maps directly to a Github API call returns a
L<Pithub::Result> object. Once you got the result object, you can
set L<attributes|/ATTRIBUTES> on them or call L<methods|/METHODS>.
=head1 ATTRIBUTES
=head2 auto_pagination
If you set this to true and use the L</next> method to iterate
over the result rows, it will call automatically L</next_page>
for you until you got all the results. Be careful using this
feature, if there are 100 pages, this will make 100 API calls.
By default it's off. Instead of setting it per L<Pithub::Result>
you can also set it directly on any of the L<Pithub> API objects.
Examples:
my $r = Pithub::Repos->new;
my $result = $r->list( user => 'rjbs' );
# This would just show the first 30 by default
while ( my $row = $result->next ) {
printf "%s: %s\n", $row->{name}, $row->{description};
}
# Let's do the same thing using auto_pagination to fetch all
$result = $r->list( user => 'rjbs' );
$result->auto_pagination(1);
while ( my $row = $result->next ) {
printf "%s: %s\n", $row->{name}, $row->{description};
}
# Turn auto_pagination on for all L<Pithub::Result> objects
my $p = Pithub::Repos->new( auto_pagination => 1 );
my $result = $p->list( user => 'rjbs' );
while ( my $row = $result->next ) {
printf "%s: %s\n", $row->{name}, $row->{description};
}
=head2 content
The decoded JSON response. May be an arrayref or hashref, depending
on the API call. For some calls there is no content at all.
=head2 first_page_uri
The extracted value from the C<< Link >> header for the first page.
This can return undef.
=head2 last_page_uri
The extracted value from the C<< Link >> header for the last page.
This can return undef.
=head2 next_page_uri
The extracted value from the C<< Link >> header for the next page.
This can return undef.
=head2 prev_page_uri
The extracted value from the C<< Link >> header for the previous
page. This can return undef.
=head2 response
The L<HTTP::Response> object.
=head2 utf8
This can set utf8 flag.
=head1 METHODS
=head2 raw_content
Returns the content of the API response as a string, it will probably
be JSON.
=head2 request
Returns the L<HTTP::Request> object used to make the API call.
=head2 code
Returns the HTTP code from the API call.
=head2 success
Returns whether the API call was successful.
=head2 count
Returns the count of the elements in L</content>. If the result is
not an arrayref but a hashref, it will still return C<< 1 >>. Some
calls return an empty hashref, for those calls it returns C<< 0 >>.
=head2 first
Return the first element from L</content> if L</content> is an
arrayref. If it's a hashref, it returns just that.
=head2 first_page
Get the L<Pithub::Result> of the first page. Returns undef if there
is no first page (if you're on the first page already or if there
is no pages at all).
=head2 get_page
Get the L<Pithub::Result> for a specific page. The parameter is not
validated, if you hit a page that does not exist, the Github API will
tell you so. If there is only one page, this method will return
undef, no matter which page you ask for, even for page 1.
=head2 last_page
Get the L<Pithub::Result> of the last page. Returns undef if there
is no last page (if you're on the last page already or if there
is only one page or no pages at all).
=head2 next
Most of the results returned by the Github API calls are arrayrefs
of hashrefs. The data structures can be retrieved directly by
calling L</content>. Besides that it's possible to iterate over
the results using this method.
Examples:
my $r = Pithub::Repos->new;
my $result = $r->list( user => 'rjbs' );
while ( my $row = $result->next ) {
printf "%s: %s\n", $row->{name}, $row->{description};
}
=head2 next_page
Get the L<Pithub::Result> of the next page. Returns undef if there
is no next page (there's only one page at all).
Examples:
=over
=item *
List all followers in order, from the first one on the first
page to the last one on the last page. See also
L</auto_pagination>.
my $followers = Pithub->new->users->followers;
my $result = $followers->list( user => 'rjbs' );
do {
if ( $result->success ) {
while ( my $row = $result->next ) {
printf "%s\n", $row->{login};
}
}
} while $result = $result->next_page;
The nature of the implementation requires you here to do a
C<< do { ... } while ... >> loop. If you're going to fetch
all results of all pages, I suggest to use the
L</auto_pagination> feature, it's much more convenient.
=back
=head2 prev_page
Get the L<Pithub::Result> of the previous page. Returns undef if there
is no previous page (you're on the first page).
Examples:
=over
=item *
List all followers in reverse order, from the last one on the last
page to the first one on the first page. See also
L</auto_pagination>.
my $followers = Pithub->new->users->followers;
my $result = $followers->list( user => 'rjbs' )->last_page; # this makes two requests!
do {
if ( $result->success ) {
while ( my $row = $result->next ) {
printf "%s\n", $row->{login};
}
}
} while $result = $result->prev_page;
=back
=head2 etag
Returns the value of the C<< ETag >> http header.
=head2 ratelimit
Returns the value of the C<< X-Ratelimit-Limit >> http header.
=head2 ratelimit_remaining
Returns the value of the C<< X-Ratelimit-Remaining >> http header.
=head1 AUTHOR
Johannes Plunien <plu@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Johannes Plunien.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|