blib/lib/WebService/SmsGora.pm

filter: Statementstmt 93.3% 98 / 105 Branchbran 82.6% 38 / 46 Conditioncond 55.5% 35 / 63 MC/DCmcdc 20.5% 7 / 34 Subroutinesub 82.3% 28 / 34 Podpod 94.1% 16 / 17 totaltotal 74.2% 222 / 299 CC 48 SCAR 20.2
CC
48
Cov
80%
request
18.6
verify_webhook
11.9
send_sms
8.0
CRAP 7.5
↑ Summary
1 package WebService::SmsGora;
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 Digest::SHA qw(hmac_sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '1.001';
12  
13 1 my $DEFAULT_BASE_URL = 'https://tekservix.com/smsgora/api/v1';
14  
15 2 ▶sub new {
Subroutine new: called CC=6 SCAR=20.5
Pod documented
16 2 my ($class, %args) = @_;
17 croak 'WebService::SmsGora: api_key is required (create one in SMS Gora -> API keys)'
18 2 ▶ unless defined $args{api_key} && length $args{api_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"api_key"} and length $args{"api_key"} 1 1 2
Condition66%2/3
defined $args{"api_key"} and length $args{"api_key"}
!ll&&!rl&&r
101
MC/DC50%1/2
defined $args{"api_key"} and length $args{"api_key"}
defined $args{"api_key"} length $args{"api_key"}
Truth table66%2/3
A and B
A= defined $args{"api_key"}B= length $args{"api_key"}
ABresult
0X0
100
111
19 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
20 1 $base =~ s{/+\z}{};
21 my $self = bless {
22 api_key => $args{api_key},
23 base_url => $base,
24 json => JSON::PP->new->utf8->canonical,
25 http => $args{http} // HTTP::Tiny->new(
26 agent => "smsgora-perl/$VERSION",
27 1 ▶ timeout => $args{timeout} // 30,
Condition20%1/5
$args{"http"} // "HTTP::Tiny"->new("agent", "smsgora-perl/$WebService::SmsGora::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", "smsgora-perl/$WebService::SmsGora::VERSION", "timeout", $args{"timeout"} // 30, "verify_SSL", 1)
$args{"http"} "HTTP::Tiny"->new("agent", "smsgora-perl/$WebService::SmsGora::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", "smsgora-perl/$WebService::SmsGora::VERSION", "timeout", $args{"timeout"} // 30, "verify_SSL", 1)
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{"timeout"}
Aresult
11
00
28 verify_SSL => 1,
29 ),
30 }, $class;
31 1 return $self;
32 }
33  
34 0 ▶sub base_url { $_[0]{base_url} }
Subroutine base_url: not called CC=1 SCAR=6.9
Pod undocumented
35  
36 # Call any documented endpoint. Returns the decoded `data` field; dies with a
37 # WebService::SmsGora::Error on failure.
38 10 ▶sub request {
Subroutine request: called CC=16 SCAR=29.2
Pod documented
39 10 my ($self, $method, $path, $body) = @_;
40 my %headers = (
41 'x-api-key' => $self->{api_key},
42 10 'Accept' => 'application/json',
43 );
44 10 my %options = (headers => \%headers);
45 10 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 4 6 10
46 4 $headers{'Content-Type'} = 'application/json';
47 4 $options{content} = $self->{json}->encode($body);
48 }
49  
50 10 my $res = $self->{http}->request(uc $method, $self->{base_url} . $path, \%options);
51 10 ▶ my $text = $res->{content} // '';
Condition50%1/2
$res->{"content"} // ""
l!l
100
MC/DC0%0/1
$res->{"content"} // ""
$res->{"content"}
Truth table50%1/2
A
A= $res->{"content"}
Aresult
11
00
52 10 ▶ my $json = eval { length $text ? $self->{json}->decode($text) : undef };
Branch: 1 branch
BranchTrueFalseTotal
length $text ? : 10 0 10
53  
54 10 ▶ my $failed = ref $json eq 'HASH' && exists $json->{success} && !$json->{success};
Condition83%5/6
ref $json eq 'HASH' && exists $json->{'success'} && !$json->{'success'}
!ll&&!rl&&r
181
ref $json eq "HASH" and exists $json->{"success"}
!ll&&!rl&&r
109
MC/DC66%2/3
ref $json eq 'HASH' && exists $json->{'success'} && !$json->{'success'}
ref $json eq "HASH" exists $json->{"success"} !$json->{'success'}
Truth table75%3/4
A and B && not(C)
A= ref $json eq "HASH"B= exists $json->{"success"}C= !$json->{'success'}
ABCresult
0XX0
10X0
1100
1111
55 10 ▶ if (!$res->{success} || $failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res->{"success"} or $failed) 2 8 10
Condition66%2/3
not $res->{"success"} or $failed
l!l&&r!l&&!r
208
MC/DC50%1/2
not $res->{"success"} or $failed
not $res->{"success"} $failed
Truth table66%2/3
not(A) or B
A= not $res->{"success"}B= $failed
ABresult
1X1
011
000
56 2 my $status = $res->{status};
57 die WebService::SmsGora::Error->new(
58 status => $status == 599 ? 0 : $status,
59 message => (ref $json eq 'HASH' && defined $json->{message}) ? $json->{message}
60 : $status == 599 ? "request failed: $text"
61 : "HTTP $status",
62 errors => ref $json eq 'HASH' ? $json->{errors} : undef,
63 body => $json // $text,
64 2 ▶ retry_after => $res->{headers}{'retry-after'},
Branch: 4 branches
BranchTrueFalseTotal
$status == 599 ? : 0 2 2
ref $json eq 'HASH' && defined $json->{'message'} ? : 1 1 2
$status == 599 ? : 0 1 1
ref $json eq 'HASH' ? : 1 1 2
Condition66%4/6
ref $json eq "HASH" and defined $json->{"message"}
!ll&&!rl&&r
101
$json // $text
l!l&&r!l&&!r
110
MC/DC50%1/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 table66%2/3
A and B
A= ref $json eq "HASH"B= defined $json->{"message"}
ABresult
0X0
100
111
Truth table66%2/3
A // B
A= $jsonB= $text
ABresult
1X1
011
000
65 );
66 }
67 8 ▶ return (ref $json eq 'HASH' && exists $json->{data}) ? $json->{data} : $json;
Branch: 1 branch
BranchTrueFalseTotal
ref $json eq 'HASH' && exists $json->{'data'} ? : 8 0 8
Condition33%1/3
ref $json eq "HASH" and exists $json->{"data"}
!ll&&!rl&&r
008
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
68 }
69  
70 3 ▶sub send_sms {
Subroutine send_sms: called CC=8 SCAR=20.8
Pod documented
71 3 my ($self, %args) = @_;
72 3 ▶ my @to = ref $args{to} eq 'ARRAY' ? @{ $args{to} } : grep { length } split /\s*,\s*/, ($args{to} // '');
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'to'} eq 'ARRAY' ? : 2 1 3
Condition50%1/2
$args{"to"} // ""
l!l
10
MC/DC0%0/1
$args{"to"} // ""
$args{"to"}
Truth table50%1/2
A
A= $args{"to"}
Aresult
11
00
73 3 ▶ croak 'send_sms: at least one recipient is required' unless @to;
Branch: 1 branch
BranchTrueFalseTotal
unless @to 1 2 3
74 2 ▶ croak 'send_sms: message is required' unless defined $args{message};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"message"} 0 2 2
75  
76 2 my %body = (recipients => \@to, message => $args{message});
77 2 ▶ $body{sim_slot} = $args{sim_slot} + 0 if defined $args{sim_slot};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"sim_slot"} 1 1 2
78 2 ▶ $body{schedule_at} = $args{schedule_at} if defined $args{schedule_at};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"schedule_at"} 1 1 2
79 2 ▶ my $path = defined $args{device_id} ? '/devices/' . _esc($args{device_id}) . '/send-sms' : '/send-sms';
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'device_id'} ? : 1 1 2
80 2 return $self->request('POST', $path, \%body);
81 }
82  
83 1 ▶sub send_bulk_sms {
Subroutine send_bulk_sms: called CC=3 SCAR=11.4
Pod documented
84 1 my ($self, $device_id, %args) = @_;
85 1 ▶ my %body = (messages => $args{messages} // []);
Condition50%1/2
$args{"messages"} // []
l!l
10
MC/DC0%0/1
$args{"messages"} // []
$args{"messages"}
Truth table50%1/2
A
A= $args{"messages"}
Aresult
11
00
86 1 ▶ $body{messageTemplate} = $args{message_template} if defined $args{message_template};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"message_template"} 1 0 1
87 1 return $self->request('POST', '/devices/' . _esc($device_id) . '/send-bulk-sms', \%body);
88 }
89  
90 0 ▶sub get_message { $_[0]->request('GET', '/messages/' . _esc($_[1])) }
Subroutine get_message: not called CC=1 SCAR=6.9
Pod documented
91 1 ▶sub cancel_message { $_[0]->request('POST', '/messages/' . _esc($_[1]) . '/cancel') }
Subroutine cancel_message: called CC=1 SCAR=0.0
Pod documented
92  
93 0 ▶sub list_messages {
Subroutine list_messages: not called CC=1 SCAR=6.9
Pod documented
94 0 my ($self, %filters) = @_;
95 0 return $self->request('GET', '/messages' . _query(%filters));
96 }
97  
98 2 ▶sub list_received_sms {
Subroutine list_received_sms: called CC=2 SCAR=6.9
Pod documented
99 2 my ($self, %filters) = @_;
100 2 my $device_id = delete $filters{device_id};
101 2 ▶ my $path = defined $device_id ? '/devices/' . _esc($device_id) . '/get-received-sms' : '/received-sms';
Branch: 1 branch
BranchTrueFalseTotal
defined $device_id ? : 1 1 2
102 2 return $self->request('GET', $path . _query(%filters));
103 }
104  
105 0 ▶sub get_batch { $_[0]->request('GET', '/batches/' . _esc($_[1])) }
Subroutine get_batch: not called CC=1 SCAR=6.9
Pod documented
106 1 ▶sub list_devices { $_[0]->request('GET', '/devices') }
Subroutine list_devices: called CC=1 SCAR=0.0
Pod documented
107 0 ▶sub get_device { $_[0]->request('GET', '/devices/' . _esc($_[1])) }
Subroutine get_device: not called CC=1 SCAR=6.9
Pod documented
108 1 ▶sub get_account { $_[0]->request('GET', '/account') }
Subroutine get_account: called CC=1 SCAR=0.0
Pod documented
109 0 ▶sub list_webhooks { $_[0]->request('GET', '/webhooks') }
Subroutine list_webhooks: not called CC=1 SCAR=6.9
Pod documented
110  
111 1 ▶sub create_webhook {
Subroutine create_webhook: called CC=3 SCAR=11.7
Pod documented
112 1 my ($self, %args) = @_;
113 1 ▶ croak 'create_webhook: url is required' unless $args{url};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"url"} 0 1 1
114 return $self->request('POST', '/webhooks', {
115 url => $args{url},
116 events => $args{events} // ['sms.received'],
117 device_id => $args{device_id},
118 1 ▶ });
Condition50%1/2
$args{"events"} // ["sms.received"]
l!l
01
MC/DC0%0/1
$args{"events"} // ["sms.received"]
$args{"events"}
Truth table50%1/2
A
A= $args{"events"}
Aresult
11
00
119 }
120  
121 1 ▶sub delete_webhook { $_[0]->request('DELETE', '/webhooks/' . _esc($_[1])) }
Subroutine delete_webhook: called CC=1 SCAR=0.0
Pod documented
122  
123 # Check the X-SmsGora-Signature header (sha256=<hex HMAC-SHA256 of the raw body>).
124 5 ▶sub verify_webhook {
Subroutine verify_webhook: called CC=10 SCAR=24.8
Pod documented
125 5 my ($raw_body, $signature, $secret) = @_;
126 5 ▶ ($raw_body, $signature, $secret) = @_[1 .. 3] if ref $raw_body || (defined $raw_body && $raw_body eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if ref $raw_body or defined $raw_body and $raw_body eq __PACKAGE__ 1 4 5
Condition66%4/6
ref $raw_body or defined $raw_body and $raw_body eq __PACKAGE__
l!l&&r!l&&!r
014
defined $raw_body and $raw_body eq __PACKAGE__
!ll&&!rl&&r
041
MC/DC33%1/3
ref $raw_body or defined $raw_body and $raw_body eq __PACKAGE__
ref $raw_body defined $raw_body $raw_body eq __PACKAGE__
Truth table50%2/4
A or B and C
A= ref $raw_bodyB= defined $raw_bodyC= $raw_body eq __PACKAGE__
ABCresult
1XX1
0111
00X0
0100
127 5 ▶ return 0 unless defined $raw_body && length $raw_body && defined $signature && length $signature
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw_body and length $raw_body and defined $signature and length $signature and defined $secret and length $secret 1 4 5
Condition60%9/15
defined $raw_body and length $raw_body and defined $signature and length $signature and defined $secret and length $secret
!ll&&!rl&&r
104
defined $raw_body and length $raw_body and defined $signature and length $signature and defined $secret
!ll&&!rl&&r
104
defined $raw_body and length $raw_body and defined $signature and length $signature
!ll&&!rl&&r
104
defined $raw_body and length $raw_body and defined $signature
!ll&&!rl&&r
014
defined $raw_body and length $raw_body
!ll&&!rl&&r
005
MC/DC16%1/6
defined $raw_body and length $raw_body and defined $signature and length $signature and defined $secret and length $secret
defined $raw_body length $raw_body defined $signature length $signature defined $secret length $secret
Truth table28%2/7
A and B and C and D and E and F
A= defined $raw_bodyB= length $raw_bodyC= defined $signatureD= length $signatureE= defined $secretF= length $secret
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
128 && defined $secret && length $secret;
129 4 my $expected = 'sha256=' . hmac_sha256_hex($raw_body, $secret);
130 4 return _secure_eq($expected, $signature);
131 }
132  
133 4 ▶sub _secure_eq {
Subroutine _secure_eq: called CC=4 SCAR=13.9
134 4 my ($a, $b) = @_;
135 4 ▶ return 0 unless length $a == length $b;
Branch: 1 branch
BranchTrueFalseTotal
unless length $a == length $b 0 4 4
136 4 my $diff = 0;
137 4 $diff |= ord(substr $a, $_, 1) ^ ord(substr $b, $_, 1) for 0 .. length($a) - 1;
138 4 ▶ return $diff == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$diff == 0 ? : 2 2 4
139 }
140  
141 9 ▶sub _esc {
Subroutine _esc: called CC=2 SCAR=7.0
142 9 ▶ my $s = shift // '';
Condition50%1/2
shift() // ""
l!l
90
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
143 9 $s =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord $1)/ge;
144 9 return $s;
145 }
146  
147 2 ▶sub _query {
Subroutine _query: called CC=3 SCAR=11.2
148 2 my %params = @_;
149 2 my @pairs = map { _esc($_) . '=' . _esc($params{$_}) }
150 2 ▶ grep { defined $params{$_} && length $params{$_} } sort keys %params;
Condition33%1/3
defined $params{$_} && length $params{$_}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $params{$_} && length $params{$_}
defined $params{$_} length $params{$_}
Truth table33%1/3
A && B
A= defined $params{$_}B= length $params{$_}
ABresult
0X0
100
111
151 2 ▶ return @pairs ? '?' . join('&', @pairs) : '';
Branch: 1 branch
BranchTrueFalseTotal
@pairs ? : 1 1 2
152 }
153  
154 package WebService::SmsGora::Error;
155  
156 1 ▶use overload '""' => sub { 'SmsGora: ' . $_[0]{message} . ' (HTTP ' . $_[0]{status} . ')' }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
157  
158 2 ▶sub new { my ($class, %args) = @_; return bless {%args}, $class }
Subroutine new: called CC=1 SCAR=0.0
159 1 ▶sub status { $_[0]{status} }
Subroutine status: called CC=1 SCAR=0.0
160 2 ▶sub message { $_[0]{message} }
Subroutine message: called CC=1 SCAR=0.0
161 1 ▶sub errors { $_[0]{errors} }
Subroutine errors: called CC=1 SCAR=0.0
162 1 ▶sub body { $_[0]{body} }
Subroutine body: called CC=1 SCAR=0.0
163 1 ▶sub retry_after { $_[0]{retry_after} }
Subroutine retry_after: called CC=1 SCAR=0.0
164  
165 1 1;
166  
167 __END__
↑ Summary