CustomerIO.pm

filter: Statementstmt 91.0% 112 / 123 Branchbran 73.8% 31 / 42 Conditioncond 43.4% 10 / 23 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 92.1% 35 / 38 Podpod 87.5% 14 / 16 totaltotal 79.9% 203 / 254 CC 35 SCAR 12.2
CC
35
Cov
81%
send_transactional
8.1
_request
6.2
_ua
6.0
CRAP 3.4
« CustomerIO/Trigger.pm ↑ Summary CustomerIO/RateLimiter.pm »
1 package WebService::Async::CustomerIO;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.002';
7  
8 =head1 NAME
9  
10 WebService::Async::CustomerIO - unofficial support for the Customer.io service
11  
12 =head1 SYNOPSIS
13  
14 =head1 DESCRIPTION
15  
16 =cut
17  
18 4 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use JSON::MaybeUTF8 qw(:v1);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 4 ▶use WebService::Async::CustomerIO::Customer;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use WebService::Async::CustomerIO::RateLimiter;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use WebService::Async::CustomerIO::Trigger;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 4 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 TRACKING_END_POINT => 'https://track.customer.io/api/v1',
34 API_END_POINT => 'https://api.customer.io/v1',
35 RATE_LIMITS => {
36 track => {
37 limit => 30,
38 interval => 1
39 },
40 api => {
41 limit => 10,
42 interval => 1
43 },
44 transactional => {
45 limit => 100,
46 interval => 1
47 },
48 trigger => {
49 limit => 1,
50 interval => 10
51 }, # https://www.customer.io/docs/api/#operation/triggerBroadcast
52 4 }};
53  
54 =head2 new
55  
56 Creates a new API client object
57  
58 Usage: C<< new(%params) -> obj >>
59  
60 Parameters:
61  
62 =over 4
63  
64 =item * C<site_id>
65  
66 =item * C<api_key>
67  
68 =item * C<api_token>
69  
70 =back
71  
72 =cut
73  
74 27 ▶sub _init {
Subroutine _init: called CC=4 SCAR=13.9
75 27 my ($self, $args) = @_;
76  
77 27 for my $k (qw(site_id api_key api_token)) {
78 75 ▶ Carp::croak "Missing required argument: $k" unless exists $args->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$k} 5 70 75
79 70 ▶ $self->{$k} = delete $args->{$k} if exists $args->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$k} 70 0 70
80 }
81  
82 22 return $self->next::method($args);
83 }
84  
85 22 ▶sub configure {
Subroutine configure: called CC=3 SCAR=11.1
Pod documented
86 22 my ($self, %args) = @_;
87  
88 22 for my $k (qw(site_id api_key api_token api_uri track_uri)) {
89 110 ▶ $self->{$k} = delete $args{$k} if exists $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$k} 0 110 110
90 }
91  
92 22 return $self->next::method(%args);
93 }
94  
95 =head2 site_id
96  
97 =cut
98  
99 12 ▶sub site_id { return shift->{site_id} }
Subroutine site_id: called CC=1 SCAR=0.0
Pod documented
100  
101 =head2 api_key
102  
103 =cut
104  
105 12 ▶sub api_key { return shift->{api_key} }
Subroutine api_key: called CC=1 SCAR=0.0
Pod documented
106  
107 =head2 api_token
108  
109 =cut
110  
111 3 ▶sub api_token { return shift->{api_token} }
Subroutine api_token: called CC=1 SCAR=0.0
Pod documented
112  
113 =head2 api_uri
114  
115 =cut
116  
117 1 ▶sub api_uri { return shift->{api_uri} }
Subroutine api_uri: called CC=1 SCAR=0.0
Pod documented
118  
119 =head2 track_uri
120  
121 =cut
122  
123 1 ▶sub track_uri { return shift->{track_uri} }
Subroutine track_uri: called CC=1 SCAR=0.0
Pod documented
124  
125 =head2 API endpoints:
126  
127 There is 2 stable API for Customer.io, if you need to add a new method check
128 the L<documentation for API|https://customer.io/docs/api/> which endpoint
129 you need to use:
130  
131 =over 4
132  
133 =item * C<Tracking API> - Behavioral Tracking API is used to identify and track
134 customer data with Customer.io.
135  
136 =item * C<Regular API> - Currently, this endpoint is used to fetch list of customers
137 given an email and for sending
138 L<API triggered broadcasts|https://customer.io/docs/api-triggered-broadcast-setup>.
139  
140 =back
141  
142 =head2 tracking_request
143  
144 Sending request to Tracking API end point.
145  
146 Usage: C<< tracking_request($method, $uri, $data) -> future($data) >>
147  
148 =cut
149  
150 1 ▶sub tracking_request {
Subroutine tracking_request: called CC=1 SCAR=0.1
Pod documented
151 1 my ($self, $method, $uri, $data) = @_;
152 return $self->ratelimiter('track')->acquire->then(
153 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
154 1 ▶ $self->_request($method, join(q{/} => (($self->track_uri // TRACKING_END_POINT), $uri)), $data);
Condition50%1/2
$self->track_uri // "https://track.customer.io/api/v1"
l!l
01
MC/DC0%0/1
$self->track_uri // "https://track.customer.io/api/v1"
$self->track_uri
Truth table50%1/2
A
A= $self->track_uri
Aresult
11
00
155 1 });
156 }
157  
158 =head2 api_request
159  
160 Sending request to Regular API end point with optional limit type.
161  
162 Usage: C<< api_request($method, $uri, $data, $limit_type) -> future($data) >>
163  
164 =cut
165  
166 1 ▶sub api_request {
Subroutine api_request: called CC=3 SCAR=11.8
Pod documented
167 1 my ($self, $method, $uri, $data, $limit_type) = @_;
168  
169 1 ▶ Carp::croak('API token is missed') unless $self->api_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->api_token 0 1 1
170  
171 return $self->ratelimiter($limit_type // 'api')->acquire->then(
172 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
173 1 ▶ $self->_request($method, join(q{/} => (($self->api_uri // API_END_POINT), $uri)), $data, {authorization => 'Bearer ' . $self->api_token},
Condition50%1/2
$self->api_uri // "https://api.customer.io/v1"
l!l
01
MC/DC0%0/1
$self->api_uri // "https://api.customer.io/v1"
$self->api_uri
Truth table50%1/2
A
A= $self->api_uri
Aresult
11
00
174 );
175 1 ▶ });
Condition50%1/2
$limit_type // "api"
l!l
01
MC/DC0%0/1
$limit_type // "api"
$limit_type
Truth table50%1/2
A
A= $limit_type
Aresult
11
00
176 }
177  
178 8 ▶sub ratelimiter {
Subroutine ratelimiter: called CC=3 SCAR=11.0
Pod undocumented
179 8 my ($self, $type) = @_;
180  
181 8 ▶ return $self->{ratelimiters}{$type} if $self->{ratelimiters}{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ratelimiters"}{$type} 4 4 8
182  
183 4 ▶ Carp::croak "Can't use rate limiter without a loop" unless $self->loop;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->loop 0 4 4
184  
185 4 $self->{ratelimiters}{$type} = WebService::Async::CustomerIO::RateLimiter->new(RATE_LIMITS->{$type}->%*);
186  
187 4 $self->add_child($self->{ratelimiters}{$type});
188  
189 4 return $self->{ratelimiters}{$type};
190 }
191  
192 4 my %PATTERN_FOR_ERROR = (
193 RESOURCE_NOT_FOUND => qr/^404$/,
194 INVALID_REQUEST => qr/^400$/,
195 INVALID_API_KEY => qr/^401$/,
196 INTERNAL_SERVER_ERR => qr/^50[0234]$/,
197 );
198  
199 11 ▶sub _request {
Subroutine _request: called CC=6 SCAR=18.3
200 11 my ($self, $method, $uri, $data, $headers) = @_;
201  
202 11 ▶ my $body =
Branch: 2 branches
BranchTrueFalseTotal
$data ? : 1 10 11
$method eq 'POST' ? : 0 10 10
203 $data ? encode_json_utf8($data)
204 : $method eq 'POST' ? q{}
205 : undef;
206  
207 return $self->_ua->do_request(
208 method => $method,
209 uri => $uri,
210 $headers->{authorization} ? ()
211 : (
212 user => $self->site_id,
213 pass => $self->api_key,
214 ),
215 !defined $body ? ()
216 : (
217 content => $body,
218 content_type => 'application/json',
219 ),
220 headers => $headers // {},
221  
222 )->catch(
223 8 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.5
224 8 my ($code_msg, $err_type, $response) = @_;
225  
226 8 ▶ return Future->fail(@_) unless $err_type && $err_type eq 'http';
Branch: 1 branch
BranchTrueFalseTotal
unless $err_type and $err_type eq "http" 0 8 8
Condition33%1/3
$err_type and $err_type eq "http"
!ll&&!rl&&r
008
MC/DC0%0/2
$err_type and $err_type eq "http"
$err_type $err_type eq "http"
Truth table33%1/3
A and B
A= $err_typeB= $err_type eq "http"
ABresult
0X0
100
111
227  
228 8 my $code = $response->code;
229 8 my $request_data = {
230 method => $method,
231 uri => $uri,
232 data => $data
233 };
234  
235 8 for my $error_code (keys %PATTERN_FOR_ERROR) {
236 20 ▶ next unless $code =~ /$PATTERN_FOR_ERROR{$error_code}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /$PATTERN_FOR_ERROR{$error_code}/ 13 7 20
237 7 return Future->fail($error_code, 'customerio', $request_data);
238 }
239  
240 1 return Future->fail('UNEXPECTED_HTTP_CODE: ' . $code_msg, 'customerio', $response);
241 }
242 )->then(
243 3 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
244 3 my ($response) = @_;
245 try {
246 3 my $response_data = decode_json_utf8($response->content);
247 2 return Future->done($response_data);
248 3 } catch {
249 1 return Future->fail('UNEXPECTED_RESPONSE_FORMAT', 'customerio', $@, $response);
250 }
251 11 ▶ });
Branch: 2 branches
BranchTrueFalseTotal
$headers->{'authorization'} ? : 0 11 11
!defined($body) ? : 10 1 11
Condition50%1/2
$headers // {}
l!l
110
MC/DC0%0/1
$headers // {}
$headers
Truth table50%1/2
A
A= $headers
Aresult
11
00
252 }
253  
254 0 ▶sub _ua {
Subroutine _ua: not called CC=2 SCAR=17.9
255 0 my ($self) = @_;
256  
257 0 ▶ return $self->{ua} if $self->{ua};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ua"} 0 0 0
258  
259 0 $self->{ua} = Net::Async::HTTP->new(
260 fail_on_error => 1,
261 decode_content => 0,
262 pipeline => 0,
263 stall_timeout => 60,
264 max_connections_per_host => 4,
265 user_agent => 'Mozilla/4.0 (WebService::Async::CustomerIO; BINARY@cpan.org; https://metacpan.org/pod/WebService::Async::CustomerIO)',
266 );
267  
268 0 $self->add_child($self->{ua});
269  
270 0 return $self->{ua};
271 }
272  
273 =head2 new_customer
274  
275 Creating new customer object
276  
277 Usage: C<< new_customer(%params) -> obj >>
278  
279 =cut
280  
281 0 ▶sub new_customer {
Subroutine new_customer: not called CC=1 SCAR=6.9
Pod documented
282 0 my ($self, %param) = @_;
283  
284 0 return WebService::Async::CustomerIO::Customer->new(%param, api_client => $self);
285 }
286  
287 =head2 new_trigger
288  
289 Creating new trigger object
290  
291 Usage: C<< new_trigger(%params) -> obj >>
292  
293 =cut
294  
295 0 ▶sub new_trigger {
Subroutine new_trigger: not called CC=1 SCAR=6.9
Pod documented
296 0 my ($self, %param) = @_;
297  
298 0 return WebService::Async::CustomerIO::Trigger->new(%param, api_client => $self);
299 }
300  
301 =head2 new_customer
302  
303 Creating new customer object
304  
305 Usage: C<< new_customer(%params) -> obj >>
306  
307 =cut
308  
309 1 ▶sub emit_event {
Subroutine emit_event: called CC=1 SCAR=0.0
Pod undocumented
310 1 my ($self, %params) = @_;
311  
312 1 return $self->tracking_request(
313 POST => 'events',
314 \%params
315 );
316 }
317  
318 =head2 add_to_segment
319  
320 Add people to a manual segment.
321  
322 Usage: C<< add_to_segment($segment_id, @$customer_ids) -> Future() >>
323  
324 =cut
325  
326 3 ▶sub add_to_segment {
Subroutine add_to_segment: called CC=3 SCAR=11.0
Pod documented
327 3 my ($self, $segment_id, $customers_ids) = @_;
328  
329 3 ▶ Carp::croak 'Missing required attribute: segment_id' unless $segment_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $segment_id 1 2 3
330 2 ▶ Carp::croak 'Invalid value for customers_ids' unless ref $customers_ids eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $customers_ids eq "ARRAY" 1 1 2
331  
332 1 return $self->tracking_request(
333 POST => "segments/$segment_id/add_customers",
334 {ids => $customers_ids});
335 }
336  
337 =head2 remove_from_segment
338  
339 remove people from a manual segment.
340  
341 usage: c<< remove_from_segment($segment_id, @$customer_ids) -> future() >>
342  
343 =cut
344  
345 3 ▶sub remove_from_segment {
Subroutine remove_from_segment: called CC=3 SCAR=11.0
Pod documented
346 3 my ($self, $segment_id, $customers_ids) = @_;
347  
348 3 ▶ Carp::croak 'Missing required attribute: segment_id' unless $segment_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $segment_id 1 2 3
349 2 ▶ Carp::croak 'Invalid value for customers_ids' unless ref $customers_ids eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $customers_ids eq "ARRAY" 1 1 2
350  
351 1 return $self->tracking_request(
352 POST => "segments/$segment_id/remove_customers",
353 {ids => $customers_ids});
354 }
355  
356 =head2 get_customers_by_email
357  
358 Query Customer.io API for list of clients, who has requested email address.
359  
360 usage: c<< get_customers_by_email($email)->future([$customer_obj1, ...]) >>
361  
362 =cut
363  
364 1 ▶sub get_customers_by_email {
Subroutine get_customers_by_email: called CC=2 SCAR=7.6
Pod documented
365 1 my ($self, $email) = @_;
366  
367 1 ▶ Carp::croak 'Missing required argument: email' unless $email;
Branch: 1 branch
BranchTrueFalseTotal
unless $email 0 1 1
368  
369 return $self->api_request(GET => "customers?email=" . uri_escape_utf8($email))->then(
370 1 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
371 1 my ($resp) = @_;
372  
373 1 ▶ if (ref $resp ne 'HASH' || ref $resp->{results} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $resp ne "HASH" or ref $resp->{"results"} ne "ARRAY") 0 1 1
Condition33%1/3
ref $resp ne "HASH" or ref $resp->{"results"} ne "ARRAY"
l!l&&r!l&&!r
001
MC/DC0%0/2
ref $resp ne "HASH" or ref $resp->{"results"} ne "ARRAY"
ref $resp ne "HASH" ref $resp->{"results"} ne "ARRAY"
Truth table33%1/3
A or B
A= ref $resp ne "HASH"B= ref $resp->{"results"} ne "ARRAY"
ABresult
1X1
011
000
374 0 return Future->fail('UNEXPECTED_RESPONSE_FORMAT', 'customerio', 'Unexpected response format is recived', $resp);
375 }
376  
377 try {
378 1 my @customers = map { WebService::Async::CustomerIO::Customer->new($_->%*, api_client => $self) } $resp->{results}->@*;
379 1 return Future->done(\@customers);
380 } catch ($e) {
381 0 return Future->fail('UNEXPECTED_RESPONSE_FORMAT', 'customerio', $e, $resp);
382 }
383  
384 1 });
385 }
386  
387 =head2 send_transactional
388  
389 =cut
390  
391 5 ▶sub send_transactional {
Subroutine send_transactional: called CC=7 SCAR=20.9
Pod documented
392 5 my ($self, $data) = @_;
393 5 for my $attribute (qw/transactional_message_id to identifiers/) {
394 12 ▶ Carp::croak "Missing required attribute: $attribute" unless $data->{$attribute};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{$attribute} 3 9 12
395 }
396 Carp::croak 'Missing required attribute: identifiers value'
397 unless (
398 ref $data->{identifiers} eq 'HASH'
399 && ( $data->{identifiers}->{id}
400 || $data->{identifiers}->{email}
401 2 ▶ || $data->{identifiers}->{cio}));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"identifiers"} eq "HASH" and $data->{"identifiers"}{"id"} || $data->{"identifiers"}{"email"} || $data->{"identifiers"}{"cio"} 1 1 2
Condition44%4/9
ref $data->{"identifiers"} eq "HASH" and $data->{"identifiers"}{"id"} || $data->{"identifiers"}{"email"} || $data->{"identifiers"}{"cio"}
!ll&&!rl&&r
101
$data->{"identifiers"}{"id"} or $data->{"identifiers"}{"email"} or $data->{"identifiers"}{"cio"}
l!l&&r!l&&!r
100
$data->{"identifiers"}{"id"} or $data->{"identifiers"}{"email"}
l!l&&r!l&&!r
100
MC/DC25%1/4
ref $data->{"identifiers"} eq "HASH" and $data->{"identifiers"}{"id"} || $data->{"identifiers"}{"email"} || $data->{"identifiers"}{"cio"}
ref $data->{"identifiers"} eq "HASH" $data->{"identifiers"}{"id"} $data->{"identifiers"}{"email"} $data->{"identifiers"}{"cio"}
Truth table40%2/5
A and B or C or D
A= ref $data->{"identifiers"} eq "HASH"B= $data->{"identifiers"}{"id"}C= $data->{"identifiers"}{"email"}D= $data->{"identifiers"}{"cio"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
402  
403 1 return $self->api_request(
404 POST => "send/email",
405 $data, 'transactional'
406 );
407 }
408  
409 4 1;
« CustomerIO/Trigger.pm ↑ Summary CustomerIO/RateLimiter.pm »