Segment.pm

filter: Statementstmt 100% 97 / 97 Branchbran 90.0% 18 / 20 Conditioncond 68.7% 11 / 16 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 25 / 25 Podpod 100% 7 / 7 totaltotal 92.5% 162 / 175 CC 19 SCAR 11.7
CC
19
Cov
95%
_snake_case_to_camelCase
8.1
method_call
5.0
configure
3.0
CRAP 3.2
« Segment/Customer.pm ↑ Summary
1 package WebService::Async::Segment;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use JSON::MaybeUTF8 qw(encode_json_utf8 decode_json_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use WebService::Async::Segment::Customer;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use constant SEGMENT_BASE_URL => 'https://api.segment.io/v1/';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use constant TIMEOUT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use constant SNAKE_FIELDS => {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 anonymous_id => 'anonymousId',
23 user_id => 'userId',
24 sent_at => 'sentAt',
25 traits => {
26 created_at => 'createdAt',
27 first_name => 'firstName',
28 last_name => 'lastName',
29 address => {
30 postal_code => 'postalCode',
31 },
32 },
33 context => {
34 user_agent => 'userAgent',
35 group_id => 'groupId',
36 device => {
37 advertising_id => 'advertisingId',
38 ad_tracking_enabled => 'adTrackingEnabled'
39 },
40 },
41 3 };
42  
43 3 our $VERSION = '0.001';
44  
45 =head1 NAME
46  
47 WebService::Async::Segment - Unofficial support for the Segment service
48  
49 =head1 DESCRIPTION
50  
51 This class acts as a L<Future>-based async Perl wrapper for segment HTTP API.
52  
53 =cut
54  
55 =head1 METHODS
56  
57 =head2 configure
58  
59 Overrides the same method of the parent class L<IO::Async::Notifier>; required for object initialization.
60  
61 parameters:
62  
63 =over 4
64  
65 =item * C<write_key> - the API token of a Segment source.
66  
67 =item * C<base_uri> - the base uri of the Segment host, primarily useful for setting up test mock servers.
68  
69 =back
70  
71 =cut
72  
73 2 ▶sub configure {
Subroutine configure: called CC=3 SCAR=11.1
Pod documented
74 2 my ($self, %args) = @_;
75  
76 2 for my $k (qw(write_key base_uri)) {
77 4 ▶ $self->{$k} = delete $args{$k} if exists $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$k} 4 0 4
78 }
79  
80 2 $self->next::method(%args);
81 }
82  
83 =head2 write_key
84  
85 API token of the intended Segment source
86  
87 =cut
88  
89 14 ▶sub write_key { shift->{write_key} }
Subroutine write_key: called CC=1 SCAR=0.0
Pod documented
90  
91 =head2 base_uri
92  
93 Server endpoint. Defaults to C<< https://api.segment.io/v1/ >>.
94  
95 Returns a L<URI> instance.
96  
97 =cut
98  
99 14 ▶sub base_uri {
Subroutine base_uri: called CC=3 SCAR=11.0
Pod documented
100 14 my $self = shift;
101 14 ▶ return $self->{base_uri} if blessed($self->{base_uri});
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($self->{"base_uri"}) 12 2 14
102 2 ▶ $self->{base_uri} = URI->new($self->{base_uri} // SEGMENT_BASE_URL);
Condition50%1/2
$self->{"base_uri"} // "https://api.segment.io/v1/"
l!l
20
MC/DC0%0/1
$self->{"base_uri"} // "https://api.segment.io/v1/"
$self->{"base_uri"}
Truth table50%1/2
A
A= $self->{"base_uri"}
Aresult
11
00
103 2 return $self->{base_uri};
104 }
105  
106 =head2 ua
107  
108 A L<Net::Async::HTTP> object acting as HTTP user agent
109  
110 =cut
111  
112 14 ▶sub ua {
Subroutine ua: called CC=2 SCAR=6.9
Pod documented
113 14 my ($self) = @_;
114  
115 14 ▶ return $self->{ua} if $self->{ua};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ua"} 12 2 14
116  
117 2 $self->{ua} = Net::Async::HTTP->new(
118 fail_on_error => 1,
119 decode_content => 1,
120 pipeline => 0,
121 stall_timeout => TIMEOUT,
122 max_connections_per_host => 2,
123 user_agent => 'Mozilla/4.0 (WebService::Async::Segment; DERIV@cpan.org; https://metacpan.org/pod/WebService::Async::Segment)',
124 );
125  
126 2 $self->add_child($self->{ua});
127  
128 2 return $self->{ua};
129 }
130  
131 =head2 basic_authentication
132  
133 Settings required for basic HTTP authentication
134  
135 =cut
136  
137 14 ▶sub basic_authentication {
Subroutine basic_authentication: called CC=2 SCAR=7.2
Pod documented
138 14 my $self = shift;
139  
140 #C<Net::Async::Http> basic authentication information
141 return {
142 14 ▶ user => $self->write_key // '',
Condition50%1/2
$self->write_key // ""
l!l
140
MC/DC0%0/1
$self->write_key // ""
$self->write_key
Truth table50%1/2
A
A= $self->write_key
Aresult
11
00
143 pass => ''
144 };
145 }
146  
147 =head2 method_call
148  
149 Makes a Segment method call. It automatically defaults C<sent_at> to the current time and C<< context->{library} >> to the current module.
150  
151 It takes the following named parameters:
152  
153 =over 4
154  
155 =item * C<method> - required. Segment method name (such as B<identify> and B<track>).
156  
157 =item * C<args> - optional. Method arguments represented as a dictionary. This may include either common, method-specific or custom fields.
158  
159 =back
160  
161 Please refer to L<https://segment.com/docs/spec/common/> for a full list of common fields supported by Segment.
162  
163 It returns a L<Future> object.
164  
165 =cut
166  
167 18 ▶sub method_call {
Subroutine method_call: called CC=5 SCAR=16.1
Pod documented
168 18 my ($self, $method, %args) = @_;
169  
170 18 ▶ $args{sent_at} ||= Time::Moment->now_utc->to_string();
Condition66%2/3
$args{"sent_at"} ||= "Time::Moment"->now_utc->to_string
l!l&&r!l&&!r
1170
MC/DC0%0/2
$args{"sent_at"} ||= "Time::Moment"->now_utc->to_string
$args{"sent_at"} "Time::Moment"->now_utc->to_string
Truth table66%2/3
A ||= B
A= $args{"sent_at"}B= "Time::Moment"->now_utc->to_string
ABresult
1X1
011
000
171 18 $args{context}->{library}->{name} = ref $self;
172 18 $args{context}->{library}->{version} = $VERSION;
173  
174 18 ▶ return Future->fail('ValidationError', 'segment', 'Method name is missing', 'segment', $method, %args) unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 1 17 18
175  
176 return Future->fail('ValidationError', 'segment', 'Both user_id and anonymous_id are missing', $method, %args)
177 17 ▶ unless $args{user_id} or $args{anonymous_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"user_id"} or $args{"anonymous_id"} 3 14 17
Condition100%3/3
$args{"user_id"} or $args{"anonymous_id"}
l!l&&r!l&&!r
1133
MC/DC100%2/2
$args{"user_id"} or $args{"anonymous_id"}
$args{"user_id"} $args{"anonymous_id"}
Truth table100%3/3
A or B
A= $args{"user_id"}B= $args{"anonymous_id"}
ABresult
1X1
011
000
178  
179 14 %args = _snake_case_to_camelCase(\%args, SNAKE_FIELDS)->%*;
180  
181 14 $log->tracef('Segment method %s called with params %s', $method, \%args);
182  
183 return $self->ua->POST(
184 URI->new_abs($method, $self->base_uri),
185 encode_json_utf8(\%args),
186 content_type => 'application/json',
187 14 %{$self->basic_authentication},
188 )->then(
189 13 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
190 13 my $result = shift;
191  
192 13 $log->tracef('Segment response for %s method received: %s', $method, $result);
193  
194 13 my $response_str = $result->content;
195  
196 13 ▶ if ($result->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->code == 200) 12 1 13
197 12 $log->tracef('Segment %s method call finished successfully.', $method);
198  
199 12 return Future->done(1);
200 }
201  
202 1 return Future->fail('RequestFailed', 'segment', $response_str);
203 }
204 )->on_fail(
205 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
206 2 $log->errorf('Segment method %s call failed: %s', $method, $_[0]);
207 14 })->retain;
208 }
209  
210 =head2 new_customer
211  
212 Creates a new C<WebService::Async::Segment::Customer> object as the starting point of making B<identify> and B<track> calls.
213 It may takes the following named standard arguments to populate the customer onject with:
214  
215 =over 4
216  
217 =item * C<user_id> or C<userId> - Unique identifier of a user.
218  
219 =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.
220  
221 =item * C<traits> - Free-form dictionary of traits of the user, like email or name.
222  
223 =back
224  
225 =cut
226  
227 4 ▶sub new_customer {
Subroutine new_customer: called CC=1 SCAR=0.0
Pod documented
228 4 my ($self, %args) = @_;
229  
230 4 $args{api_client} = $self;
231  
232 4 $log->tracef('A new customer is being created with: %s', \%args);
233  
234 4 return WebService::Async::Segment::Customer->new(%args);
235 }
236  
237 =head2 _snake_case_to_camelCase
238  
239 Creates a deep copy of API call args, replacing the standard snake_case keys with equivalent camelCases, necessary to keep consistent with Segment HTTP API.
240 It doesn't automatically alter any non-standard custom keys even they are snake_case.
241  
242 =over 4
243  
244 =item * C<$args> - call args as a hash reference.
245  
246 =item * C<$snake_fields> - a hash ref representing mapping from snake_case to camelCase.
247  
248 =back
249  
250 Returns a hash reference of args with altered keys.
251  
252 =cut
253  
254 134 ▶sub _snake_case_to_camelCase {
Subroutine _snake_case_to_camelCase: called CC=8 SCAR=20.9
255 134 my ($args, $snake_fields) = @_;
256  
257 134 ▶ return $args unless ref($args) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 87 47 134
258 47 ▶ $snake_fields = {} unless ref($snake_fields) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $snake_fields eq "HASH" 15 32 47
259  
260 47 my $result;
261 47 for my $key (keys %$args) {
262 120 ▶ next unless defined $args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{$key} 0 120 120
263  
264 120 ▶ if ($snake_fields->{$key} and not(ref $snake_fields->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($snake_fields->{$key} and not ref $snake_fields->{$key}) 40 80 120
Condition100%3/3
$snake_fields->{$key} and not ref $snake_fields->{$key}
!ll&&!rl&&r
621840
MC/DC100%2/2
$snake_fields->{$key} and not ref $snake_fields->{$key}
$snake_fields->{$key} not ref $snake_fields->{$key}
Truth table100%3/3
A and not(B)
A= $snake_fields->{$key}B= not ref $snake_fields->{$key}
ABresult
0X0
100
111
265 40 my $camel = $snake_fields->{$key};
266 40 ▶ $result->{$camel} = _snake_case_to_camelCase($args->{$camel} // $args->{$key}, $snake_fields->{$camel});
Condition33%1/3
$args->{$camel} // $args->{$key}
l!l&&r!l&&!r
0400
MC/DC0%0/2
$args->{$camel} // $args->{$key}
$args->{$camel} $args->{$key}
Truth table33%1/3
A // B
A= $args->{$camel}B= $args->{$key}
ABresult
1X1
011
000
267 40 next;
268 }
269 80 $result->{$key} = _snake_case_to_camelCase($args->{$key}, $snake_fields->{$key});
270 }
271 47 return $result;
272 }
273  
274 3 1;
275  
276 __END__
« Segment/Customer.pm ↑ Summary