CustomerIO.pm

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