File Coverage

blib/lib/Vero/API.pm
Criterion Covered Total %
statement 47 47 100.0
branch 17 18 94.4
condition 6 6 100.0
subroutine 12 12 100.0
pod 2 2 100.0
total 84 85 98.8


line stmt bran cond sub pod time code
1             package Vero::API;
2 1     1   604353 use version; our $VERSION = version->declare("v0.1.1");
  1         1834  
  1         5  
3 1     1   96 use 5.010;
  1         5  
  1         29  
4 1     1   5 use Carp;
  1         1  
  1         62  
5 1     1   894 use failures qw/vero::api/;
  1         5060  
  1         7  
6 1     1   943 use Mojo::UserAgent;
  1         473468  
  1         15  
7 1     1   40 use Mojo::JSON 'j';
  1         2  
  1         55  
8 1     1   3029 use Moo;
  1         17406  
  1         9  
9 1     1   3029 use namespace::autoclean;
  1         14240  
  1         9  
10              
11             has ua => (
12             is => 'rw',
13             builder => 1,
14             );
15             sub _build_ua {
16 4     4   4584 my $ua = Mojo::UserAgent->new;
17 4         48 my $agentid = "Vero::API/$VERSION (Perl)";
18             # $ua->name was deprecated on Mojolicious 4.50
19 4 50       99 $ua->transactor->can('name') ? $ua->transactor->name($agentid) : $ua->name($agentid);
20 4         349 return $ua;
21             }
22              
23             has token => (
24             is => 'rw',
25             builder => 1,
26             );
27              
28             sub _build_token {
29 1     1   1509 croak 'A token is required during initialization. Pass one on constructor or override "_build_token" to return one';
30             }
31              
32             sub identify_user {
33 5     5 1 18669 my ($self, %info) = @_;
34 5         13 my $id = delete $info{id};
35 5         11 my $email = delete $info{email};
36              
37 5 100 100     47 croak 'id or email is required' unless defined $id or defined $email;
38              
39 4 100       54 my $tx = $self->ua->post(
    100          
40             'https://api.getvero.com/api/v2/users/track.json',
41             json => {
42             auth_token => $self->token,
43             ($id ? (id => $id) : ()),
44             ($email ? (email => $email) : ()),
45             data => {%info},
46             });
47 4 100       44 unless ($tx->success) {
48 1         8 my ($err, $code) = $tx->error;
49 1         9 failure::vero::api->throw("Vero API returned error: code $code, error $err, data " . j($tx->res->json));
50             }
51 3         21 return $tx->res->json;
52             }
53              
54             sub track_event {
55 5     5 1 35731 my ($self, $event_name, %info) = @_;
56 5         9 my $id = delete $info{id};
57 5         9 my $email = delete $info{email};
58              
59 5 100 100     42 croak 'id or email is required' unless defined $id or defined $email;
60              
61 4 100       59 my $tx = $self->ua->post(
    100          
62             'https://api.getvero.com/api/v2/events/track.json',
63             json => {
64             auth_token => $self->token,
65             'identity' => {
66             ($id ? (id => $id) : ()),
67             ($email ? (email => $email) : ()),
68             },
69             event_name => $event_name,
70             data => {%info},
71             });
72 4 100       48 unless ($tx->success) {
73 1         6 my ($err, $code) = $tx->error;
74 1         8 failure::vero::api->throw("Vero API returned error: code $code, error $err, data " . j($tx->res->json));
75             }
76 3         21 return $tx->res->json;
77             }
78              
79             1;
80              
81             __END__