File Coverage

blib/lib/WWW/GetProve.pm
Criterion Covered Total %
statement 7 53 13.2
branch 0 24 0.0
condition 0 3 0.0
subroutine 3 12 25.0
pod 0 5 0.0
total 10 97 10.3


line stmt bran cond sub pod time code
1             package WWW::GetProve;
2             BEGIN {
3 1     1   1337 $WWW::GetProve::AUTHORITY = 'cpan:GETTY';
4             }
5             {
6             $WWW::GetProve::VERSION = '0.001';
7             }
8             # ABSTRACT: Easy access to the already so easy GetProve API
9              
10              
11 1         8 use MooX qw(
12             +LWP::UserAgent
13             +HTTP::Request::Common
14             +URI
15             +URI::QueryParam
16             +JSON
17             +WWW::GetProve::Verification
18 1     1   1295 );
  1         25552  
19              
20              
21 1     1   240 use Carp qw( croak );
  1         1  
  1         904  
22              
23             our $VERSION ||= '0.000';
24              
25              
26             has api_key => (
27             is => 'ro',
28             required => 1,
29             );
30              
31              
32             has base_uri => (
33             is => 'ro',
34             lazy => 1,
35             builder => 1,
36             );
37              
38 0     0     sub _build_base_uri { 'https://getprove.com/api/v1' }
39              
40              
41             has useragent => (
42             is => 'ro',
43             lazy => 1,
44             builder => 1,
45             );
46              
47             sub _build_useragent {
48 0     0     my ( $self ) = @_;
49 0 0         my $useragent = LWP::UserAgent->new(
50             agent => $self->useragent_agent,
51             $self->has_useragent_timeout ? (timeout => $self->useragent_timeout) : (),
52             );
53 0           my $host_port = URI->new($self->base_uri)->host_port;
54 0           $useragent->credentials(
55             $host_port,"Authorization Required",$self->api_key,"1"
56             );
57 0           return $useragent;
58             }
59              
60              
61             has useragent_agent => (
62             is => 'ro',
63             lazy => 1,
64             builder => 1,
65             );
66              
67 0 0   0     sub _build_useragent_agent { (ref $_[0] ? ref $_[0] : $_[0]).'/'.$VERSION }
68              
69              
70             has useragent_timeout => (
71             is => 'ro',
72             predicate => 'has_useragent_timeout',
73             );
74              
75              
76             has json => (
77             is => 'ro',
78             lazy => 1,
79             builder => 1,
80             );
81              
82             sub _build_json {
83 0     0     my $json = JSON->new;
84 0           $json->allow_nonref;
85 0           return $json;
86             }
87              
88             #############################################################################################################
89              
90             sub BUILDARGS {
91 0     0 0   my ( $class, @args ) = @_;
92 0 0 0       unshift @args, "api_key" if @args % 2 && ref $args[0] ne 'HASH';
93 0           return { @args };
94             }
95              
96             sub make_url {
97 0     0 0   my ( $self, @args ) = @_;
98 0           my $url = join('/',$self->base_uri,@args);
99 0           my $uri = URI->new($url);
100 0           return $uri;
101             }
102              
103             sub create_verification {
104 0     0 0   my $self = shift;
105 0           my %args;
106 0 0         $args{id} = shift unless (scalar @_ % 2);
107 0           %args = %{@_};
  0            
108 0           WWW::GetProve::Verification->new(%args);
109             }
110              
111             sub verify_request {
112 0     0 0   my ( $self, $tel_or_verification, $pin ) = @_;
113 0 0         if (ref $tel_or_verification) {
    0          
114 0           my $id;
115 0 0         if (ref $tel_or_verification eq 'SCALAR') {
    0          
116 0           $id = ${$tel_or_verification};
  0            
117             } elsif ($tel_or_verification->isa('WWW::GetProve::Verification')) {
118 0           $id = $tel_or_verification->id;
119             }
120 0 0         if ($pin) {
121 0           POST(shift->make_url('verify', $id, 'pin'), [ pin => $pin ])
122             } else {
123 0           GET(shift->make_url('verify', $id))
124             }
125             } elsif ($tel_or_verification) {
126 0           POST($self->make_url('verify'), [ tel => $tel_or_verification ])
127             } else {
128 0           GET($self->make_url('verify'))
129             }
130             }
131              
132             sub verify {
133 0     0 0   my ( $self, @args ) = @_;
134 0           my $request = $self->verify_request(@args);
135 0           my $response = $self->useragent->request($request);
136 0 0         die __PACKAGE__." API server says unauthorized access" if $response->code == 401;
137 0           my $data = $self->json->decode($response->content);
138 0 0         if (ref $data eq 'ARRAY') {
139 0           my @verifications = map {
140 0           WWW::GetProve::Verification->new($_);
141 0           } @{$data};
142 0 0         return wantarray ? @verifications : \@verifications;
143             } else {
144 0           return WWW::GetProve::Verification->new($data);
145             }
146             }
147              
148             1;
149              
150              
151             __END__