Code/Role/Pageable.pm

filter: Statementstmt 94.2% 82 / 87 Branchbran 71.7% 33 / 46 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 86.5% 129 / 149 CC 28 SCAR 26.7
CC
28
Cov
86%
rows
19.8
_rows
13.0
BEGIN
1.0
CRAP 14.5
« Code/Role/Authentication.pm ↑ Summary Code/Issue/Search.pm »
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
l!l
31
MC/DC100%1/1
$args{"limit"} ||= 999999999
$args{"limit"}
Truth table100%2/2
A
A= $args{"limit"}
Aresult
11
00
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
BranchTrueFalseTotal
unless builtin::blessed($tree) 4 0 4
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
BranchTrueFalseTotal
if ($title_tag) { } 26 4 30
33 26 my $title = $title_tag->as_text;
34 26 ▶ if ( $title eq "\x{a0}" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title eq "\240") 3 23 26
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
BranchTrueFalseTotal
if ($title =~ /(\w+)/) 26 0 26
40 26 push @titles, lc $1;
41  
42 26 ▶ if ( $title =~ /label/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title =~ /label/i) 4 22 26
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
BranchTrueFalseTotal
unless @titles 0 4 4
53  
54 4 my @rows;
55  
56 4 my $pagination = $tree->look_down( class => 'pagination' );
57 4 ▶ return unless $pagination;
Branch: 1 branch
BranchTrueFalseTotal
unless $pagination 0 4 4
58  
59 4 ▶ if ( $pagination->as_text =~ /\d+\s+-\s+\d+\s+of\s+\d+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagination->as_text =~ /\d+\s+-\s+\d+\s+of\s+\d+/) 4 0 4
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
BranchTrueFalseTotal
if (scalar @rows < $args{"limit"}) 4 0 4
68 4 my $next_link = $self->mech->find_link( text_regex => qr/Next\s+/ );
69 4 ▶ if ($next_link) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_link) { } 0 4 4
70 0 $self->mech->get( $next_link->url );
71 0 ▶ if ( $self->mech->response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mech->response->is_success) { } 0 0 0
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
BranchTrueFalseTotal
if $need_delete 4 0 4
89 4 ▶ if ( scalar @rows > $args{limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @rows > $args{'limit'}) { } 0 4 4
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
BranchTrueFalseTotal
unless builtin::blessed($tree) 0 4 4
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
BranchTrueFalseTotal
if ($i < @titles) 26 4 30
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
BranchTrueFalseTotal
if ($j < @tags) 202 26 228
122 202 my $column = $tags[$j]->as_text;
123 202 ▶ next unless $column =~ /[-\w]/; # skip the '›' thing or alike
Branch: 1 branch
BranchTrueFalseTotal
unless $column =~ /[-\w]/ 24 178 202
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
BranchTrueFalseTotal
if $column eq "----" 51 127 178
132  
133 178 ▶ if ( $i == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 26 152 178
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
BranchTrueFalseTotal
if ($label_column and $i == $label_column) 26 152 178
Condition66%2/3
$label_column and $i == $label_column
!ll&&!rl&&r
015226
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
ABresult
0X0
100
111
141 26 my @labels;
142 26 ▶ if (@elements) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elements) 8 18 26
143 8 @labels = split /\s+/, shift @elements;
144 }
145 26 ▶ $rows[$k]{labels} = \@labels if @labels;
Branch: 1 branch
BranchTrueFalseTotal
if @labels 8 18 26
146 }
147 178 $k++;
148 }
149 }
150 4 ▶ $tree->delete if $need_delete;
Branch: 1 branch
BranchTrueFalseTotal
if $need_delete 0 4 4
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__
« Code/Role/Authentication.pm ↑ Summary Code/Issue/Search.pm »