File Coverage

blib/lib/WebService/Async/CustomerIO/Customer.pm
Criterion Covered Total %
statement 49 49 100.0
branch 6 6 100.0
condition 5 8 62.5
subroutine 19 19 100.0
pod 15 15 100.0
total 94 97 96.9


line stmt bran cond sub pod time code
1             package WebService::Async::CustomerIO::Customer;
2              
3 3     3   21 use strict;
  3         7  
  3         97  
4 3     3   16 use warnings;
  3         7  
  3         84  
5              
6             =head1 NAME
7              
8             WebService::Async::CustomerIO::Customer - Class for working with customer.
9              
10             =head1 SYNOPSIS
11              
12             =head1 DESCRIPTION
13              
14             =cut
15              
16 3     3   20 use Carp qw();
  3         6  
  3         2987  
17              
18             our $VERSION = '0.001'; ## VERSION
19              
20             =head2 new
21              
22             Creates customer object. This method just creates an object, to sent this data to api, after creation should be called upsert method.
23              
24             Usage: C<< new(%params) -> obj >>
25              
26             parameters:
27              
28             =over 4
29              
30             =item * C - the unique identifier for the customer.
31              
32             =item * C - optional. the email address of the user.
33              
34             =item * C - optional. the unix timestamp from when the user was created in your system
35              
36             =item * C - hashref which contains custom attributes to define the customer.
37              
38             =back
39              
40             =cut
41              
42             sub new {
43 7     7 1 5617 my ($cls, %param) = @_;
44              
45 7   66     281 $param{$_} or Carp::croak "Missing required argument: $_" for (qw(id api_client));
46              
47 5         76 return bless \%param, $cls;
48             }
49              
50             =head2 api
51              
52             =cut
53              
54 11     11 1 45 sub api { return shift->{api_client} }
55              
56             =head2 id
57              
58             =cut
59              
60 13     13 1 1374 sub id { return shift->{id} }
61              
62             =head2 email
63              
64             =cut
65              
66 1     1 1 6 sub email { return shift->{email} }
67              
68             =head2 created_at
69              
70             =cut
71              
72 1     1 1 5 sub created_at { return shift->{created_at} }
73              
74             =head2 attributes
75              
76             =cut
77              
78 2     2 1 10 sub attributes { return shift->{attributes} }
79              
80             =head2 upsert
81              
82             Create or update a customer
83              
84             Usage: C<< upsert() -> Future() >>
85              
86             =cut
87              
88             sub upsert {
89 1     1 1 877 my ($self) = @_;
90 1         5 my $user_id = $self->id;
91              
92 1   50     4 my $attr = $self->attributes // {};
93             my %user_params =
94 1         5 map { $_ => $attr->{$_} }
95 1         4 grep { defined $attr->{$_} }
  1         4  
96             keys %$attr;
97              
98 1         16 @user_params{qw(email created_at)} = @{$self}{qw(email created_at)};
  1         6  
99              
100 1         8 return $self->api->tracking_request(
101             PUT => $self->_get_uri,
102             \%user_params
103             );
104             }
105              
106             =head2 set_attribute
107              
108             Set a customer attribute
109              
110             Usage: C<< set_attribute($name, $value) -> Future() >>
111              
112             =cut
113              
114             sub set_attribute {
115 2     2 1 4121 my ($self, $name, $val) = @_;
116              
117 2         7 return $self->api->tracking_request(
118             PUT => $self->_get_uri,
119             {$name => $val});
120             }
121              
122             =head2 remove_attribute
123              
124             Remove customer attribute
125              
126             Usage: C<< remove_attribute($name, $value) -> Future() >>
127              
128             =cut
129              
130             sub remove_attribute {
131 1     1 1 3920 my ($self, $name) = @_;
132              
133 1         3 return $self->set_attribute($name, '');
134             }
135              
136             =head2 suppress
137              
138             Suppress the customer. All events related to this customer wil be ignored by API.
139              
140             Usage: C<< suppress() -> Future() >>
141              
142             =cut
143              
144             sub suppress {
145 1     1 1 3928 my ($self) = @_;
146              
147 1         5 return $self->api->tracking_request(POST => $self->_get_uri('suppress'));
148             }
149              
150             =head2 unsuppress
151              
152             Unsuppress the customer.
153              
154             Usage: C<< unsuppress() -> Future() >>
155              
156             =cut
157              
158             sub unsuppress {
159 1     1 1 4144 my ($self) = @_;
160              
161 1         4 return $self->api->tracking_request(POST => $self->_get_uri('unsuppress'));
162             }
163              
164             =head2 upsert_device
165              
166             Create or update a customer device
167              
168             Usage: C<< upsert_device(%params) -> Future() >>
169              
170             Parameters:
171              
172             =over 4
173              
174             =item * C - The unique token for the user device.
175              
176             =item * C - The platform for the user device. Allowed values are 'ios' and 'android'.
177              
178             =item * C - Optional. UNIX timestamp representing the last used time for the device. If this is not included we default to the time of the device identify.
179              
180             =back
181              
182             =cut
183              
184             sub upsert_device {
185 5     5 1 9677 my ($self, %param) = @_;
186              
187 5   66     180 $param{$_} or Carp::croak "Missing required argument: $_" for (qw(device_id platform));
188              
189             Carp::croak 'Invalid value for platform: ' . $param{platform}
190 3 100       98 if $param{platform} !~ /^(?:ios|android)$/;
191              
192             my $device = {
193             id => $param{device_id},
194             platform => $param{platform},
195             last_used => $param{last_used},
196 2         10 };
197              
198 2         7 return $self->api->tracking_request(
199             PUT => $self->_get_uri('devices'),
200             {device => $device});
201             }
202              
203             =head2 delete_device
204              
205             Delete a customer device
206              
207             Usage: C<< delete_device($id) -> Future() >>
208              
209             =cut
210              
211             sub delete_device {
212 2     2 1 4562 my ($self, $device_id) = @_;
213              
214 2 100       94 $device_id or Carp::croak "Missing required argument: device_id";
215              
216 1         4 return $self->api->tracking_request(DELETE => $self->_get_uri('devices', $device_id));
217             }
218              
219             =head2 emit_event
220              
221             Track a customer event
222              
223             Usage: C<< emit_event(%params) -> Future() >>
224              
225             Parameters:
226              
227             - name: The name of the event to track
228             - type: Optional. Used to change event type. For Page View events set to "page".
229             - data: Optional. Custom data to include with the event.
230              
231             =cut
232              
233             sub emit_event {
234 2     2 1 5826 my ($self, %param) = @_;
235              
236 2 100       87 Carp::croak 'Missing required argument: name' unless $param{name};
237              
238 1         4 return $self->api->tracking_request(
239             POST => $self->_get_uri('events'),
240             \%param
241             );
242             }
243              
244             =head2 delete_customer
245              
246             Delete a customer
247              
248             Usage: C<< delete_customer() -> Future() >>
249              
250             =cut
251              
252             sub delete_customer {
253 1     1 1 2694 my $self = shift;
254              
255 1         4 return $self->api->tracking_request(DELETE => $self->_get_uri());
256             }
257              
258             sub _get_uri {
259 10     10   25 my ($self, @path) = @_;
260              
261 10         27 return join q{/} => ('customers', $self->id, @path);
262             }
263              
264             1;