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 CC 14 SCAR 25.3
CC
14
Cov
55%
participate
21.3
convert
20.0
_get_response
6.0
CRAP 12.6
↑ 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 =cut
20  
21 3 our $VERSION = '0.04';
22  
23 =head1 SYNOPSIS
24  
25 use WWW::Sixpack;
26  
27 my $sixpack = WWW::Sixpack->new();
28  
29 # Participate in a test (creates the test if necessary)
30 my $alternative = $sixpack->participate('new-test', [ 'alt-1', 'alt-2' ],
31 { ip_address => $client_ip, user_agent => $client_ua,
32 force => 'alt-2', traffic_fraction => 0.10 });
33  
34 if( $alternative->{alternative}{name} eq 'alt-1' ) {
35 # show data for variant alt-1
36 } else {
37 # show data for variant alt-2
38 }
39  
40 # Convert
41 $sixpack->convert('new-test')
42  
43 =head1 SUBROUTINES/METHODS
44  
45 =head2 new
46  
47 Constructs the WWW::Sixpack object. Options that can be passed in are:
48  
49 =over 4
50  
51 =item C<host>
52  
53 The sixpack server (defaults to 'http://localhost:5000').
54  
55 =item C<client_id>
56  
57 The client id if the "user" is known already. By default we generate a new UUID.
58  
59 =item C<ua>
60  
61 The useragent to use (defaults to L<LWP::UserAgent>).
62  
63 =back
64  
65 =cut
66  
67 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
68 1 my ($class, %args) = @_;
69 1 my $self = {
70 host => 'http://localhost:5000',
71 ua => LWP::UserAgent->new,
72 json => JSON::Any->new,
73 client_id => Data::UUID->new->create_str,
74 %args,
75 };
76 1 bless $self, $class;
77 }
78  
79 =head2 participate
80  
81 This function takes the following arguments:
82  
83 Arguments:
84  
85 =over 4
86  
87 =item C<experiment>
88  
89 The name of the experiment. This will generate a new experiment when the name is unknown.
90  
91 =item C<alternatives>
92  
93 At least two alternatives.
94  
95 =item C<options>
96  
97 An optional hashref with the following options:
98  
99 =over 4
100  
101 =item C<user_agent>
102  
103 User agent of the user making a request. Used for bot detection.
104  
105 =item C<ip_address>
106  
107 IP address of user making a request. Used for bot detection.
108  
109 =item C<force>
110  
111 (optional) Force a specific alternative to be returned
112  
113 =item C<traffic_fraction>
114  
115 (optional) Sixpack allows for limiting experiments to a subset of traffic. You can pass the percentage of traffic you'd like to expose the test to as a decimal number here. (0.10 for 10%)
116  
117 =back
118  
119 =back
120  
121 =cut
122  
123 3 ▶sub participate {
Subroutine participate: called CC=10 SCAR=30.6
Pod documented
124 3 my ($self, $experiment, $alternatives, $options) = @_;
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 0 ▶sub convert {
Subroutine convert: not called CC=4 SCAR=30.0
Pod documented
173 0 my ($self, $experiment, $kpi) = @_;
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 0 ▶sub _get_response {
Subroutine _get_response: not called CC=2 SCAR=17.9
200 0 my ($self, $endpoint, $params) = @_;
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
254  
255 This library is free software; you can redistribute it and/or modify
256 it under the same terms as Perl itself.
257  
258 =cut
259  
260 3 1;
↑ Summary