Analytics/Response.pm

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