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