Analytics/Response.pm

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