blib/lib/SMS/Send/Mocean.pm

filter: Statementstmt 68.1% 47 / 69 Branchbran 16.6% 2 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.3% 14 / 15 Podpod 100% 2 / 2 totaltotal 63.1% 65 / 103 CC 10 SCAR 32.2
CC
10
Cov
58%
send_sms
72.0
_required
3.0
BEGIN
1.0
CRAP 24.9
↑ Summary Build »
1 package SMS::Send::Mocean;
2  
3 5 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Carp::Always;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Request::Common qw(POST);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use URL::Encode qw(url_decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use XML::Hash::LX qw(xml2hash);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use base 'SMS::Send::Driver';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 our $VERSION = '0.02';
18  
19 sub new {
20 4 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
21 4 my $args = { @_ };
22  
23 4 _required($args, qw(_api_key _api_secret));
24  
25 1 my $opts = {
26 _endpoint => 'https://rest.moceanapi.com/rest/1/sms',
27 %$args
28 };
29  
30 1 my $self = bless $opts, $class;
31  
32 1 $self->{ua} = LWP::UserAgent->new(
33 agent => __PACKAGE__ . qq| v| . $SMS::Send::Mocean::VERSION,
34 timeout => 10,
35 );
36  
37 1 return $self;
38 }
39  
40 sub send_sms {
41 0 ▶ my $self = shift;
Subroutine send_sms: not called CC=8 SCAR=42.8
Pod documented
42 0 my $args = { @_ };
43  
44 0 _required($args, qw(to text _from));
45  
46 0 my @extra_args = qw(
47 _udh
48 _coding
49 _dlr_mask
50 _dlr_url
51 _schedule
52 _mclass
53 _alt_dcs
54 _charset
55 _validity
56 _resp_format
57 );
58  
59 0 my @extra_params;
60 0 foreach (@extra_args) {
61 push @extra_params, _to_mocean_field_name($_) => $args->{$_}
62 0 ▶ if (defined $args->{$_});
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$_} 0 0 0
63 }
64  
65 my $request = POST($self->{_endpoint}, [
66 'mocean-api-key' => $self->{_api_key},
67 'mocean-api-secret' => $self->{_api_secret},
68 'mocean-to' => $args->{to},
69 'mocean-text' => $args->{text},
70 'mocean-from' => $args->{_from},
71 @extra_params
72 0 ]);
73  
74 0 ▶ my $format = defined $args->{_resp_format} && lc($args->{_resp_format}) eq 'json'
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'_resp_format'} && lc $args->{'_resp_format'} eq 'json' ? : 0 0 0
Condition0%0/3
defined $args->{"_resp_format"} and lc $args->{"_resp_format"} eq "json"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args->{"_resp_format"} and lc $args->{"_resp_format"} eq "json"
defined $args->{"_resp_format"} lc $args->{"_resp_format"} eq "json"
Truth table0%0/3
A and B
A= defined $args->{"_resp_format"}B= lc $args->{"_resp_format"} eq "json"
ABresult
0X0
100
111
75 ? 'json' : 'xml';
76  
77 0 my $response = $self->{ua}->request($request);
78  
79 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
80 0 ▶ my $content = ($format eq 'json')
Branch: 1 branch
BranchTrueFalseTotal
$format eq 'json' ? : 0 0 0
81 ? decode_json($response->decoded_content)
82 : xml2hash $response->decoded_content;
83  
84 0 return $content->{result}->{message};
85 }
86 else {
87 0 my ($error_code, $error_msg) = ('', '');
88  
89 0 ▶ if ($format eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'json') { } 0 0 0
90 0 my $content = decode_json($response->decoded_content);
91 0 $error_code = $content->{status};
92 0 $error_msg = url_decode($content->{err_msg});
93 }
94 else {
95 0 my $content = xml2hash $response->decoded_content;
96 0 $error_code = $content->{result}->{status};
97 0 $error_msg = url_decode($content->{result}->{err_msg});
98 }
99  
100 0 die sprintf "\n%s (error code: %d)", $error_msg, $error_code;
101 }
102 }
103  
104 sub _required {
105 5 ▶ my ($args, @required_args) = @_;
Subroutine _required: called CC=3 SCAR=11.0
106  
107 5 foreach (@required_args) {
108 8 ▶ die "'$_' parameter required" unless $args->{$_}
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{$_} 4 4 8
109 }
110 }
111  
112 sub _to_mocean_field_name {
113 3 ▶ my ($name) = @_;
Subroutine _to_mocean_field_name: called CC=1 SCAR=0.0
114  
115 3 $name =~ tr/_/-/;
116  
117 3 return qq|mocean$name|;
118 }
119  
120  
121 5 1;
122 __END__
123  
124 =encoding utf-8
125  
126 =head1 NAME
127  
128 SMS::Send::Mocean - SMS::Send driver to send messages via Mocean,
129 https://moceanapi.com/.
130  
131 =head1 SYNOPSIS
132  
133 use SMS::Send;
134  
135 my $gateway = SMS::Send->new(
136 'Mocean',
137 '_api_key' => 'foo',
138 '_api_secret' => 'bar'
139 );
140  
141 $gateway->send_sms(
142 to => '+60123456789',
143 ext => 'Hello',
144 _from => 'foobar'
145 );
146  
147 =head1 DESCRIPTION
148  
149 SMS::Send::Mocean is a driver for L<SMS::Send> to send message via Mocean,
150 https://moceanapi.com/.
151  
152 =head1 DEVELOPMENT
153  
154 Source repo at L<https://github.com/kianmeng/send-sms-mocean|https://github.com/kianmeng/sms-send-mocean>.
155  
156 How to contribute? Follow through the L<CONTRIBUTING.md|https://github.com/kianmeng/sms-send-mocean/blob/master/CONTRIBUTING.md> document to setup your development environment.
157  
158 =head1 METHODS
159  
160 =head2 new(_api_key, _api_secret)
161  
162 Construct a new SMS::Send instance.
163  
164 my $gateway = SMS::Send->new(
165 'Mocean',
166 '_api_key' => 'foo',
167 '_api_secret' => 'bar'
168 );
169  
170 =head3 _api_key
171  
172 Compulsory. The API access key used to make request through web service.
173  
174 =head3 _api_secret
175  
176 Compulsory. The API secret key.
177  
178 =head2 send_sms(to, text, _from, [%params])
179  
180 Send the SMS text to a mobile user.
181  
182 # Default parameters with XML response format if the '_resp_format' field
183 # is not defined.
184 $gateway->send_sms(
185 to => '+60123456789',
186 ext => 'Hello',
187 _from => 'foobar',
188 );
189  
190 # With JSON response format.
191 $gateway->send_sms(
192 to => '+60123456789',
193 ext => 'Hello',
194 _from => 'foobar',
195 _resp_format => 'json',
196 );
197  
198 =head3 to
199  
200 Compulsory. The required field needed by SMS::Send. Only accept leading-plus
201 number in the format of "+99 XXX XXXX".
202  
203 =head3 text
204  
205 Compulsory. The required field needed by SMS::Send. The content of the SMS
206 message. Depends on the language of the content, there is a limit of characters
207 that can be sent.
208  
209 =head3 _from
210  
211 Compulsory. The login username of the Mocean API portal.
212  
213 =head3 [%params]
214  
215 Optional. Additional parameters that can be used when sending SMS. Check the
216 Mocean API documentation on the L<available parameters|https://moceanapi.com/docs/#sms-api>.
217 Due to the design constraints of L<SMS::Send::Driver>, all parameters name must
218 start with underscore. For example, '_resp_format'. This driver will convert the
219 parameter name to equivalent format used by Mocean. In this case, '_foo_bar'
220 will be formatted as 'mocean-resp-format'.
221  
222 $gateway->send_sms(
223 to => '+60123456789',
224 ext => 'Hello',
225 _from => 'foobar',
226 _resp_format => 'json',
227 _charset => 'utf-8',
228 );
229  
230 =head1 COPYRIGHT AND LICENSE
231  
232 This software is Copyright (c) 2019 Kian Meng, Ang.
233  
234 This is free software, licensed under:
235  
236 The Artistic License 2.0 (GPL Compatible)
237  
238 =head1 AUTHOR
239  
240 Kian Meng, Ang E<lt>kianmeng@users.noreply.github.comE<gt>
241  
242 =head1 SEE ALSO
243  
244 L<SMS::Driver>
↑ Summary Build »