Segment/Customer.pm

filter: Statementstmt 100% 53 / 53 Branchbran 92.8% 26 / 28 Conditioncond 50.0% 5 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 89.6% 104 / 116 CC 23 SCAR 16.9
CC
23
Cov
92%
_make_call_args
9.2
new
7.0
identify
5.1
CRAP 5.4
↑ Summary Segment.pm »
1 package WebService::Async::Segment::Customer;
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 ▶use constant COMMON_FIELDS => qw(context integrations timestamp);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 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</identify> and L</track> 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<WebService::Async::Segment::Customer>).
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<api_client> - Segment API wrapper object.
33  
34 =item * C<user_id> or C<userId> - Unique identifier of a user.
35  
36 =item * C<anonymous_id> or C<anonymousId> - A pseudo-unique substitute for a User ID, for cases when you don't have an absolutely unique identifier.
37  
38 =item * C<traits> - Free-form dictionary of traits of the user, like email or name.
39  
40 =back
41  
42 =cut
43  
44 7 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
45 7 my ($class, %args) = @_;
46  
47 7 ▶ die 'Missing required arg api_client' unless $args{api_client};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"api_client"} 1 6 7
48 6 my $api_client = $args{api_client};
49 6 ▶ die 'Invalid api_client value' unless $api_client->isa('WebService::Async::Segment');
Branch: 1 branch
BranchTrueFalseTotal
unless $api_client->isa("WebService::Async::Segment") 1 5 6
50  
51 5 my $self;
52  
53 5 ▶ $args{user_id} = delete $args{userId} if $args{userId};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"userId"} 1 4 5
54 5 ▶ $args{anonymous_id} = delete $args{anonymousId} if $args{anonymousId};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"anonymousId"} 1 4 5
55  
56 5 $self->{$_} = $args{$_} for (qw(api_client user_id anonymous_id));
57 5 ▶ $self->{traits} = {%{$args{traits}}} if $args{traits};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"traits"} 2 3 5
58  
59 5 bless $self, $class;
60  
61 5 return $self;
62 }
63  
64 =head2 user_id
65  
66 Unique identifier for the user in the database.
67  
68 =cut
69  
70 19 ▶sub user_id { shift->{user_id} }
Subroutine user_id: called CC=1 SCAR=0.0
Pod documented
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 ▶sub anonymous_id { shift->{anonymous_id} }
Subroutine anonymous_id: called CC=1 SCAR=0.0
Pod documented
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<https://segment.com/docs/spec/identify/#traits>.
84  
85 =cut
86  
87 15 ▶sub traits { shift->{traits} }
Subroutine traits: called CC=1 SCAR=0.0
Pod documented
88  
89 =head2 api_client
90  
91 A C<WebService::Async::Segment> object acting as Segment HTTP API client.
92  
93 =cut
94  
95 10 ▶sub api_client { shift->{api_client} }
Subroutine api_client: called CC=1 SCAR=0.0
Pod documented
96  
97 =head2 identify
98  
99 Makes an B<identify> call on the current customer.
100 For a detailed information on the API call please refer to: L<https://segment.com/docs/spec/identify/>.
101  
102 It can be called with the following named params:
103  
104 =over
105  
106 =item * C<user_id> or C<userId> - Unique identifier of a user (will overwrite object's attribute).
107  
108 =item * C<anonymous_id> or C<anonymousId> - A pseudo-unique substitute for a User ID (will overwrite object's attribute).
109  
110 =item * C<traits> - Free-form dictionary of traits of the user, like email or name (will overwrite object's attribute).
111  
112 =item * C<context> - Context information of the API call.
113 Note that the API wrapper automatically sets context B<sentAt> and B<library> fields.
114  
115 =item * C<integrations> - Dictionary of destinations to either enable or disable.
116  
117 =item * C<timestamp> - 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<custom> - Dictionary of custom business specific fileds.
120  
121 =back
122  
123 About common fields please refer to: L<https://segment.com/docs/spec/common/>.
124  
125 It returns a L<Future> object.
126  
127 =cut
128  
129 6 ▶sub identify {
Subroutine identify: called CC=5 SCAR=16.3
Pod documented
130 6 my ($self, %args) = @_;
131  
132 6 ▶ $args{traits} //= $self->traits if $self->traits;
Branch: 1 branch
BranchTrueFalseTotal
if $self->traits 0 6 6
Condition0%0/3
$args{"traits"} //= $self->traits
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"traits"} //= $self->traits
$args{"traits"} $self->traits
Truth table0%0/3
A //= B
A= $args{"traits"}B= $self->traits
ABresult
1X1
011
000
133  
134 6 ▶ $args{user_id} = delete $args{userId} if $args{userId};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"userId"} 1 5 6
135 6 ▶ $args{anonymous_id} = delete $args{anonymousId} if $args{anonymousId};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"anonymousId"} 1 5 6
136  
137 6 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 5 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
141 5 for (qw(user_id anonymous_id)) {
142 10 ▶ $self->{$_} = $args{$_} if $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $args{$_} 8 2 10
143 }
144 5 ▶ $self->{traits} = {%{$args{traits}}} if $args{traits};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"traits"} 1 4 5
145  
146 5 return Future->done(@_);
147 6 })->retain;
148 }
149  
150 =head2 track
151  
152 Makes a B<track> call on the current customer. It can take any standard (B<event> and B<properties>), common or custom fields.
153 For more information on track API please refer to L<https://segment.com/docs/spec/track/>.
154  
155 It can be called with the following parameters:
156  
157 =over
158  
159 =item * C<event> - required. event name.
160  
161 =item * C<properties> - Free-form dictionary of event properties.
162  
163 =item * C<context> - Context information of the API call.
164 Note that the API wrapper automatically sets context B<sentAt> and B<library> fields.
165  
166 =item * C<integrations> - Dictionary of destinations to either enable or disable.
167  
168 =item * C<timestamp> - 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<custom> - Dictionary of custom business specific fileds.
171  
172 =back
173  
174 About common API call params: L<https://segment.com/docs/spec/common/>.
175  
176 It returns a L<Future> object.
177  
178 =cut
179  
180 5 ▶sub track {
Subroutine track: called CC=2 SCAR=6.9
Pod documented
181 5 my ($self, %args) = @_;
182  
183 5 ▶ return Future->fail('ValidationError', 'segment', 'Missing required argument "event"') unless $args{event};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"event"} 1 4 5
184  
185 4 my %call_args = $self->_make_call_args(\%args, [COMMON_FIELDS, qw(event properties)]);
186  
187 4 return $self->api_client->method_call('track', %call_args);
188 }
189  
190 10 ▶sub _make_call_args {
Subroutine _make_call_args: called CC=9 SCAR=22.1
191 10 my ($self, $args, $accepted_fields) = @_;
192 10 ▶ $args //= {};
Condition50%1/2
$args //= {}
l!l
100
MC/DC0%0/1
$args //= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
193 10 ▶ my $custom = delete $args->{custom} // {};
Condition100%2/2
delete $args->{"custom"} // {}
l!l
28
MC/DC100%1/1
delete $args->{"custom"} // {}
delete $args->{"custom"}
Truth table100%2/2
A
A= delete $args->{"custom"}
Aresult
11
00
194  
195 10 for my $field (keys %$args) {
196 19 ▶ delete $args->{$field} unless grep { $field eq $_ } (@$accepted_fields);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$field eq $_;} @$accepted_fields 5 14 19
197 }
198  
199 10 for (qw(user_id anonymous_id)) {
200 20 ▶ $args->{$_} //= $self->$_ if $self->$_;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$_ 8 12 20
Condition66%2/3
$args->{$_} //= $self->$_
l!l&&r!l&&!r
350
MC/DC0%0/2
$args->{$_} //= $self->$_
$args->{$_} $self->$_
Truth table66%2/3
A //= B
A= $args->{$_}B= $self->$_
ABresult
1X1
011
000
201 }
202  
203 10 ▶ my %call_args = map { $args->{$_} ? ($_ => $args->{$_}) : () } (keys %$args);
Branch: 1 branch
BranchTrueFalseTotal
$args->{$_} ? : 19 0 19
204  
205 10 return (%$custom, %call_args);
206 }
207  
208 4 1;
209  
210 __END__
↑ Summary Segment.pm »