| 1 | package Net::Google::Code::Role::Pageable; | ||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use Any::Moose 'Role'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use Params::Validate ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use WWW::Mechanize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 10 | with 'Net::Google::Code::Role::Fetchable'; | |||||||||||||||||||||||||||
| 6 | 10 | with 'Net::Google::Code::Role::HTMLTree'; | |||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | sub rows { | ||||||||||||||||||||||||||
|
Subroutine
rows: called CC=16 SCAR=29.9
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 11 | 4 | my %args = validate( | |||||||||||||||||||||||||||
| 12 | @_, | ||||||||||||||||||||||||||||
| 13 | { | ||||||||||||||||||||||||||||
| 14 | html => { type => SCALAR | OBJECT }, | ||||||||||||||||||||||||||||
| 15 | limit => { | ||||||||||||||||||||||||||||
| 16 | type => SCALAR | UNDEF, | ||||||||||||||||||||||||||||
| 17 | optional => 1, | ||||||||||||||||||||||||||||
| 18 | }, | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | ▶ | $args{limit} ||= 999_999_999; # the impossible huge limit | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"limit"} ||= 999999999
MC/DC100%1/1
$args{"limit"} ||= 999999999
$args{"limit"}
Truth table100%2/2
A
A= $args{"limit"}
| |||||||||||||||||||||||||||||
| 23 | 4 | my $tree = $args{html}; | |||||||||||||||||||||||||||
| 24 | 4 | my $need_delete = not blessed $tree; | |||||||||||||||||||||||||||
| 25 | 4 | ▶ | $tree = $self->html_tree( html => $tree ) unless blessed $tree; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # assuming there's at most 20 columns | ||||||||||||||||||||||||||||
| 28 | 4 | my @titles; | |||||||||||||||||||||||||||
| 29 | my $label_column; | ||||||||||||||||||||||||||||
| 30 | 4 | for my $num ( 0 .. 20 ) { | |||||||||||||||||||||||||||
| 31 | 30 | my $title_tag = $tree->look_down( class => "col_$num" ); | |||||||||||||||||||||||||||
| 32 | 30 | ▶ | if ( $title_tag ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 26 | my $title = $title_tag->as_text; | |||||||||||||||||||||||||||
| 34 | 26 | ▶ | if ( $title eq "\x{a0}" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 3 | $title_tag = ($tree->look_down( class => "col_$num" ))[1]; | |||||||||||||||||||||||||||
| 36 | 3 | $title = $title_tag->as_text; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 26 | ▶ | if ( $title =~ /(\w+)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 26 | push @titles, lc $1; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 26 | ▶ | if ( $title =~ /label/i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 4 | $label_column = $num; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | else { | ||||||||||||||||||||||||||||
| 48 | 4 | last; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 4 | ▶ | die "no idea what the column spec is" unless @titles; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 4 | my @rows; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 4 | my $pagination = $tree->look_down( class => 'pagination' ); | |||||||||||||||||||||||||||
| 57 | 4 | ▶ | return unless $pagination; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 4 | ▶ | if ( $pagination->as_text =~ /\d+\s+-\s+\d+\s+of\s+\d+/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | # all the rows in a page | ||||||||||||||||||||||||||||
| 61 | 4 | push @rows, $self->_rows( | |||||||||||||||||||||||||||
| 62 | html => $tree, | ||||||||||||||||||||||||||||
| 63 | titles => \@titles, | ||||||||||||||||||||||||||||
| 64 | label_column => $label_column, | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 4 | ▶ | while ( scalar @rows < $args{limit} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 4 | my $next_link = $self->mech->find_link( text_regex => qr/Next\s+/ ); | |||||||||||||||||||||||||||
| 69 | 4 | ▶ | if ($next_link) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | $self->mech->get( $next_link->url ); | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ( $self->mech->response->is_success ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | push @rows, $self->_rows( | |||||||||||||||||||||||||||
| 73 | html => $self->mech->content, | ||||||||||||||||||||||||||||
| 74 | titles => \@titles, | ||||||||||||||||||||||||||||
| 75 | label_column => $label_column, | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 0 | die "failed to follow 'Next' link"; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||||||||||
| 83 | 4 | last; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | ▶ | $tree->delete if $need_delete; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 4 | ▶ | if ( scalar @rows > $args{limit} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | # this happens when limit is less than the 1st page's number, so in | ||||||||||||||||||||||||||||
| 91 | # some similar situations | ||||||||||||||||||||||||||||
| 92 | 0 | return @rows[0 .. $args{limit}-1]; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||
| 95 | 4 | return @rows; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 4 | ▶ | sub _rows { | ||||||||||||||||||||||||||
|
Subroutine
_rows: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 100 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 4 | my %args = validate( | |||||||||||||||||||||||||||
| 102 | @_, | ||||||||||||||||||||||||||||
| 103 | { | ||||||||||||||||||||||||||||
| 104 | html => { type => SCALAR | OBJECT }, | ||||||||||||||||||||||||||||
| 105 | titles => { type => ARRAYREF, }, | ||||||||||||||||||||||||||||
| 106 | label_column => { optional => 1 }, | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||
| 109 | 4 | my $tree = $args{html}; | |||||||||||||||||||||||||||
| 110 | 4 | my $need_delete = not blessed $tree; | |||||||||||||||||||||||||||
| 111 | 4 | ▶ | $tree = $self->html_tree( html => $tree ) unless blessed $tree; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 4 | my @titles = @{$args{titles}}; | |||||||||||||||||||||||||||
| 113 | 4 | my $label_column = $args{label_column}; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 4 | my @columns; | |||||||||||||||||||||||||||
| 116 | my @rows; | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 4 | ▶ | for ( my $i = 0 ; $i < @titles ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 26 | my @tags = $tree->look_down( class => qr/^vt (id )?col_$i/ ); | |||||||||||||||||||||||||||
| 120 | 26 | my $k = 0; | |||||||||||||||||||||||||||
| 121 | 26 | ▶ | for ( my $j = 0 ; $j < @tags ; $j++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 202 | my $column = $tags[$j]->as_text; | |||||||||||||||||||||||||||
| 123 | 202 | ▶ | next unless $column =~ /[-\w]/; # skip the '›' thing or alike | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 178 | my @elements = split /\x{a0}/, $column; | |||||||||||||||||||||||||||
| 126 | 178 | for ( @elements ) { | |||||||||||||||||||||||||||
| 127 | 186 | s/^\s+//; | |||||||||||||||||||||||||||
| 128 | 186 | s/\s+$//; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 178 | $column = shift @elements; | |||||||||||||||||||||||||||
| 131 | 178 | ▶ | $column = '' if $column eq '----'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 178 | ▶ | if ( $i == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 26 | push @rows, { $titles[0] => $column }; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||
| 137 | 152 | $rows[$k]{ $titles[$i] } = $column; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 178 | ▶ | if ( $label_column && $i == $label_column ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$label_column and $i == $label_column
MC/DC50%1/2
$label_column and $i == $label_column
$label_column
$i == $label_column
Truth table66%2/3
A and B
A= $label_columnB= $i == $label_column
| |||||||||||||||||||||||||||||
| 141 | 26 | my @labels; | |||||||||||||||||||||||||||
| 142 | 26 | ▶ | if (@elements) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 8 | @labels = split /\s+/, shift @elements; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 26 | ▶ | $rows[$k]{labels} = \@labels if @labels; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 178 | $k++; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 4 | ▶ | $tree->delete if $need_delete; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 4 | return @rows; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 10 | ▶ | no Any::Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 155 | 10 | 1; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||