Analytics.pm

filter: Statementstmt 67.3% 68 / 101 Branchbran 42.8% 12 / 28 Conditioncond 25.0% 2 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 80.0% 16 / 20 Podpod 100% 9 / 9 totaltotal 63.1% 108 / 171 CC 18 SCAR 34.4
CC
18
Cov
60%
retrieve_paged
110.0
_uri
4.0
auth_params
3.0
CRAP 31.2
↑ Summary Analytics/OAuth2.pm »
1 package Net::Google::Analytics;
2 2 $Net::Google::Analytics::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: Simple interface to the Google Analytics Core Reporting API
7  
8 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Net::Google::Analytics::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Net::Google::Analytics::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::Google::Analytics::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub new {
16 2 ▶ my $package = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
17  
18 2 my $self = bless({}, $package);
19  
20 2 return $self;
21 }
22  
23 sub auth_params {
24 4 ▶ my $self = shift;
Subroutine auth_params: called CC=3 SCAR=11.0
Pod documented
25  
26 4 ▶ my $auth_params = $self->{auth_params} || [];
Condition100%2/2
$self->{'auth_params'} || []
l!l
22
MC/DC100%1/1
$self->{'auth_params'} || []
$self->{'auth_params'}
Truth table100%2/2
A
A= $self->{'auth_params'}
Aresult
11
00
27  
28 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 2 4
29 2 $self->{auth_params} = [ @_ ];
30 }
31  
32 4 return @$auth_params;
33 }
34  
35 sub token {
36 0 ▶ my ($self, $token) = @_;
Subroutine token: not called CC=1 SCAR=6.9
Pod documented
37  
38 $self->{auth_params} = [
39 0 Authorization => "$token->{token_type} $token->{access_token}",
40 ];
41 }
42  
43 sub user_agent {
44 6 ▶ my $self = $_[0];
Subroutine user_agent: called CC=3 SCAR=11.0
Pod documented
45  
46 6 my $ua = $self->{user_agent};
47  
48 6 ▶ if (@_ > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 2 4 6
elsif (not defined $ua) { } 2 2 4
49 2 $self->{user_agent} = $_[1];
50 }
51 elsif (!defined($ua)) {
52 2 $ua = LWP::UserAgent->new();
53 2 $self->{user_agent} = $ua;
54 }
55  
56 6 return $ua;
57 }
58  
59 sub new_request {
60 2 ▶ my $self = shift;
Subroutine new_request: called CC=1 SCAR=0.0
Pod documented
61  
62 2 return Net::Google::Analytics::Request->new(@_);
63 }
64  
65 sub _uri {
66 2 ▶ my ($self, $req, $start_index, $max_results) = @_;
Subroutine _uri: called CC=4 SCAR=13.9
67  
68 2 ▶ my $service = $req->realtime ? 'realtime' : 'ga';
Branch: 1 branch
BranchTrueFalseTotal
$req->realtime ? : 1 1 2
69 2 my $uri = URI->new(
70 "https://www.googleapis.com/analytics/v3/data/$service"
71 );
72 2 my @params;
73 2 ▶ push(@params, 'start-index' => $start_index) if defined($start_index);
Branch: 1 branch
BranchTrueFalseTotal
if defined $start_index 1 1 2
74 2 ▶ push(@params, 'max-results' => $max_results) if defined($max_results);
Branch: 1 branch
BranchTrueFalseTotal
if defined $max_results 2 0 2
75  
76 2 $uri->query_form(
77 $req->_params,
78 @params,
79 );
80  
81 2 return $uri;
82 }
83  
84 sub uri {
85 0 ▶ my ($self, $req) = @_;
Subroutine uri: not called CC=1 SCAR=6.9
Pod documented
86  
87 0 return $self->_uri($req, $req->start_index, $req->max_results);
88 }
89  
90 sub _retrieve_http {
91 2 ▶ my ($self, $req, $start_index, $max_results) = @_;
Subroutine _retrieve_http: called CC=1 SCAR=0.0
92  
93 2 my $uri = $self->_uri($req, $start_index, $max_results);
94 2 my @auth_params = $self->auth_params;
95  
96 2 return $self->user_agent->get($uri->as_string, @auth_params);
97 }
98  
99 sub retrieve_http {
100 0 ▶ my ($self, $req) = @_;
Subroutine retrieve_http: not called CC=1 SCAR=6.9
Pod documented
101  
102 0 return $self->_retrieve_http($req, $req->start_index, $req->max_results);
103 }
104  
105 sub _retrieve {
106 2 ▶ my ($self, $req, $start_index, $max_results) = @_;
Subroutine _retrieve: called CC=2 SCAR=7.1
107  
108 2 my $http_res = $self->_retrieve_http($req, $start_index, $max_results);
109 2 my $res = Net::Google::Analytics::Response->new;
110 2 $res->code($http_res->code);
111 2 $res->message($http_res->message);
112  
113 2 ▶ if (!$http_res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($http_res->is_success) 0 2 2
114 0 $res->content($http_res->decoded_content);
115 0 return $res;
116 }
117  
118 2 my $json = from_json($http_res->decoded_content);
119 2 $res->_parse_json($json);
120  
121 2 $res->start_index($start_index);
122 2 $res->is_success(1);
123  
124 2 return $res;
125 }
126  
127 sub retrieve {
128 2 ▶ my ($self, $req) = @_;
Subroutine retrieve: called CC=1 SCAR=0.0
Pod documented
129  
130 2 return $self->_retrieve($req, $req->start_index, $req->max_results);
131 }
132  
133 sub retrieve_paged {
134 0 ▶ my ($self, $req) = @_;
Subroutine retrieve_paged: not called CC=10 SCAR=47.0
Pod documented
135  
136 0 my $start_index = $req->start_index;
137 0 ▶ $start_index = 1 if !defined($start_index);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start_index 0 0 0
138 0 my $remaining_items = $req->max_results;
139 0 my $max_items_per_page = 10_000;
140 0 my $res;
141  
142 0 ▶ while (!defined($remaining_items) || $remaining_items > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $remaining_items or $remaining_items > 0) 0 0 0
Condition0%0/3
not defined $remaining_items or $remaining_items > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $remaining_items or $remaining_items > 0
not defined $remaining_items $remaining_items > 0
Truth table0%0/3
not(A) or B
A= not defined $remaining_itemsB= $remaining_items > 0
ABresult
1X1
011
000
143 0 ▶ my $max_results =
Branch: 1 branch
BranchTrueFalseTotal
defined $remaining_items && $remaining_items < $max_items_per_page ? : 0 0 0
Condition0%0/3
defined $remaining_items and $remaining_items < $max_items_per_page
!ll&&!rl&&r
000
MC/DC0%0/2
defined $remaining_items and $remaining_items < $max_items_per_page
defined $remaining_items $remaining_items < $max_items_per_page
Truth table0%0/3
A and B
A= defined $remaining_itemsB= $remaining_items < $max_items_per_page
ABresult
0X0
100
111
144 defined($remaining_items) &&
145 $remaining_items < $max_items_per_page ?
146 $remaining_items : $max_items_per_page;
147  
148 0 my $page = $self->_retrieve($req, $start_index, $max_results);
149 0 ▶ return $page if !$page->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $page->is_success 0 0 0
150  
151 0 ▶ if (!defined($res)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $res) { } 0 0 0
152 0 $res = $page;
153 }
154 else {
155 0 push(@{ $res->rows }, @{ $page->rows });
156 }
157  
158 0 my $num_items = @{ $page->rows };
159 0 ▶ last if $num_items < $max_results;
Branch: 1 branch
BranchTrueFalseTotal
if $num_items < $max_results 0 0 0
160  
161 0 ▶ $remaining_items -= $num_items if defined($remaining_items);
Branch: 1 branch
BranchTrueFalseTotal
if defined $remaining_items 0 0 0
162 0 $start_index += $num_items;
163 }
164  
165 0 my $total_items = @{ $res->rows };
166 0 $res->items_per_page($total_items);
167 # The total result count of the first page isn't always accurate
168 0 $res->total_results($total_items);
169  
170 0 return $res;
171 }
172  
173 2 1;
174  
175 __END__
↑ Summary Analytics/OAuth2.pm »