File Coverage

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


line stmt bran cond sub pod time code
1             package Vero::API;
2 1     1   53583 use version; our $VERSION = version->declare("v0.1.2");
  1         1222  
  1         4  
3 1     1   82 use 5.010;
  1         4  
4 1     1   3 use Carp;
  1         2  
  1         42  
5 1     1   430 use failures qw/vero::api/;
  1         3523  
  1         4  
6 1     1   505 use Mojo::UserAgent;
  1         181833  
  1         8  
7 1     1   29 use Mojo::JSON 'j';
  1         1  
  1         34  
8 1     1   469 use Moo;
  1         7648  
  1         5  
9 1     1   1404 use namespace::autoclean;
  1         8119  
  1         3  
10              
11             has ua => (
12             is => 'rw',
13             builder => 1,
14             );
15             sub _build_ua {
16 4     4   2993 my $ua = Mojo::UserAgent->new;
17 4         31 my $agentid = "Vero::API/$VERSION (Perl)";
18             # $ua->name was deprecated on Mojolicious 4.50
19 4 50       9 $ua->transactor->can('name') ? $ua->transactor->name($agentid) : $ua->name($agentid);
20 4         130 return $ua;
21             }
22              
23             has token => (
24             is => 'rw',
25             builder => 1,
26             );
27              
28             sub _build_token {
29 1     1   1193 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 12376 my ($self, %info) = @_;
34 5         9 my $id = delete $info{id};
35 5         6 my $email = delete $info{email};
36              
37 5 100 100     29 croak 'id or email is required' unless defined $id or defined $email;
38              
39 4 100       31 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       26 unless ($tx->success) {
48 1         4 my ($err, $code) = $tx->error;
49 1         6 failure::vero::api->throw("Vero API returned error: code $code, error $err, data " . j($tx->res->json));
50             }
51 3         10 return $tx->res->json;
52             }
53              
54             sub track_event {
55 5     5 1 21215 my ($self, $event_name, %info) = @_;
56 5         9 my $id = delete $info{id};
57 5         6 my $email = delete $info{email};
58              
59 5 100 100     28 croak 'id or email is required' unless defined $id or defined $email;
60              
61 4 100       40 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       33 unless ($tx->success) {
73 1         4 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         13 return $tx->res->json;
77             }
78              
79             1;
80              
81             __END__