blib/lib/WWW/Piwik/API.pm

filter: Statementstmt 91.6% 44 / 48 Branchbran 75.0% 3 / 4 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 90.9% 10 / 11 Podpod 100% 2 / 2 totaltotal 84.0% 63 / 75 CC 6 SCAR 11.0
CC
6
Cov
86%
track_uri
6.2
track
2.0
BEGIN
1.0
CRAP 3.0
↑ Summary
1 package WWW::Piwik::API;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Types::Standard qw/Str Object Int/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 WWW::Piwik::API - Tracking module for Piwik using the Tracking API
15  
16 =head1 VERSION
17  
18 Version 0.01
19  
20 =cut
21  
22 2 our $VERSION = '0.01';
23  
24  
25 =head1 SYNOPSIS
26  
27 my $tracker = WWW::Piwik::API->new(endpoint => $ENV{PIWIK_URL} || 'http://localhost/piwik.php',
28 idsite => $ENV{PIWIK_IDSITE} || 1,
29 token_auth => $ENV{PIWIK_TOKEN_AUTH},
30 );
31 # see https://developer.piwik.org/api-reference/tracking-api for params
32 $tracker->track(%data)
33  
34 =head1 ACCESSORS
35  
36 They are read-only and need to be set in the constructor.
37  
38 =head2 endpoint
39  
40 The endpoint of piwik. Required.
41  
42 =head2 idsite
43  
44 The ID of the site being tracked. You can look this up in the piwik console.
45  
46 =head2 token_auth
47  
48 The authentication token (you need an admin account for this, and the
49 token can be looked up under Personal settings/API.
50  
51 =head2 ua
52  
53 The user agent, which defaults to L<LWP::UserAgent> with a timeout of
54 5 seconds.
55  
56 =cut
57  
58 2 has endpoint => (is => 'ro', isa => Str, required => 1);
59  
60 2 has idsite => (is => 'ro', isa => Int, required => 1);
61  
62 2 has token_auth => (is => 'ro', isa => Str);
63  
64 has ua => (is => 'ro',
65 isa => Object,
66 default => sub {
67 1 ▶ my $ua = LWP::UserAgent->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
68 1 $ua->timeout(5);
69 1 return $ua;
70 2 });
71  
72 sub track {
73 0 ▶ my ($self, %data) = @_;
Subroutine track: not called CC=1 SCAR=6.9
Pod documented
74 0 my $uri = $self->track_uri(%data);
75 0 my $res = $self->ua->get($uri->as_string);
76 0 return $res;
77 }
78  
79 sub track_uri {
80 2 ▶ my ($self, %data) = @_;
Subroutine track_uri: called CC=6 SCAR=18.3
Pod documented
81 2 my %params = (bots => 1,
82 rec => 1,
83 idsite => $self->idsite,
84 token_auth => $self->token_auth,
85 );
86 2 my $json = JSON::MaybeXS->new(ascii => 1);
87 2 foreach my $k (keys %data) {
88 14 my $v = $data{$k};
89 14 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 14 0 14
90 14 ▶ if (ref($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 2 12 14
91 2 ▶ $params{$k} ||= $json->encode($v);
Condition33%1/3
$params{$k} ||= $json->encode($v)
l!l&&r!l&&!r
020
MC/DC0%0/2
$params{$k} ||= $json->encode($v)
$params{$k} $json->encode($v)
Truth table33%1/3
A ||= B
A= $params{$k}B= $json->encode($v)
ABresult
1X1
011
000
92 }
93 else {
94 12 ▶ $params{$k} ||= $v;
Condition66%2/3
$params{$k} ||= $v
l!l&&r!l&&!r
0102
MC/DC50%1/2
$params{$k} ||= $v
$params{$k} $v
Truth table66%2/3
A ||= B
A= $params{$k}B= $v
ABresult
1X1
011
000
95 }
96 }
97 }
98 2 my $uri = URI->new($self->endpoint);
99 2 $uri->query_form(%params);
100 2 return $uri;
101 }
102  
103  
104 =head1 METHODS
105  
106 =head2 track(%data)
107  
108 Build an URI and do a call against it, serializing the parameters and
109 adding the default one set in the constructor.
110  
111 =head2 track_uri(%data)
112  
113 The URI against which track will be called.
114  
115 =head1 AUTHOR
116  
117 Stefan Hornburg, C<< <racke at linuxia.de> >>
118  
119 =head1 BUGS
120  
121 Please report any bugs or feature requests at L<https://github.com/interchange/WWW-Piwik-API/issues>.
122  
123 =head1 SEE ALSO
124  
125 L<https://developer.piwik.org/api-reference/tracking-api>
126  
127 =head1 LICENSE AND COPYRIGHT
128  
129 Copyright 2016 Stefan Hornburg.
130  
131 This program is free software; you can redistribute it and/or modify it
132 under the terms of the the Artistic License (2.0). You may obtain a
133 copy of the full license at:
134  
135 L<http://www.perlfoundation.org/artistic_license_2_0>
136  
137 Any use, modification, and distribution of the Standard or Modified
138 Versions is governed by this Artistic License. By using, modifying or
139 distributing the Package, you accept this license. Do not use, modify,
140 or distribute the Package, if you do not accept this license.
141  
142 If your Modified Version has been derived from a Modified Version made
143 by someone other than you, you are nevertheless required to ensure that
144 your Modified Version complies with the requirements of this license.
145  
146 This license does not grant you the right to use any trademark, service
147 mark, tradename, or logo of the Copyright Holder.
148  
149 This license includes the non-exclusive, worldwide, free-of-charge
150 patent license to make, have made, use, offer to sell, sell, import and
151 otherwise transfer the Package with respect to any patent claims
152 licensable by the Copyright Holder that are necessarily infringed by the
153 Package. If you institute patent litigation (including a cross-claim or
154 counterclaim) against any party alleging that the Package constitutes
155 direct or contributory patent infringement, then this Artistic License
156 to you shall terminate on the date that such litigation is filed.
157  
158 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
159 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
160 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
161 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
162 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
163 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
164 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
165 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
166  
167  
168 =cut
169  
170 2 1; # End of WWW::Piwik::API
↑ Summary