blib/lib/WWW/OneAll.pm

filter: Statementstmt 33.3% 20 / 60 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 6 / 12 Podpod 100% 5 / 5 totaltotal 29.8% 31 / 104 CC 13 SCAR 33.3
CC
13
Cov
24%
request
56.0
new
42.0
__ua
6.0
CRAP 27.8
↑ Summary
1 package WWW::OneAll;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Mojo::Util qw(b64_encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.03';
10  
11 2 ▶use vars qw/$errstr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶sub errstr { return $errstr }
Subroutine errstr: not called CC=1 SCAR=6.9
Pod documented
13  
14 0 ▶sub new { ## no critic (ArgUnpacking)
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
15 0 my $class = shift;
16 0 ▶ my %args = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
17  
18 0 for (qw/subdomain public_key private_key/) {
19 0 ▶ $args{$_} || croak "Param $_ is required.";
Branch: 1 branch
BranchTrueFalseTotal
$args{$_} or croak("Param $_ is required.") 0 0 0
20 }
21  
22 0 ▶ $args{endpoint} ||= "https://" . $args{subdomain} . ".api.oneall.com";
Condition0%0/2
$args{"endpoint"} ||= 'https://' . $args{"subdomain"} . '.api.oneall.com'
l!l
00
MC/DC0%0/1
$args{"endpoint"} ||= 'https://' . $args{"subdomain"} . '.api.oneall.com'
$args{"endpoint"}
Truth table0%0/2
A
A= $args{"endpoint"}
Aresult
11
00
23 0 ▶ $args{timeout} ||= 60; # for ua timeout
Condition0%0/2
$args{"timeout"} ||= 60
l!l
00
MC/DC0%0/1
$args{"timeout"} ||= 60
$args{"timeout"}
Truth table0%0/2
A
A= $args{"timeout"}
Aresult
11
00
24  
25 0 return bless \%args, $class;
26 }
27  
28 0 ▶sub __ua {
Subroutine __ua: not called CC=2 SCAR=17.9
29 0 my $self = shift;
30  
31 0 ▶ return $self->{ua} if exists $self->{ua};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"ua"} 0 0 0
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 0 ▶sub connections {
Subroutine connections: not called CC=1 SCAR=6.9
Pod documented
45 0 return (shift)->request('GET', "/connections");
46 }
47  
48 0 ▶sub connection {
Subroutine connection: not called CC=1 SCAR=6.9
Pod documented
49 0 my ($self, $connection_token) = @_;
50  
51 0 return $self->request('GET', "/connection/$connection_token");
52 }
53  
54 0 ▶sub request {
Subroutine request: not called CC=7 SCAR=40.3
Pod documented
55 0 my ($self, $method, $url, %params) = @_;
56  
57 0 $errstr = ''; # reset
58  
59 # Format the query params if exist
60 0 my $ref_url_params = delete $params{query_params};
61 0 my $query_params = "?";
62 0 ▶ $query_params = $query_params . join("&", @$ref_url_params) if $ref_url_params;
Branch: 1 branch
BranchTrueFalseTotal
if $ref_url_params 0 0 0
63  
64 0 my $ua = $self->__ua;
65 0 my $header = {Authorization => 'Basic ' . b64_encode($self->{public_key} . ':' . $self->{private_key}, '')};
66 0 ▶ $header->{'Content-Type'} = 'application/json' if %params;
Branch: 1 branch
BranchTrueFalseTotal
if %params 0 0 0
67 0 ▶ my @extra = %params ? (json => \%params) : ();
Branch: 1 branch
BranchTrueFalseTotal
%params ? : 0 0 0
68 0 my $tx = $ua->build_tx($method => $self->{endpoint} . $url . '.json' . $query_params => $header => @extra);
69 0 $tx->req->headers->accept('application/json');
70  
71 0 $tx = $ua->start($tx);
72 0 ▶ if ($tx->res->headers->content_type and $tx->res->headers->content_type =~ 'application/json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->res->headers->content_type and $tx->res->headers->content_type =~ m[application/json]) 0 0 0
Condition0%0/3
$tx->res->headers->content_type and $tx->res->headers->content_type =~ m[application/json]
!ll&&!rl&&r
000
MC/DC0%0/2
$tx->res->headers->content_type and $tx->res->headers->content_type =~ m[application/json]
$tx->res->headers->content_type $tx->res->headers->content_type =~ m[application/json]
Truth table0%0/3
A and B
A= $tx->res->headers->content_typeB= $tx->res->headers->content_type =~ m[application/json]
ABresult
0X0
100
111
73 0 return $tx->res->json;
74 }
75 0 ▶ if (!$tx->success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tx->success) 0 0 0
76 0 $errstr = "Failed to fetch $url: " . $tx->error->{message};
77 0 return;
78 }
79  
80 0 $errstr = "Unknown Response.";
81 0 return;
82 }
83  
84 2 1;
85 __END__
↑ Summary