blib/lib/Net/MBlox.pm

filter: Statementstmt 45.3% 34 / 75 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 61.5% 8 / 13 Podpod 0.0% 0 / 3 totaltotal 35.9% 42 / 117 CC 11 SCAR 38.9
CC
11
Cov
35%
query
110.0
get_token
6.0
_build_ua
2.0
CRAP 48.9
↑ Summary
1 package Net::MBlox;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.004'; # VERSION
6  
7 # ABSTRACT: link to the MBlox api for sending SMS
8  
9 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use WWW::Mechanize::GZip;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 has 'consumer_key' => ( is => 'rw', predicate => 1, required => 1);
17 1 has 'consumer_secret' => ( is => 'rw', predicate => 1, required => 1);
18 1 has 'app_id' => ( is => 'rw', predicate => 1, required => 1);
19 1 has 'access_token' => ( is => 'rw', predicate => 1, clearer => 1);
20 0 ▶has 'api_url' => (is => 'ro', default => sub { 'https://api.mblox.com/v1/apps/' });
Subroutine __ANON__: not called CC=1 SCAR=1.2
21  
22 1 has 'ua' => (
23 is => 'lazy',
24 );
25  
26 0 ▶sub _build_ua {
Subroutine _build_ua: not called CC=1 SCAR=6.9
27 0 my $self = shift;
28 0 return WWW::Mechanize::GZip->new(
29 agent => "perl-net-mblox",
30 cookie_jar => {},
31 stack_depth => 1,
32 autocheck => 0,
33 keep_alive => 4,
34 timeout => 60,
35 );
36 }
37  
38 #on creation do oauth, update when expired.
39 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
40 0 my $self = shift;
41 0 $self->get_token;
42 }
43  
44 0 ▶sub get_token {
Subroutine get_token: not called CC=2 SCAR=17.9
Pod undocumented
45 0 my $self = shift;
46 0 my $ua = $self->ua;
47 0 $ua->default_header('Content-Type', "application/x-www-form-urlencoded");
48 0 my $auth_basic = $self->consumer_key . ':' . $self->consumer_secret;
49 0 $ua->default_header('Authorization', 'Basic ' . encode_base64($auth_basic));
50 0 my $res = $ua->request(POST 'https://api.mblox.com/oauthv2/accesstoken',
51 [grant_type => 'client_credentials']);
52  
53 0 ▶ if($res->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 200) 0 0 0
54 0 my $json = decode_json($res->content);
55 0 $self->access_token($json->{'access_token'});
56 }
57 }
58  
59 {
60 1 my $retries = 0;
61 0 ▶ sub query {
Subroutine query: not called CC=10 SCAR=47.0
Pod undocumented
62 0 my $self = shift;
63  
64 0 my @args = @_;
65  
66 0 ▶ if (@args == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1) { } 0 0 0
elsif (@args > 1 and not grep({$args[0] eq $_;} 'GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE')) { } 0 0 0
67 0 unshift @args, 'GET'; # method by default
68 0 ▶ } elsif (@args > 1 and not (grep { $args[0] eq $_ } ('GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE')) ) {
Condition0%0/3
@args > 1 and not grep({$args[0] eq $_;} "GET", "POST", "PUT", "PATCH", "HEAD", "DELETE")
!ll&&!rl&&r
000
MC/DC0%0/2
@args > 1 and not grep({$args[0] eq $_;} "GET", "POST", "PUT", "PATCH", "HEAD", "DELETE")
@args > 1 not grep({$args[0] eq $_;} "GET", "POST", "PUT", "PATCH", "HEAD", "DELETE")
Truth table0%0/3
A and not(B)
A= @args > 1B= not grep({$args[0] eq $_;} "GET", "POST", "PUT", "PATCH", "HEAD", "DELETE")
ABresult
0X0
100
111
69 0 unshift @args, 'POST'; # if POST content
70 }
71 0 my $request_method = shift @args;
72 0 my $url = shift @args;
73 0 ▶ $url = $self->api_url . $self->app_id . '/' . $url unless $url =~ /^https\:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /^https\:/ 0 0 0
74  
75 0 my $data = shift @args;
76 0 my $ua = $self->ua;
77  
78 ## always go with login:pass or access_token (for private repos)
79 0 ▶ unless ($self->has_access_token) { $self->get_token }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_access_token) 0 0 0
80  
81 0 $ua->default_header('Content-Type', "application/json");
82 0 $ua->default_header('Authorization', "Bearer " . $self->access_token);
83  
84 0 my $req = HTTP::Request->new( $request_method, $url );
85  
86 0 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 0 0 0
87 0 my $json = encode_json($data);
88 0 $req->content($json);
89 }
90  
91 0 $req->header( 'Content-Length' => length $req->content );
92 0 my $res = $ua->request($req);
93  
94 #if denied, re-get token, once, or fail permanently.
95 0 ▶ if($res->code == 401 && $retries++ < 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 401 and $retries++ < 10) 0 0 0
Condition0%0/3
$res->code == 401 and $retries++ < 10
!ll&&!rl&&r
000
MC/DC0%0/2
$res->code == 401 and $retries++ < 10
$res->code == 401 $retries++ < 10
Truth table0%0/3
A and B
A= $res->code == 401B= $retries++ < 10
ABresult
0X0
100
111
96 #invalidate token
97 0 $self->clear_access_token;
98 0 $res = $self->query(@_);
99 }
100 #warn $res->code;
101 #warn $res->as_string;
102  
103 0 ▶ if($res->code == 200) { $retries = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 200) 0 0 0
104 0 $res;
105 }
106 }
107  
108  
109 1 1;
110  
111 __END__
↑ Summary