File Coverage

blib/lib/WebService/Async/Segment/Customer.pm
Criterion Covered Total %
statement 51 51 100.0
branch 26 28 92.8
condition 5 10 50.0
subroutine 12 12 100.0
pod 7 7 100.0
total 101 108 93.5


line stmt bran cond sub pod time code
1             package WebService::Async::Segment::Customer;
2              
3 2     2   14 use strict;
  2         4  
  2         59  
4 2     2   11 use warnings;
  2         5  
  2         76  
5              
6 2     2   11 use constant COMMON_FIELDS => qw(context integrations timestamp);
  2         12  
  2         1666  
7              
8             our $VERSION = '0.001'; # VERSION
9              
10             =head1 NAME
11              
12             WebService::Async::Segment::Customer - represents a customer object with methods to make Segment API calls.
13              
14             =head1 DESCRIPTION
15              
16             You can create objects directly or (preferably) indirectly using C<< WebService::Async::Segment::new_customer >>.
17             Segment calls L and L can be triggered on objects of this class.
18              
19             =cut
20              
21             =head1 METHODS
22              
23             =head2 new
24              
25             Class constructor accepting a hash of named args containing customer info, along with a Segment API wrapper object (an object of class L).
26             There is no need to make this call if you create an object using C<< WebService::Async::Segment::new_customer >> (as recommended).
27              
28             The accepted params are:
29              
30             =over 4
31              
32             =item * C - Segment API wrapper object.
33              
34             =item * C or C - Unique identifier of a user.
35              
36             =item * C or C - A pseudo-unique substitute for a User ID, for cases when you don't have an absolutely unique identifier.
37              
38             =item * C - Free-form dictionary of traits of the user, like email or name.
39              
40             =back
41              
42             =cut
43              
44             sub new {
45 7     7 1 7514 my ($class, %args) = @_;
46              
47 7 100       45 die 'Missing required arg api_client' unless $args{api_client};
48 6         13 my $api_client = $args{api_client};
49 6 100       40 die 'Invalid api_client value' unless $api_client->isa('WebService::Async::Segment');
50              
51 5         10 my $self;
52              
53 5 100       14 $args{user_id} = delete $args{userId} if $args{userId};
54 5 100       15 $args{anonymous_id} = delete $args{anonymousId} if $args{anonymousId};
55              
56 5         28 $self->{$_} = $args{$_} for (qw(api_client user_id anonymous_id));
57 5 100       14 $self->{traits} = {%{$args{traits}}} if $args{traits};
  2         8  
58              
59 5         13 bless $self, $class;
60              
61 5         20 return $self;
62             }
63              
64             =head2 user_id
65              
66             Unique identifier for the user in the database.
67              
68             =cut
69              
70 19     19 1 1559 sub user_id { shift->{user_id} }
71              
72             =head2 anonymous_id
73              
74             A pseudo-unique substitute for a User ID, for cases when you don't have an absolutely unique identifier.
75              
76             =cut
77              
78 18     18 1 2709 sub anonymous_id { shift->{anonymous_id} }
79              
80             =head2 traits
81              
82             Free-form dictionary of traits of the user, containg both standard and custom attributes.
83             For more information on standard (reserved) traits please refer to L.
84              
85             =cut
86              
87 15     15 1 2144 sub traits { shift->{traits} }
88              
89             =head2 api_client
90              
91             A C object acting as Segment HTTP API client.
92              
93             =cut
94              
95 10     10 1 53 sub api_client { shift->{api_client} }
96              
97             =head2 identify
98              
99             Makes an B call on the current customer.
100             For a detailed information on the API call please refer to: L.
101              
102             It can be called with the following named params:
103              
104             =over
105              
106             =item * C or C - Unique identifier of a user (will overwrite object's attribute).
107              
108             =item * C or C - A pseudo-unique substitute for a User ID (will overwrite object's attribute).
109              
110             =item * C - Free-form dictionary of traits of the user, like email or name (will overwrite object's attribute).
111              
112             =item * C - Context information of the API call.
113             Note that the API wrapper automatically sets context B and B fields.
114              
115             =item * C - Dictionary of destinations to either enable or disable.
116              
117             =item * C - Timestamp when the message itself took place, defaulted to the current time by the Segment Tracking API. It is an ISO-8601 date string
118              
119             =item * C - Dictionary of custom business specific fileds.
120              
121             =back
122              
123             About common fields please refer to: L.
124              
125             It returns a L object.
126              
127             =cut
128              
129             sub identify {
130 6     6 1 6610 my ($self, %args) = @_;
131              
132 6 50 0     16 $args{traits} //= $self->traits if $self->traits;
133              
134 6 100       21 $args{user_id} = delete $args{userId} if $args{userId};
135 6 100       20 $args{anonymous_id} = delete $args{anonymousId} if $args{anonymousId};
136              
137 6         29 my %call_args = $self->_make_call_args(\%args, [COMMON_FIELDS, qw(user_id anonymous_id traits)]);
138              
139             return $self->api_client->method_call('identify', %call_args)->then(
140             sub {
141 5     5   656 for (qw(user_id anonymous_id)) {
142 10 100       32 $self->{$_} = $args{$_} if $args{$_};
143             }
144 5 100       15 $self->{traits} = {%{$args{traits}}} if $args{traits};
  1         4  
145              
146 5         15 return Future->done(@_);
147 6         23 })->retain;
148             }
149              
150             =head2 track
151              
152             Makes a B call on the current customer. It can take any standard (B and B), common or custom fields.
153             For more information on track API please refer to L.
154              
155             It can be called with the following parameters:
156              
157             =over
158              
159             =item * C - required. event name.
160              
161             =item * C - Free-form dictionary of event properties.
162              
163             =item * C - Context information of the API call.
164             Note that the API wrapper automatically sets context B and B fields.
165              
166             =item * C - Dictionary of destinations to either enable or disable.
167              
168             =item * C - Timestamp when the message itself took place, defaulted to the current time by the Segment Tracking API. It is an ISO-8601 date string.
169              
170             =item * C - Dictionary of custom business specific fileds.
171              
172             =back
173              
174             About common API call params: L.
175              
176             It returns a L object.
177              
178             =cut
179              
180             sub track {
181 5     5 1 11659 my ($self, %args) = @_;
182              
183 5 100       23 return Future->fail('ValidationError', 'segment', 'Missing required argument "event"') unless $args{event};
184              
185 4         19 my %call_args = $self->_make_call_args(\%args, [COMMON_FIELDS, qw(event properties)]);
186              
187 4         13 return $self->api_client->method_call('track', %call_args);
188             }
189              
190             sub _make_call_args {
191 10     10   22 my ($self, $args, $accepted_fields) = @_;
192 10   50     27 $args //= {};
193 10   100     53 my $custom = delete $args->{custom} // {};
194              
195 10         29 for my $field (keys %$args) {
196 19 100       38 delete $args->{$field} unless grep { $field eq $_ } (@$accepted_fields);
  104         203  
197             }
198              
199 10         23 for (qw(user_id anonymous_id)) {
200 20 100 66     48 $args->{$_} //= $self->$_ if $self->$_;
201             }
202              
203 10 50       28 my %call_args = map { $args->{$_} ? ($_ => $args->{$_}) : () } (keys %$args);
  19         59  
204              
205 10         65 return (%$custom, %call_args);
206             }
207              
208             1;
209              
210             __END__