File Coverage

blib/lib/WebService/SiftScience.pm
Criterion Covered Total %
statement 171 173 98.8
branch 87 164 53.0
condition 35 93 37.6
subroutine 38 38 100.0
pod n/a
total 331 468 70.7


line stmt bran cond sub pod time code
1             package WebService::SiftScience;
2             $WebService::SiftScience::VERSION = '0.0100';
3 4     4   148396 use Moo;
  4         26369  
  4         15  
4             with 'WebService::Client';
5              
6             # VERSION
7              
8 4     4   5500 use Method::Signatures;
  4         167078  
  4         27  
9              
10             has '+base_url' => ( default => 'http://api.siftscience.com/v203' );
11             has api_key => ( is => 'ro', required => 1 );
12             has events_uri => ( is => 'ro', default => '/events' );
13             has score_uri => ( is => 'ro', default => '/score' );
14             has users_uri => ( is => 'ro', default => '/users' );
15              
16 4 50 33 4   344206 method get_score (Str $user_id) {
  1 50   1   285897  
  1 50       4  
  1         1  
  1         5  
  1         64  
17 1         3 return $self->get($self->_score_uri($user_id) .
18             '?api_key=' . $self->api_key);
19             }
20              
21 4 50 33 4   21984 method create_event (Str $user_id, Str $type, Maybe[HashRef] $data = {}) {
  40 50 33 40   89  
  40 50 33     149  
  40 50       62  
  40 100       146  
  40 50       2240  
  40 50       56  
  40         142  
  40         2119  
  40         131  
  40         2100  
22 40         586 return $self->post($self->events_uri, {
23             '$type' => $type,
24             '$api_key' => $self->api_key,
25             '$user_id' => $user_id,
26             ( %$data ) x!! $data,
27             });
28             }
29              
30 4 50 33 4   12740 method create_account (Str $user_id, Maybe[HashRef] $data) {
  4 50 33 4   814267  
  4 50       21  
  4 50       8  
  4 50       36  
  4         293  
  4         8  
  4         20  
  4         217  
31 4         148 return $self->create_event($user_id, '$create_account', $data);
32             }
33              
34 4 50 33 4   12119 method update_account (Str $user_id, Maybe[HashRef] $data) {
  4 50 33 4   681009  
  4 50       18  
  4 50       5  
  4 50       44  
  4         316  
  4         6  
  4         19  
  4         248  
35 4         18 return $self->create_event($user_id, '$update_account', $data);
36             }
37              
38 4 50 33 4   11967 method create_order (Str $user_id, Maybe[HashRef] $data) {
  6 50 33 6   854584  
  6 50       29  
  6 50       10  
  6 50       59  
  6         471  
  6         11  
  6         32  
  6         373  
39 6         27 return $self->create_event($user_id, '$create_order', $data);
40             }
41              
42 4 50 33 4   11843 method transaction (Str $user_id, Maybe[HashRef] $data) {
  2 50 33 2   342379  
  2 100       10  
  2 50       4  
  2 0       19  
  2         192  
  1         4  
  1         6  
  0         0  
43 0         0 return $self->create_event($user_id, '$transaction', $data);
44             }
45              
46 4 50 33 4   9242 method link_session_to_user (Str $user_id, $data) {
  3 50   3   172786  
  3 100       8  
  3 100       5  
  3         17  
  3         191  
  2         3  
  2         12  
47 1         6 return $self->create_event($user_id, '$link_session_to_user', $data);
48             }
49              
50 4 50 66 4   11273 method add_item_to_cart (Str $user_id, Maybe[HashRef] $data) {
  4 50 66 4   514006  
  4 50       19  
  4 50       12  
  4 50       38  
  4         290715  
  4         8  
  4         22  
  4         3952  
51 4         22 return $self->create_event($user_id, '$add_item_to_cart', $data);
52             }
53              
54 4 50 33 4   11945 method remove_item_from_cart (Str $user_id, Maybe[HashRef] $data) {
  4 50 33 4   679972  
  4 50       24  
  4 50       7  
  4 50       43  
  4         324  
  4         6  
  4         20  
  4         224  
55 4         21 return $self->create_event($user_id, '$remove_item_from_cart', $data);
56             }
57              
58 4 50 33 4   11771 method submit_review (Str $user_id, Maybe[HashRef] $data) {
  4 50 33 4   682371  
  4 50       30  
  4 50       11  
  4 50       48  
  4         312  
  4         8  
  4         20  
  4         214  
59 4         19 return $self->create_event($user_id, '$submit_review', $data);
60             }
61              
62 4 50 33 4   11482 method send_message (Str $user_id, Maybe[HashRef] $data) {
  4 50 33 4   679215  
  4 50       19  
  4 50       9  
  4 50       45  
  4         351  
  4         6  
  4         22  
  4         278  
63 4         21 return $self->create_event($user_id, '$send_message', $data);
64             }
65              
66 4 50 66 4   17753 method login (Str $user_id, Maybe[HashRef] $data = {}) {
  5 50 66 5   685046  
  5 100       25  
  5 50       10  
  5 50       53  
  5         327027  
  5         28  
  5         4748  
67 5         41 return $self->create_event($user_id, '$login', $data);
68             }
69              
70 4 50 33 4   7762 method logout (Str $user_id) {
  1 50   1   169184  
  1 50       6  
  1         2  
  1         10  
  1         108  
71 1         6 return $self->create_event($user_id, '$logout');
72             }
73              
74 4 50 33 4   14771 method custom_event (Str $user_id, Str $type, Maybe[HashRef] $data) {
  4 50 33 4   345165  
  4 100 33     14  
  4 50       6  
  4 50       25  
  4 50       366  
  3 50       7  
  3         13  
  3         263  
  3         6  
  3         18  
  3         222  
75 3         15 return $self->create_event($user_id, $type, $data);
76             }
77              
78 4 50 33 4   9590 method label_user (Str $user_id, $data) {
  1 50   1   1841  
  1 50       4  
  1 50       2  
  1         10  
  1         290344  
  1         2  
  1         3  
79 1         19 return $self->post($self->_label_uri($user_id), {
80             '$api_key' => $self->api_key,
81             ( %$data ) x!! $data,
82             });
83             }
84              
85 4 50 33 4   7191 method unlabel_user (Str $user_id) {
  1 50   1   233151  
  1 50       5  
  1         1  
  1         5  
  1         97  
86 1         11 return $self->delete($self->_label_uri($user_id) .
87             '?api_key=' . $self->api_key);
88             }
89              
90 4 50 33 4   6602 method _score_uri (Str $user_id) {
  1 50   1   2  
  1 50       3  
  1         2  
  1         3  
  1         51  
91 1         10 return $self->score_uri . "/$user_id";
92             }
93              
94 4 50 33 4   6403 method _label_uri (Str $user_id) {
  2 50   2   3  
  2 50       7  
  2         2  
  2         13  
  2         128  
95 2         25 return $self->users_uri . "/$user_id/labels";
96             }
97              
98              
99             1;
100              
101             __END__
102              
103             =pod
104              
105             =encoding UTF-8
106              
107             =head1 NAME
108              
109             WebService::SiftScience
110              
111             =head1 VERSION
112              
113             version 0.0100
114              
115             =head1 SYNOPSIS
116              
117             use WebService::SiftScience;
118              
119             my $ss = WebService::SiftScience->new(
120             api_key => 'YOUR_API_KEY_HERE',
121             );
122              
123             $ss->create_account(...);
124              
125             =head1 DESCRIPTION
126              
127             This module provides bindings for the
128             L<SiftScience|https://www.siftscience.com/resources/references/> API.
129              
130             =for markdown [![Build Status](https://travis-ci.org/aanari/WebService-SiftScience.svg?branch=master)](https://travis-ci.org/aanari/WebService-SiftScience)
131              
132             =head1 METHODS
133              
134             =head2 new
135              
136             Instantiates a new WebService::SiftScience client object.
137              
138             my $ss = WebService::SiftScience->new(
139             api_key => $api_key,
140             timeout => $retries, # optional
141             retries => $retries, # optional
142             );
143              
144             B<Parameters>
145              
146             =over 4
147              
148             =item - C<api_key>
149              
150             I<Required>E<10> E<8>
151              
152             A valid SiftScience API key for your account.
153              
154             =item - C<timeout>
155              
156             I<Optional>E<10> E<8>
157              
158             The number of seconds to wait per request until timing out. Defaults to C<10>.
159              
160             =item - C<retries>
161              
162             I<Optional>E<10> E<8>
163              
164             The number of times to retry requests in cases when SiftScience returns a 5xx response. Defaults to C<0>.
165              
166             =back
167              
168             =head2 add_item_to_cart
169              
170             Record when a user adds an item to their shopping cart or list.
171              
172             B<Request:>
173              
174             add_item_to_cart('billy_jones_301', {
175             '$session_id' => 'gigtleqddo84l8cm15qe4il',
176             '$item' => {
177             '$item_id' => 'B004834GQO',
178             '$product_title' => 'The Slanket Blanket-Texas Tea',
179             '$price' => '39990000',
180             '$currency_code' => 'USD',
181             ...
182             },
183             });
184              
185             B<Response:>
186              
187             {
188             error_message => 'OK',
189             status => 0,
190             time => 1428607810,
191             request => { ... }
192             }
193              
194             =head2 create_account
195              
196             Capture account creation and user details.
197              
198             B<Request:>
199              
200             create_account('billy_jones_301', {
201             '$session_id' => 'gigtleqddo84l8cm15qe4il',
202             '$user_email' => 'bill@gmail.com',
203             '$name' => 'Bill Jones',
204             '$phone' => '1-415-555-6040',
205             '$referrer_user_id' => 'janejane101',
206             ...
207             });
208              
209             B<Response:>
210              
211             {
212             error_message => 'OK',
213             status => 0,
214             time => 1428607810,
215             request => { ... }
216             }
217              
218             =head2 create_order
219              
220             Record when a user submits an order for products or services they intend to purchase. This API event should contain the products/services ordered, the payment instrument proposed, and user identification data.
221              
222             B<Request:>
223              
224             create_order('billy_jones_301', {
225             '$session_id' => 'gigtleqddo84l8cm15qe4il',
226             '$order_id' => 'ORDER-28168441',
227             '$user_email' => 'bill@gmail.com',
228             '$amount' => 506790000,
229             '$currency_code' => 'USD',
230             ...
231             });
232              
233             B<Response:>
234              
235             {
236             error_message => 'OK',
237             status => 0,
238             time => 1428607810,
239             request => { ... }
240             }
241              
242             =head2 custom_event
243              
244             Event that you can come up with on your own, in order to capture user behavior not currently captured in Sift Science's supported set of events.
245              
246             B<Request:>
247              
248             custom_event('billy_jones_301', 'make_call', {
249             recipient_user_id => 'marylee819',
250             call_duration => 4428,
251             });
252              
253             B<Response:>
254              
255             {
256             error_message => 'OK',
257             status => 0,
258             time => 1428607810,
259             request => { ... }
260             }
261              
262             =head2 get_score
263              
264             Retrieve a Sift Score for a particular user on your site, including a list of signals that describe the reasoning behind the score, and the latest label information if the user has been labeled.
265              
266             B<Request:>
267              
268             get_score('billy_jones_301');
269              
270             B<Response:>
271              
272             {
273             user_id => 'billy_jones_301',
274             score => 0.93,
275             error_message => 'OK',
276             status => 0,
277             reasons => [
278             name => 'UsersPerDevice',
279             value => 4,
280             details => {
281             users => 'a, b, c, d',
282             },
283             ],
284             latest_label => {
285             is_bad => JSON::true,
286             time => 1350201660000,
287             reasons => [
288             '$chargeback',
289             '$spam',
290             ],
291             description => 'known fraudster',
292             },
293             }
294              
295             =head2 label_user
296              
297             Label a user as bad (or not bad).
298              
299             B<Request:>
300              
301             label_user('billy_jones_301', {
302             '$is_bad' => JSON::true,
303             '$reasons' => ['$chargeback'],
304             '$description' => 'Freeform text describing the user or incident.',
305             '$source' => 'Payment Gateway',
306             '$analyst' => 'someone@your-site.com',
307             });
308              
309             B<Response:>
310              
311             {
312             error_message => 'OK',
313             status => 0,
314             time => 1428607810,
315             request => { ... }
316             }
317              
318             =head2 link_session_to_user
319              
320             Associate data from a specific session to a user. Generally used only in anonymous checkout workflows.
321              
322             B<Request:>
323              
324             link_session_to_user('billy_jones_301', {
325             '$session_id' => 'gigtleqddo84l8cm15qe4il',
326             });
327              
328             B<Response:>
329              
330             {
331             error_message => 'OK',
332             status => 0,
333             time => 1428607810,
334             request => { ... }
335             }
336              
337             =head2 login
338              
339             Record when a user attempts to log in.
340              
341             B<Request:>
342              
343             login('billy_jones_301', {
344             '$session_id' => 'gigtleqddo84l8cm15qe4il',
345             '$login_status' => '$success',
346             });
347              
348             B<Response:>
349              
350             {
351             error_message => 'OK',
352             status => 0,
353             time => 1428607810,
354             request => { ... }
355             }
356              
357             =head2 logout
358              
359             Record when a user logs out.
360              
361             B<Request:>
362              
363             logout('billy_jones_301');
364              
365             B<Response:>
366              
367             {
368             error_message => 'OK',
369             status => 0,
370             time => 1428607810,
371             request => { ... }
372             }
373              
374             =head2 transaction
375              
376             Record attempts to exchange money, credit or other tokens of value. This is most commonly used to record the results of interactions with a payment gateway, e.g., recording that a credit card authorization attempt failed.
377              
378             B<Request:>
379              
380             transaction('billy_jones_301', {
381             '$session_id' => 'gigtleqddo84l8cm15qe4il',
382             '$order_id' => 'ORDER-28168441',
383             '$user_email' => 'bill@gmail.com',
384             '$transaction_type' => '$sale',
385             '$transaction_status' => '$success',
386             '$transaction_id' => '719637215',
387             '$amount' => 506790000,
388             '$currency_code' => 'USD',
389             ...
390             });
391              
392             B<Response:>
393              
394             {
395             error_message => 'OK',
396             status => 0,
397             time => 1428607810,
398             request => { ... }
399             }
400              
401             =head2 remove_item_from_cart
402              
403             Record when a user removes an item from their shopping cart or list.
404              
405             B<Request:>
406              
407             remove_item_from_cart('billy_jones_301', {
408             '$session_id' => 'gigtleqddo84l8cm15qe4il',
409             '$item' => {
410             '$item_id' => 'B004834GQO',
411             '$product_title' => 'The Slanket Blanket-Texas Tea',
412             '$price' => '39990000',
413             '$currency_code' => 'USD',
414             ...
415             },
416             });
417              
418             B<Response:>
419              
420             {
421             error_message => 'OK',
422             status => 0,
423             time => 1428607810,
424             request => { ... }
425             }
426              
427             =head2 send_message
428              
429             Record when a user sends a message to another user i.e., the recipient.
430              
431             B<Request:>
432              
433             send_message('billy_jones_301', {
434             '$recipient_user_id' => '512924123',
435             '$subject' => 'Subject line of the message.',
436             '$content' => 'Text content of the message.',
437             });
438              
439             B<Response:>
440              
441             {
442             error_message => 'OK',
443             status => 0,
444             time => 1428607810,
445             request => { ... }
446             }
447              
448             =head2 submit_review
449              
450             Record a user-submitted review of a product or other users. e.g., a seller on your site.
451              
452             B<Request:>
453              
454             submit_review('billy_jones_301', {
455             '$content' => 'Text content of submitted review goes here',
456             '$review_title' => 'Title of Review Goes Here',
457             '$item_id' => 'V4C3D5R2Z6',
458             '$reviewed_user_id' => 'billy_jones_301',
459             '$submission_status' => '$success',
460             'rating' => 5,
461             });
462              
463             B<Response:>
464              
465             {
466             error_message => 'OK',
467             status => 0,
468             time => 1428607810,
469             request => { ... }
470             }
471              
472             =head2 unlabel_user
473              
474             Remove a label from a user programatically.
475              
476             B<Request:>
477              
478             unlabel_user('billy_jones_301');
479              
480             B<Response:>
481              
482             204 No Content
483              
484             =head2 update_account
485              
486             Record changes to the user's account information.
487              
488             B<Request:>
489              
490             update_account('billy_jones_301', {
491             '$session_id' => 'gigtleqddo84l8cm15qe4il',
492             '$user_email' => 'bill@gmail.com',
493             '$name' => 'Bill Jones',
494             '$phone' => '1-415-555-6040',
495             '$referrer_user_id' => 'janejane101',
496             '$changed_password' => JSON::true,
497             ...
498             });
499              
500             B<Response:>
501              
502             {
503             error_message => 'OK',
504             status => 0,
505             time => 1428607810,
506             request => { ... }
507             }
508              
509             =head1 BUGS
510              
511             Please report any bugs or feature requests on the bugtracker website
512             https://github.com/aanari/WebService-SiftScience/issues
513              
514             When submitting a bug or request, please include a test-file or a
515             patch to an existing test-file that illustrates the bug or desired
516             feature.
517              
518             =head1 AUTHOR
519              
520             Ali Anari <ali@anari.me>
521              
522             =head1 COPYRIGHT AND LICENSE
523              
524             This software is copyright (c) 2015 by Ali Anari.
525              
526             This is free software; you can redistribute it and/or modify it under
527             the same terms as the Perl 5 programming language system itself.
528              
529             =cut