line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Sixpack; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
67069
|
use 5.006; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
116
|
|
4
|
2
|
|
|
2
|
|
17
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
81
|
|
5
|
2
|
|
|
2
|
|
11
|
use warnings FATAL => 'all'; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
114
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
11
|
use Carp qw( croak ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
161
|
|
8
|
2
|
|
|
2
|
|
3710
|
use Data::UUID; |
|
2
|
|
|
|
|
7414
|
|
|
2
|
|
|
|
|
178
|
|
9
|
2
|
|
|
2
|
|
2566
|
use JSON::Any; |
|
2
|
|
|
|
|
81598
|
|
|
2
|
|
|
|
|
14
|
|
10
|
2
|
|
|
2
|
|
45055
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
246204
|
|
|
2
|
|
|
|
|
71
|
|
11
|
2
|
|
|
2
|
|
23
|
use URI; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
13635
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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 |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
The sixpack server (defaults to 'http://localhost:5000'). |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=item C |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
The client id if the "user" is known already. By default we generate a new UUID. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item C |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
The useragent to use (defaults to L). |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=back |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub new { |
72
|
1
|
|
|
1
|
1
|
13
|
my ($class, %args) = @_; |
73
|
1
|
|
|
|
|
14
|
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
|
|
|
|
|
5926
|
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 |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
The name of the experiment. This will generate a new experiment when the name is unknown. |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=item C |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
At least two alternatives. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=item C |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
An optional hashref with the following options: |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=over 4 |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=item C |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Force a specific alternative to be returned |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item C |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
User agent of the user making a request. Used for bot detection. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item C |
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
|
|
|
3
|
1
|
2945
|
my ($self, $experiment, $alternatives, $options) = @_; |
125
|
|
|
|
|
|
|
|
126
|
3
|
100
|
|
|
|
231
|
croak('Bad experiment name') |
127
|
|
|
|
|
|
|
if( $experiment !~ m/$VALID_NAME_RE/ ); |
128
|
2
|
100
|
33
|
|
|
54
|
croak('Must specify at least 2 alternatives') |
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
129
|
|
|
|
|
|
|
if( !$alternatives || !ref $alternatives || |
130
|
|
|
|
|
|
|
ref $alternatives ne 'ARRAY' || @$alternatives < 2 ); |
131
|
|
|
|
|
|
|
|
132
|
1
|
|
|
|
|
2
|
for my $alt (@{$alternatives}) { |
|
1
|
|
|
|
|
3
|
|
133
|
2
|
100
|
|
|
|
23
|
croak('Bad alternative name: '.$alt) if( $alt !~ m/$VALID_NAME_RE/ ); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
0
|
|
|
|
$options ||= { }; |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
$res->{alternative}{name} = $alternatives->[0] |
147
|
|
|
|
|
|
|
if( $res->{status} eq 'failed' ); |
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 |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
The name of the experiment. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item C |
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
|
|
|
0
|
1
|
|
my ($self, $experiment, $kpi) = @_; |
174
|
|
|
|
|
|
|
|
175
|
0
|
0
|
|
|
|
|
croak('Bad experiment name') |
176
|
|
|
|
|
|
|
if( $experiment !~ m/$VALID_NAME_RE/ ); |
177
|
|
|
|
|
|
|
|
178
|
0
|
|
|
|
|
|
my %params = ( |
179
|
|
|
|
|
|
|
client_id => $self->{client_id}, |
180
|
|
|
|
|
|
|
experiment => $experiment, |
181
|
|
|
|
|
|
|
); |
182
|
|
|
|
|
|
|
|
183
|
0
|
0
|
|
|
|
|
if( $kpi ) { |
184
|
0
|
0
|
|
|
|
|
croak('Bad KPI name') |
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
|
|
|
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
|
0
|
|
|
|
|
my $json = ( $resp->is_success ) |
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<< >> |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head1 BUGS |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
221
|
|
|
|
|
|
|
the web interface at L. 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 |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
L |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=item * CPAN Ratings |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
L |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=item * Search CPAN |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
L |
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 for more information. |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=cut |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
1; # End of WWW::Sixpack |