WebmasterTools/Response.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 113 SCAR 30.0
CC
4
Cov
0%
project
12.0
_columns
6.0
error_message
2.0
CRAP 20.0
1 package Net::Google::WebmasterTools::Response;
2 {
3 $Net::Google::WebmasterTools::Response::VERSION = '0.01';
4 }
5 use strict;
6 # use Data::Dumper;
7 # use Mojo::Log;
8 # my $log = Mojo::Log->new;
9  
10 # ABSTRACT: Google Webmaster Tools API response
11  
12 use Class::XSAccessor
13 accessors => [ qw(
14 is_success
15 code message content
16 total_results start_index items_per_page
17 contains_sampled_data
18 profile_info
19 rows
20 _column_headers
21 _totals
22 ) ],
23 constructor => 'new';
24  
25 sub error_message {
26 my $self = shift;
27  
28 return join(' ', $self->code, $self->message, $self->content);
29 }
30  
31 sub _parse_json {
32 my ($self, $json) = @_;
33  
34 $self->items_per_page($json->{itemsPerPage});
35 $self->total_results($json->{totalResults});
36 $self->contains_sampled_data($json->{containsSampledData});
37 $self->profile_info($json->{profileInfo});
38  
39 my $json_totals = $json->{totalsForAllResults};
40 my %totals;
41  
42 while (my ($json_name, $total) = each(%$json_totals)) {
43 my $column_name = _parse_column_name($json_name);
44 $totals{$column_name} = $total;
45 }
46  
47 $self->_totals(\%totals);
48  
49 my @column_headers;
50  
51 for my $column_header (@{ $json->{columnHeaders} }) {
52 push(@column_headers, {
53 name => _parse_column_name($column_header->{name}),
54 column_type => $column_header->{columnType},
55 data_type => $column_header->{dataType},
56 });
57 }
58  
59 $self->_column_headers(\@column_headers);
60  
61 my $class = Net::Google::WebmasterTools::Row->_gen_class(\@column_headers);
62  
63 my @rows = map { $class->new($_) } @{ $json->{rows} };
64 $self->rows(\@rows);
65 }
66  
67 sub _parse_column_name {
68 my $name = shift;
69  
70 my ($res) = $name =~ /^(?:ga|rt):(\w{1,64})\z/
71 or die("invalid column name: $name");
72 # convert camel case
73 $res =~ s/[A-Z]/'_' . lc($&)/ge;
74  
75 return $res;
76 }
77  
78 sub metrics {
79 my $self = shift;
80  
81 return $self->_columns('METRIC');
82 }
83  
84 sub dimensions {
85 my $self = shift;
86  
87 return $self->_columns('DIMENSION');
88 }
89  
90 sub totals {
91 my ($self, $metric) = @_;
92 # $log->info(Dumper(@_));
93 return $self->_totals->{$metric};
94 }
95  
96 sub _columns {
97 my ($self, $type) = @_;
98  
99 my $column_headers = $self->_column_headers;
100 my @results;
101  
102 for my $column_header (@$column_headers) {
103 if ($column_header->{column_type} eq $type) {
104 push(@results, $column_header->{name});
105 }
106 }
107  
108 return @results;
109 }
110  
111 sub project {
112 my ($self, $proj_dim_names, $projection) = @_;
113  
114 my (@metric_indices, @proj_column_headers);
115 my $column_headers = $self->_column_headers;
116  
117 for (my $i = 0; $i < @$column_headers; ++$i) {
118 my $column_header = $column_headers->[$i];
119  
120 if ($column_header->{column_type} eq 'METRIC') {
121 push(@metric_indices, $i);
122 push(@proj_column_headers, { %$column_header });
123 }
124 }
125  
126 for my $name (@$proj_dim_names) {
127 push(@proj_column_headers, {
128 name => $name,
129 column_type => 'DIMENSION',
130 });
131 }
132  
133 my $class = Net::Google::WebmasterTools::Row->_gen_class(\@proj_column_headers);
134  
135 # Projected rows are collected in hash %proj_rows. The keys of the hash
136 # are the the projected dimension values joined with zero bytes.
137  
138 my %proj_rows;
139  
140 for my $row (@{ $self->rows }) {
141 my @proj_dim_values = $projection->($row);
142 my $key = join("\0", @proj_dim_values);
143  
144 my $proj_row = $proj_rows{$key};
145  
146 if (!$proj_row) {
147 my @proj_metric_values = map { $row->[$_] } @metric_indices;
148 $proj_rows{$key} = $class->new(
149 [ @proj_metric_values, @proj_dim_values ],
150 );
151 }
152 else {
153 for (my $i = 0; $i < @metric_indices; ++$i) {
154 my $mi = $metric_indices[$i];
155 $proj_row->[$i] += $row->[$mi];
156 }
157 }
158 }
159  
160 $self->rows([ values(%proj_rows) ]);
161 }
162  
163 1;
164  
165 __END__