File Coverage

blib/lib/WWW/OneAll.pm
Criterion Covered Total %
statement 18 55 32.7
branch 0 14 0.0
condition 0 8 0.0
subroutine 6 12 50.0
pod 5 5 100.0
total 29 94 30.8


line stmt bran cond sub pod time code
1             package WWW::OneAll;
2              
3 1     1   14333 use strict;
  1         2  
  1         26  
4 1     1   3 use warnings;
  1         1  
  1         24  
5 1     1   3 use Carp qw/croak/;
  1         1  
  1         56  
6 1     1   488 use Mojo::UserAgent;
  1         234732  
  1         9  
7 1     1   36 use Mojo::Util qw(b64_encode);
  1         2  
  1         52  
8              
9             our $VERSION = '0.02';
10              
11 1     1   4 use vars qw/$errstr/;
  1         1  
  1         387  
12 0     0 1   sub errstr { return $errstr }
13              
14             sub new { ## no critic (ArgUnpacking)
15 0     0 1   my $class = shift;
16 0 0         my %args = @_ % 2 ? %{$_[0]} : @_;
  0            
17              
18 0           for (qw/subdomain public_key private_key/) {
19 0 0         $args{$_} || croak "Param $_ is required.";
20             }
21              
22 0   0       $args{endpoint} ||= "https://" . $args{subdomain} . ".api.oneall.com";
23 0   0       $args{timeout} ||= 60; # for ua timeout
24              
25 0           return bless \%args, $class;
26             }
27              
28             sub __ua {
29 0     0     my $self = shift;
30              
31 0 0         return $self->{ua} if exists $self->{ua};
32              
33 0           my $ua = Mojo::UserAgent->new;
34 0           $ua->max_redirects(3);
35 0           $ua->inactivity_timeout($self->{timeout});
36 0           $ua->proxy->detect; # env proxy
37             # $ua->cookie_jar(0);
38 0           $ua->max_connections(100);
39 0           $self->{ua} = $ua;
40              
41 0           return $ua;
42             }
43              
44             sub connections {
45 0     0 1   return (shift)->request('GET', "/connections");
46             }
47              
48             sub connection {
49 0     0 1   my ($self, $connection_token) = @_;
50              
51 0           return $self->request('GET', "/connection/$connection_token");
52             }
53              
54             sub request {
55 0     0 1   my ($self, $method, $url, %params) = @_;
56              
57 0           $errstr = ''; # reset
58              
59 0           my $ua = $self->__ua;
60 0           my $header = {Authorization => 'Basic ' . b64_encode($self->{public_key} . ':' . $self->{private_key}, '')};
61 0 0         $header->{'Content-Type'} = 'application/json' if %params;
62 0 0         my @extra = %params ? (json => \%params) : ();
63 0           my $tx = $ua->build_tx($method => $self->{endpoint} . $url . '.json' => $header => @extra);
64 0           $tx->req->headers->accept('application/json');
65              
66 0           $tx = $ua->start($tx);
67 0 0 0       if ($tx->res->headers->content_type and $tx->res->headers->content_type =~ 'application/json') {
68 0           return $tx->res->json;
69             }
70 0 0         if (!$tx->success) {
71 0           $errstr = "Failed to fetch $url: " . $tx->error->{message};
72 0           return;
73             }
74              
75 0           $errstr = "Unknown Response.";
76 0           return;
77             }
78              
79             1;
80             __END__