blib/lib/WebService/WAservix.pm

filter: Statementstmt 83.5% 81 / 97 Branchbran 71.8% 23 / 32 Conditioncond 50.0% 25 / 50 MC/DCmcdc 24.1% 7 / 29 Subroutinesub 60.0% 24 / 40 Podpod 96.1% 25 / 26 totaltotal 67.5% 185 / 274 CC 36 SCAR 21.6
CC
36
Cov
72%
request
25.5
new
10.5
verify_webhook
10.1
CRAP 8.7
↑ Summary
1 package WebService::WAservix;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '1.001';
11  
12 1 my $DEFAULT_BASE_URL = 'https://tekservix.com/waservix/api';
13  
14 1 ▶sub new {
Subroutine new: called CC=6 SCAR=23.5
Pod documented
15 1 my ($class, %args) = @_;
16 croak 'WebService::WAservix: api_key is required (Devices -> API key in the WAservix dashboard)'
17 1 ▶ unless defined $args{api_key} && length $args{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"api_key"} and length $args{"api_key"} 0 1 1
Condition33%1/3
defined $args{"api_key"} and length $args{"api_key"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $args{"api_key"} and length $args{"api_key"}
defined $args{"api_key"} length $args{"api_key"}
Truth table33%1/3
A and B
A= defined $args{"api_key"}B= length $args{"api_key"}
ABresult
0X0
100
111
18 1 ▶ my $base = $args{base_url} // $DEFAULT_BASE_URL;
Condition33%1/3
$args{"base_url"} // $DEFAULT_BASE_URL
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{"base_url"} // $DEFAULT_BASE_URL
$args{"base_url"} $DEFAULT_BASE_URL
Truth table33%1/3
A // B
A= $args{"base_url"}B= $DEFAULT_BASE_URL
ABresult
1X1
011
000
19 1 $base =~ s{/+\z}{};
20 return bless {
21 api_key => $args{api_key},
22 base_url => $base,
23 json => JSON::PP->new->utf8->canonical,
24 http => $args{http} // HTTP::Tiny->new(
25 agent => "waservix-perl/$VERSION",
26 1 ▶ timeout => $args{timeout} // 30,
Condition20%1/5
$args{"http"} // "HTTP::Tiny"->new("agent", "waservix-perl/$WebService::WAservix::VERSION", "timeout", $args{"timeout"} // 30, "verify_SSL", 1)
l!l&&r!l&&!r
100
$args{"timeout"} // 30
l!l
00
MC/DC0%0/2
$args{"http"} // "HTTP::Tiny"->new("agent", "waservix-perl/$WebService::WAservix::VERSION", "timeout", $args{"timeout"} // 30, "verify_SSL", 1)
$args{"http"} "HTTP::Tiny"->new("agent", "waservix-perl/$WebService::WAservix::VERSION", "timeout", $args{"timeout"} // 30, "verify_SSL", 1)
MC/DC0%0/1
$args{"timeout"} // 30
$args{"timeout"}
Truth table33%1/3
A // B
A= $args{"http"}B= "HTTP::Tiny"->new("agent", "waservix-perl/$WebService::WAservix::VERSION", "timeout", $args{"timeout"} // 30, "verify_SSL", 1)
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{"timeout"}
Aresult
11
00
27 verify_SSL => 1,
28 ),
29 }, $class;
30 }
31  
32 0 ▶sub base_url { $_[0]{base_url} }
Subroutine base_url: not called CC=1 SCAR=6.9
Pod undocumented
33  
34 # Call any documented endpoint. Returns the decoded `data` field; dies with a
35 # WebService::WAservix::Error on failure (a 503 means the number is reconnecting).
36 13 ▶sub request {
Subroutine request: called CC=16 SCAR=32.4
Pod documented
37 13 my ($self, $method, $path, $body) = @_;
38 13 my %headers = (
39 'Authorization' => "Bearer $self->{api_key}",
40 'Accept' => 'application/json',
41 );
42 13 my %options = (headers => \%headers);
43 13 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 9 4 13
44 9 $headers{'Content-Type'} = 'application/json';
45 9 $options{content} = $self->{json}->encode($body);
46 }
47  
48 13 my $res = $self->{http}->request(uc $method, $self->{base_url} . $path, \%options);
49 13 ▶ my $text = $res->{content} // '';
Condition50%1/2
$res->{"content"} // ""
l!l
130
MC/DC0%0/1
$res->{"content"} // ""
$res->{"content"}
Truth table50%1/2
A
A= $res->{"content"}
Aresult
11
00
50 13 ▶ my $json = eval { length $text ? $self->{json}->decode($text) : undef };
Branch: 1 branch
BranchTrueFalseTotal
length $text ? : 13 0 13
51  
52 13 ▶ my $failed = ref $json eq 'HASH' && exists $json->{success} && !$json->{success};
Condition50%3/6
ref $json eq 'HASH' && exists $json->{'success'} && !$json->{'success'}
!ll&&!rl&&r
0112
ref $json eq "HASH" and exists $json->{"success"}
!ll&&!rl&&r
0013
MC/DC33%1/3
ref $json eq 'HASH' && exists $json->{'success'} && !$json->{'success'}
ref $json eq "HASH" exists $json->{"success"} !$json->{'success'}
Truth table50%2/4
A and B && not(C)
A= ref $json eq "HASH"B= exists $json->{"success"}C= !$json->{'success'}
ABCresult
0XX0
10X0
1100
1111
53 13 ▶ if (!$res->{success} || $failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res->{"success"} or $failed) 2 11 13
Condition100%3/3
not $res->{"success"} or $failed
l!l&&r!l&&!r
1111
MC/DC100%2/2
not $res->{"success"} or $failed
not $res->{"success"} $failed
Truth table100%3/3
not(A) or B
A= not $res->{"success"}B= $failed
ABresult
1X1
011
000
54 2 my $status = $res->{status};
55 die WebService::WAservix::Error->new(
56 status => $status == 599 ? 0 : $status,
57 message => (ref $json eq 'HASH' && defined $json->{message}) ? $json->{message}
58 : $status == 599 ? "request failed: $text"
59 : "HTTP $status",
60 errors => ref $json eq 'HASH' ? $json->{errors} : undef,
61 body => $json // $text,
62 2 ▶ retry_after => $res->{headers}{'retry-after'},
Branch: 4 branches
BranchTrueFalseTotal
$status == 599 ? : 0 2 2
ref $json eq 'HASH' && defined $json->{'message'} ? : 2 0 2
$status == 599 ? : 0 0 0
ref $json eq 'HASH' ? : 2 0 2
Condition33%2/6
ref $json eq "HASH" and defined $json->{"message"}
!ll&&!rl&&r
002
$json // $text
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $json eq "HASH" and defined $json->{"message"}
ref $json eq "HASH" defined $json->{"message"}
MC/DC0%0/2
$json // $text
$json $text
Truth table33%1/3
A and B
A= ref $json eq "HASH"B= defined $json->{"message"}
ABresult
0X0
100
111
Truth table33%1/3
A // B
A= $jsonB= $text
ABresult
1X1
011
000
63 );
64 }
65 11 ▶ return (ref $json eq 'HASH' && exists $json->{data}) ? $json->{data} : $json;
Branch: 1 branch
BranchTrueFalseTotal
ref $json eq 'HASH' && exists $json->{'data'} ? : 11 0 11
Condition33%1/3
ref $json eq "HASH" and exists $json->{"data"}
!ll&&!rl&&r
0011
MC/DC0%0/2
ref $json eq "HASH" and exists $json->{"data"}
ref $json eq "HASH" exists $json->{"data"}
Truth table33%1/3
A and B
A= ref $json eq "HASH"B= exists $json->{"data"}
ABresult
0X0
100
111
66 }
67  
68 # Send any message type. Fields use the API names: text, imageUrl, videoUrl, audioUrl,
69 # documentUrl, fileName, stickerUrl, contact, location, poll, replyTo, mentions, viewOnce.
70 7 ▶sub send_message {
Subroutine send_message: called CC=3 SCAR=11.0
Pod documented
71 7 my ($self, $to, %fields) = @_;
72 7 ▶ croak 'send_message: "to" is required (international number or group JID)' unless defined $to && length $to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to and length $to 1 6 7
Condition66%2/3
defined $to and length $to
!ll&&!rl&&r
016
MC/DC50%1/2
defined $to and length $to
defined $to length $to
Truth table66%2/3
A and B
A= defined $toB= length $to
ABresult
0X0
100
111
73 6 my %body = map { $_ => $fields{$_} } grep { defined $fields{$_} } keys %fields;
74 6 $body{to} = $to;
75 6 return $self->request('POST', '/send-message', \%body);
76 }
77  
78 3 ▶sub send_text { my ($self, $to, $text) = @_; $self->send_message($to, text => $text) }
Subroutine send_text: called CC=1 SCAR=0.0
Pod documented
79  
80 1 ▶sub send_image {
Subroutine send_image: called CC=1 SCAR=0.0
Pod documented
81 1 my ($self, $to, $image_url, %opt) = @_;
82 1 return $self->send_message($to, imageUrl => $image_url, text => $opt{caption});
83 }
84  
85 1 ▶sub send_document {
Subroutine send_document: called CC=1 SCAR=0.0
Pod documented
86 1 my ($self, $to, $document_url, %opt) = @_;
87 1 return $self->send_message($to, documentUrl => $document_url, fileName => $opt{file_name}, text => $opt{caption});
88 }
89  
90 1 ▶sub send_location {
Subroutine send_location: called CC=2 SCAR=7.0
Pod documented
91 1 my ($self, $to, $latitude, $longitude, %opt) = @_;
92 1 my %location = (latitude => $latitude + 0, longitude => $longitude + 0);
93 1 ▶ $location{name} = $opt{name} if defined $opt{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"name"} 1 0 1
94 1 return $self->send_message($to, location => \%location);
95 }
96  
97 1 ▶sub get_message_info { $_[0]->request('GET', '/messages/' . _esc($_[1]) . '/info') }
Subroutine get_message_info: called CC=1 SCAR=0.0
Pod documented
98 1 ▶sub edit_message { $_[0]->request('PUT', '/messages/' . _esc($_[1]), { text => $_[2] }) }
Subroutine edit_message: called CC=1 SCAR=0.0
Pod documented
99 0 ▶sub delete_message { $_[0]->request('DELETE', '/messages/' . _esc($_[1])) }
Subroutine delete_message: not called CC=1 SCAR=6.9
Pod documented
100 0 ▶sub resend_message { $_[0]->request('POST', '/messages/' . _esc($_[1]) . '/resend') }
Subroutine resend_message: not called CC=1 SCAR=6.9
Pod documented
101  
102 1 ▶sub on_whatsapp { $_[0]->request('GET', '/on-whatsapp/' . _esc($_[1])) }
Subroutine on_whatsapp: called CC=1 SCAR=0.0
Pod documented
103 1 ▶sub get_status { $_[0]->request('GET', '/status') }
Subroutine get_status: called CC=1 SCAR=0.0
Pod documented
104 0 ▶sub get_user { $_[0]->request('GET', '/user') }
Subroutine get_user: not called CC=1 SCAR=6.9
Pod documented
105 0 ▶sub list_contacts { $_[0]->request('GET', '/contacts') }
Subroutine list_contacts: not called CC=1 SCAR=6.9
Pod documented
106 0 ▶sub get_contact { $_[0]->request('GET', '/contacts/' . _esc($_[1])) }
Subroutine get_contact: not called CC=1 SCAR=6.9
Pod documented
107 0 ▶sub save_contact { $_[0]->request('PUT', '/contacts', { phone => $_[1], name => $_[2] }) }
Subroutine save_contact: not called CC=1 SCAR=6.9
Pod documented
108 0 ▶sub list_groups { $_[0]->request('GET', '/groups') }
Subroutine list_groups: not called CC=1 SCAR=6.9
Pod documented
109 1 ▶sub get_group_metadata { $_[0]->request('GET', '/groups/' . _esc($_[1]) . '/metadata') }
Subroutine get_group_metadata: called CC=1 SCAR=0.0
Pod documented
110  
111 0 ▶sub list_sessions { $_[0]->request('GET', '/whatsapp-sessions') }
Subroutine list_sessions: not called CC=1 SCAR=6.9
Pod documented
112 0 ▶sub get_session { $_[0]->request('GET', '/whatsapp-sessions/' . _esc($_[1])) }
Subroutine get_session: not called CC=1 SCAR=6.9
Pod documented
113 0 ▶sub create_session { $_[0]->request('POST', '/whatsapp-sessions', $_[1] // {}) }
Subroutine create_session: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[1] // {}
l!l
00
MC/DC0%0/1
$_[1] // {}
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
114  
115 2 ▶sub connect_session {
Subroutine connect_session: called CC=2 SCAR=6.9
Pod documented
116 2 my ($self, $id, %opt) = @_;
117 2 ▶ my $body = defined $opt{phone} ? { method => 'code', phone => $opt{phone} } : { method => 'qr' };
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'phone'} ? : 1 1 2
118 2 return $self->request('POST', '/whatsapp-sessions/' . _esc($id) . '/connect', $body);
119 }
120  
121 0 ▶sub get_session_qr_code { $_[0]->request('GET', '/whatsapp-sessions/' . _esc($_[1]) . '/qrcode') }
Subroutine get_session_qr_code: not called CC=1 SCAR=6.9
Pod documented
122  
123 # Constant-time check of the X-Webhook-Signature header against the number's webhook secret.
124 5 ▶sub verify_webhook {
Subroutine verify_webhook: called CC=10 SCAR=23.2
Pod documented
125 5 my ($signature, $secret) = @_;
126 5 ▶ ($signature, $secret) = @_[1, 2] if defined $signature && $signature eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if defined $signature and $signature eq __PACKAGE__ 1 4 5
Condition100%3/3
defined $signature and $signature eq __PACKAGE__
!ll&&!rl&&r
131
MC/DC100%2/2
defined $signature and $signature eq __PACKAGE__
defined $signature $signature eq __PACKAGE__
Truth table100%3/3
A and B
A= defined $signatureB= $signature eq __PACKAGE__
ABresult
0X0
100
111
127 5 ▶ return 0 unless defined $signature && length $signature && defined $secret && length $secret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $signature and length $signature and defined $secret and length $secret 1 4 5
Condition66%6/9
defined $signature and length $signature and defined $secret and length $secret
!ll&&!rl&&r
104
defined $signature and length $signature and defined $secret
!ll&&!rl&&r
104
defined $signature and length $signature
!ll&&!rl&&r
104
MC/DC25%1/4
defined $signature and length $signature and defined $secret and length $secret
defined $signature length $signature defined $secret length $secret
Truth table40%2/5
A and B and C and D
A= defined $signatureB= length $signatureC= defined $secretD= length $secret
ABCDresult
0XXX0
10XX0
110X0
11100
11111
128 4 ▶ return 0 unless length $signature == length $secret;
Branch: 1 branch
BranchTrueFalseTotal
unless length $signature == length $secret 1 3 4
129 3 my $diff = 0;
130 3 $diff |= ord(substr $signature, $_, 1) ^ ord(substr $secret, $_, 1) for 0 .. length($secret) - 1;
131 3 ▶ return $diff == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$diff == 0 ? : 2 1 3
132 }
133  
134 6 ▶sub _esc {
Subroutine _esc: called CC=2 SCAR=7.0
135 6 ▶ my $s = shift // '';
Condition50%1/2
shift() // ""
l!l
60
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
136 6 $s =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord $1)/ge;
137 6 return $s;
138 }
139  
140 package WebService::WAservix::Error;
141  
142 0 ▶use overload '""' => sub { 'WAservix: ' . $_[0]{message} . ' (HTTP ' . $_[0]{status} . ')' }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
143  
144 2 ▶sub new { my ($class, %args) = @_; return bless {%args}, $class }
Subroutine new: called CC=1 SCAR=0.0
145 1 ▶sub status { $_[0]{status} }
Subroutine status: called CC=1 SCAR=0.0
146 2 ▶sub message { $_[0]{message} }
Subroutine message: called CC=1 SCAR=0.0
147 0 ▶sub errors { $_[0]{errors} }
Subroutine errors: not called CC=1 SCAR=6.9
148 0 ▶sub body { $_[0]{body} }
Subroutine body: not called CC=1 SCAR=6.9
149 0 ▶sub retry_after { $_[0]{retry_after} }
Subroutine retry_after: not called CC=1 SCAR=6.9
150  
151 1 1;
152  
153 __END__
↑ Summary