| 1 | package WWW::AUR::Iterator; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use warnings 'FATAL' => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use WWW::AUR::Package qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use WWW::AUR::URI qw( pkg_uri ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use WWW::AUR qw( _path_params _category_name _useragent ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | sub new | ||||||||||
| 11 | { | ||||||||||
| 12 | 1 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 13 | 1 | my $self = bless {}, $class; | |||||||||
| 14 | 1 | $self->init( @_ ); | |||||||||
| 15 | } | ||||||||||
| 16 | |||||||||||
| 17 | sub init | ||||||||||
| 18 | { | ||||||||||
| 19 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
init: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||
| 20 | |||||||||||
| 21 | 1 | my $startidx; | |||||||||
| 22 | 1 | ▶ | $startidx = shift if @_ % 2 == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | |||||||||||
| 24 | 1 | %$self = ( %$self, _path_params( @_ )); | |||||||||
| 25 | 1 | $self->reset(); | |||||||||
| 26 | 1 | ▶ | $self->set_pos( $startidx ) if $startidx; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | |||||||||||
| 28 | 1 | return $self; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | sub set_pos | ||||||||||
| 32 | { | ||||||||||
| 33 | 0 | ▶ | my ($self, $startidx) = @_; | ||||||||
|
Subroutine
set_pos: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 34 | 0 | ▶ | Carp::croak 'Argument to set_pos() must an integer' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | unless $startidx =~ /\A\d+\z/; | ||||||||||
| 36 | |||||||||||
| 37 | 0 | $self->{'curridx'} = $startidx; | |||||||||
| 38 | 0 | $self->{'finished'} = 0; | |||||||||
| 39 | 0 | $self->{'packages'} = []; | |||||||||
| 40 | |||||||||||
| 41 | 0 | return; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | sub reset | ||||||||||
| 45 | { | ||||||||||
| 46 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 47 | 1 | $self->{'curridx'} = 0; | |||||||||
| 48 | 1 | $self->{'finished'} = 0; | |||||||||
| 49 | 1 | $self->{'packages'} = []; | |||||||||
| 50 | 1 | $self->{'useragent'} = _useragent(); | |||||||||
| 51 | 1 | return; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | #---HELPER FUNCTION--- | ||||||||||
| 55 | sub _pkglist_uri | ||||||||||
| 56 | { | ||||||||||
| 57 | 1 | ▶ | my ($startidx) = @_; | ||||||||
|
Subroutine
_pkglist_uri: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 1 | return pkg_uri( q{SB} => q{n}, q{O} => $startidx, | |||||||||
| 59 | q{SO} => q{a}, q{PP} => 100 ); | ||||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | #---PRIVATE METHOD--- | ||||||||||
| 63 | sub _scrape_pkglist | ||||||||||
| 64 | { | ||||||||||
| 65 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_scrape_pkglist: called CC=3 SCAR=16.3
| |||||||||||
| 66 | |||||||||||
| 67 | 1 | my $uri = _pkglist_uri( $self->{'curridx'} ); | |||||||||
| 68 | 1 | my $resp = $self->{'useragent'}->get( $uri ); | |||||||||
| 69 | |||||||||||
| 70 | 1 | ▶ | Carp::croak 'Failed to GET package list webpage: ' . $resp->status_line | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | unless $resp->is_success; | ||||||||||
| 72 | |||||||||||
| 73 | 0 | my @pkginfos; | |||||||||
| 74 | 0 | my @rows = _splitrows( $resp->content ); | |||||||||
| 75 | 0 | shift @rows; # remove the header column | |||||||||
| 76 | |||||||||||
| 77 | 0 | for my $rowhtml ( @rows ) { | |||||||||
| 78 | 0 | my @cols = _splitcols( $rowhtml ); | |||||||||
| 79 | |||||||||||
| 80 | # cat, name, version, votes, desc, maintainer | ||||||||||
| 81 | 0 | push @pkginfos, @cols; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 0 | return \@pkginfos; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | sub _splitrows | ||||||||||
| 88 | { | ||||||||||
| 89 | 0 | ▶ | my ($html) = @_; | ||||||||
|
Subroutine
_splitrows: not called CC=1 SCAR=6.9
| |||||||||||
| 90 | 0 | my @rows = $html =~ m{ <tr[^>]*> ( .*? ) </tr> }gxs; | |||||||||
| 91 | 0 | return @rows; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | sub _splitcols | ||||||||||
| 95 | { | ||||||||||
| 96 | 0 | ▶ | my ($rowhtml) = @_; | ||||||||
|
Subroutine
_splitcols: not called CC=2 SCAR=17.9
| |||||||||||
| 97 | 0 | my @cols = $rowhtml =~ m{ <td[^>]*> ( .*? ) </td> }gxs; | |||||||||
| 98 | 0 | for ( @cols ) { | |||||||||
| 99 | 0 | s/<[^>]+>//g; # delete tags | |||||||||
| 100 | 0 | s/\A\s+//; s/\s+\z//; # trim whitespace | |||||||||
| 101 | } | ||||||||||
| 102 | 0 | return @cols; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | sub next | ||||||||||
| 106 | { | ||||||||||
| 107 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
next: called CC=6 SCAR=28.5
Pod
documented
| |||||||||||
| 108 | |||||||||||
| 109 | # There are no more packages to iterate over... | ||||||||||
| 110 | 1 | ▶ | return undef if $self->{'finished'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | |||||||||||
| 112 | 1 | my @pkginfo = splice @{ $self->{'packages'} }, 0, 6; | |||||||||
| 113 | 1 | ▶ | if ( @pkginfo ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 0 | my $pkg; | |||||||||
| 115 | 0 | my @k = qw/cat name version votes desc/; | |||||||||
| 116 | 0 | for my $i (0 .. $#k) { | |||||||||
| 117 | 0 | $pkg->{$k[$i]} = $pkginfo[$i]; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 0 | my $maint = $pkginfo[5]; | |||||||||
| 121 | 0 | ▶ | $pkg->{'maint'} = ($maint eq 'orphan' ? undef : $maint); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 0 | return $pkg; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | # Load a new batch of packages if our internal list is empty... | ||||||||||
| 126 | 1 | my $newpkgs = $self->_scrape_pkglist; | |||||||||
| 127 | |||||||||||
| 128 | 0 | $self->{'curridx'} += 100; | |||||||||
| 129 | 0 | $self->{'packages'} = $newpkgs; | |||||||||
| 130 | 0 | ▶ | $self->{'finished'} = 1 if scalar @$newpkgs == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | |||||||||||
| 132 | # Recurse, just avoids code copy/pasting... | ||||||||||
| 133 | 0 | return $self->next(); | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | sub next_obj | ||||||||||
| 137 | { | ||||||||||
| 138 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
next_obj: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 139 | |||||||||||
| 140 | 0 | my $next = $self->next; | |||||||||
| 141 | return ( $next | ||||||||||
| 142 | 0 | ▶ | ? WWW::AUR::Package->new( $next->{'name'}, %$self ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | : undef ); | ||||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | 1 | 1; | |||||||||
| 147 | |||||||||||
| 148 | __END__ | ||||||||||