blib/lib/WWW/Sixpack.pm

filter: Statementstmt 63.6% 35 / 55 Branchbran 37.5% 6 / 16 Conditioncond 36.3% 4 / 11 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 83.3% 10 / 12 Podpod 100% 3 / 3 totaltotal 57.8% 59 / 102 SCAR 34.7
CC
14
Cov
55%
participate
21.3
convert
20.0
_get_response
6.0
CRAP 32.0
↑ Summary
1 package WWW::Sixpack;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $VALID_NAME_RE = qr/^[a-z0-9][a-z0-9\-_ ]*$/;
14  
15 =head1 NAME
16  
17 WWW::Sixpack - Perl client library for SeatGeek's Sixpack A/B testing framework http://sixpack.seatgeek.com/
18  
19 =head1 VERSION
20  
21 Version 0.03
22  
23 =cut
24  
25 3 our $VERSION = '0.03';
26  
27 =head1 SYNOPSIS
28  
29 use WWW::Sixpack;
30  
31 my $sixpack = WWW::Sixpack->new();
32  
33 # Participate in a test (creates the test if necessary)
34 my $alternative = $sixpack->participate('new-test', [ 'alt-1', 'alt-2' ],
35 { ip_address => $client_ip, user_agent => $client_ua });
36  
37 if( $alternative->{alternative}{name} eq 'alt-1' ) {
38 # show data for variant alt-1
39 } else {
40 # show data for variant alt-2
41  
42 }
43  
44 # Convert
45 $sixpack->convert('new-test')
46  
47 =head1 SUBROUTINES/METHODS
48  
49 =head2 new
50  
51 Constructs the WWW::Sixpack object. Options that can be passed in are:
52  
53 =over 4
54  
55 =item C<host>
56  
57 The sixpack server (defaults to 'http://localhost:5000').
58  
59 =item C<client_id>
60  
61 The client id if the "user" is known already. By default we generate a new UUID.
62  
63 =item C<ua>
64  
65 The useragent to use (defaults to L<LWP::UserAgent>).
66  
67 =back
68  
69 =cut
70  
71 sub new {
72 1 ▶ my ($class, %args) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
73 1 my $self = {
74 host => 'http://localhost:5000',
75 ua => LWP::UserAgent->new,
76 json => JSON::Any->new,
77 client_id => Data::UUID->new->create_str,
78 %args,
79 };
80 1 bless $self, $class;
81 }
82  
83 =head2 participate
84  
85 This function takes the following arguments:
86  
87 Arguments:
88  
89 =over 4
90  
91 =item C<experiment>
92  
93 The name of the experiment. This will generate a new experiment when the name is unknown.
94  
95 =item C<alternatives>
96  
97 At least two alternatives.
98  
99 =item C<options>
100  
101 An optional hashref with the following options:
102  
103 =over 4
104  
105 =item C<force>
106  
107 Force a specific alternative to be returned
108  
109 =item C<user_agent>
110  
111 User agent of the user making a request. Used for bot detection.
112  
113 =item C<ip_address>
114  
115 IP address of user making a request. Used for bot detection.
116  
117 =back
118  
119 =back
120  
121 =cut
122  
123 sub participate {
124 3 ▶ my ($self, $experiment, $alternatives, $options) = @_;
Subroutine participate: called CC=10 SCAR=30.6
Pod documented
125  
126 3 ▶ croak('Bad experiment name')
Branch: 1 branch
BranchTrueFalseTotal
unless $experiment =~ /$WWW::Sixpack::VALID_NAME_RE/ 1 2 3
127 if( $experiment !~ m/$VALID_NAME_RE/ );
128 2 ▶ croak('Must specify at least 2 alternatives')
Branch: 1 branch
BranchTrueFalseTotal
if not $alternatives && ref $alternatives or ref $alternatives ne "ARRAY" or @$alternatives < 2 1 1 2
Condition44%4/9
not $alternatives && ref $alternatives or ref $alternatives ne "ARRAY" or @$alternatives < 2
l!l&&r!l&&!r
011
not $alternatives && ref $alternatives or ref $alternatives ne "ARRAY"
l!l&&r!l&&!r
002
$alternatives && ref $alternatives
!ll&&!rl&&r
002
MC/DC25%1/4
not $alternatives && ref $alternatives or ref $alternatives ne "ARRAY" or @$alternatives < 2
$alternatives ref $alternatives ref $alternatives ne "ARRAY" @$alternatives < 2
Truth table40%2/5
not(A && B) or C or D
A= $alternativesB= ref $alternativesC= ref $alternatives ne "ARRAY"D= @$alternatives < 2
ABCDresult
0XXX1
10XX1
111X1
11011
11000
129 if( !$alternatives || !ref $alternatives ||
130 ref $alternatives ne 'ARRAY' || @$alternatives < 2 );
131  
132 1 for my $alt (@{$alternatives}) {
133 2 ▶ croak('Bad alternative name: '.$alt) if( $alt !~ m/$VALID_NAME_RE/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $alt =~ /$WWW::Sixpack::VALID_NAME_RE/ 1 1 2
134 }
135  
136 0 ▶ $options ||= { };
Condition0%0/2
$options ||= {}
l!l
00
MC/DC0%0/1
$options ||= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
137  
138 my %params = (
139 client_id => $self->{client_id},
140 experiment => $experiment,
141 alternatives => $alternatives,
142 0 %{$options}
143 );
144  
145 0 my $res = $self->_get_response('/participate', \%params);
146 $res->{alternative}{name} = $alternatives->[0]
147 0 ▶ if( $res->{status} eq 'failed' );
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} eq "failed" 0 0 0
148  
149 0 return $res;
150 }
151  
152 =head2 convert
153  
154 This function takes the following arguments:
155  
156 Arguments:
157  
158 =over 4
159  
160 =item C<experiment>
161  
162 The name of the experiment.
163  
164 =item C<kpi>
165  
166 A KPI you wish to track. When the KPI is unknown, it will be created.
167  
168 =back
169  
170 =cut
171  
172 sub convert {
173 0 ▶ my ($self, $experiment, $kpi) = @_;
Subroutine convert: not called CC=4 SCAR=30.0
Pod documented
174  
175 0 ▶ croak('Bad experiment name')
Branch: 1 branch
BranchTrueFalseTotal
unless $experiment =~ /$WWW::Sixpack::VALID_NAME_RE/ 0 0 0
176 if( $experiment !~ m/$VALID_NAME_RE/ );
177  
178 my %params = (
179 client_id => $self->{client_id},
180 0 experiment => $experiment,
181 );
182  
183 0 ▶ if( $kpi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kpi) 0 0 0
184 0 ▶ croak('Bad KPI name')
Branch: 1 branch
BranchTrueFalseTotal
unless $kpi =~ /$WWW::Sixpack::VALID_NAME_RE/ 0 0 0
185 if( $kpi !~ m/$VALID_NAME_RE/ );
186 0 $params{kpi} = $kpi;
187 }
188  
189 0 return $self->_get_response('/convert', \%params);
190  
191 }
192  
193 =head2 _get_response
194  
195 Internal method to fire the actual request and parse the result
196  
197 =cut
198  
199 sub _get_response {
200 0 ▶ my ($self, $endpoint, $params) = @_;
Subroutine _get_response: not called CC=2 SCAR=17.9
201  
202 0 my $uri = URI->new($self->{host});
203 0 $uri->path($endpoint);
204 0 $uri->query_form( $params );
205  
206 0 my $resp = $self->{ua}->get( $uri );
207 0 ▶ my $json = ( $resp->is_success )
Branch: 1 branch
BranchTrueFalseTotal
$resp->is_success ? : 0 0 0
208 ? $resp->content
209 : '{"status": "failed", "response": "http error: sixpack is unreachable"}';
210  
211 0 return $self->{json}->jsonToObj( $json );
212 }
213  
214 =head1 AUTHOR
215  
216 Menno Blom, C<< <blom at cpan.org> >>
217  
218 =head1 BUGS
219  
220 Please report any bugs or feature requests to C<bug-www-sixpack at rt.cpan.org>, or through
221 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Sixpack>. I will be notified, and then you'll
222 automatically be notified of progress on your bug as I make changes.
223  
224 =head1 SUPPORT
225  
226 You can find documentation for this module with the perldoc command.
227  
228 perldoc WWW::Sixpack
229  
230  
231 You can also look for information at:
232  
233 =over 4
234  
235 =item * RT: CPAN's request tracker (report bugs here)
236  
237 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Sixpack>
238  
239 =item * AnnoCPAN: Annotated CPAN documentation
240  
241 L<http://annocpan.org/dist/WWW-Sixpack>
242  
243 =item * CPAN Ratings
244  
245 L<http://cpanratings.perl.org/d/WWW-Sixpack>
246  
247 =item * Search CPAN
248  
249 L<http://search.cpan.org/dist/WWW-Sixpack/>
250  
251 =back
252  
253 =head1 LICENSE AND COPYRIGHT
254  
255 Copyright 2013 Menno Blom.
256  
257 This program is free software; you can redistribute it and/or modify it
258 under the terms of either: the GNU General Public License as published
259 by the Free Software Foundation; or the Artistic License.
260  
261 See L<http://dev.perl.org/licenses/> for more information.
262  
263  
264 =cut
265  
266 3 1; # End of WWW::Sixpack
↑ Summary