API/CMTelecom.pm

filter: Statementstmt 76.7% 56 / 73 Branchbran 64.2% 18 / 28 Conditioncond 44.4% 4 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 84.6% 11 / 13 Podpod 100% 5 / 5 totaltotal 71.6% 96 / 134 SCAR 33.3
CC
19
Cov
71%
validate_number
20.0
send
13.8
number_details
6.0
CRAP 27.9
↑ Summary Send/CMTelecom.pm »
1 package SMS::API::CMTelecom;
2  
3 5 ▶use 5.006;
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 LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 SMS::API::CMTelecom - SMS API for cmtelecom.com
12  
13 =head1 VERSION
14  
15 Version 0.01
16  
17 =cut
18  
19 5 our $VERSION = '0.03';
20  
21  
22 =head1 SYNOPSIS
23  
24 use SMS::API::CMTelecom;
25 my $sms = SMS::API::CMTelecom->new(
26 product_token => '00000000-0000-0000-0000-000000000000',
27 );
28  
29 $sms->send(
30 sender => '00480000111111111',
31 message => 'please call me!',
32 recipients => '00490000000000000',
33 reference => 293854,
34 );
35  
36 # bulk send to many recipients:
37 $sms->send(
38 sender => '00480000111111111',
39 message => 'please call me!',
40 recipients => ['00490000000000000', '00480000000000', '004300021651202'],
41 reference => 293854,
42 );
43  
44 my $number = '00480000111111111';
45 if ($sms->validate_number($number)) {
46 print "$number is a valid phone number.\n"
47 } else {
48 print "$number is no valid phone number.\n"
49 }
50  
51 my $number_details = $sms->number_details($number);
52 print "$number was ported.\n" if $number_details->{ported};
53  
54  
55 =head1 METHODS
56  
57 =head2 new(%options)
58  
59 Instantiate and initialise object with the following options:
60  
61 =over 4
62  
63 =item C<< product_token => $product_token >>
64  
65 The product token is required to authenticate with the CM Telecom API.
66  
67 =item C<< sender => $sender >>
68  
69 Optional. SMS sender number.
70  
71 =back
72  
73 =cut
74  
75 sub new {
76 9 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
77 9 my %params = @_;
78 9 ▶ die $class.'->new requires product_token parameter' if not exists $params{product_token};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"product_token"} 1 8 9
79 8 my $self = \%params;
80 8 bless $self, $class;
81  
82 8 $self->{_ua} = LWP::UserAgent::->new();
83 8 $self->{_ua}->agent('SMS::API::CMTelecom/'.$VERSION);
84 8 ▶ if ($self->{_ua}->can('ssl_opts')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ua"}->can("ssl_opts")) 8 0 8
85 8 $self->{_ua}->ssl_opts( verify_hostname => 0, );
86 }
87  
88 8 return $self;
89 }
90  
91 =head2 send
92  
93 =over 4
94  
95 =item C<< message => $message >>
96  
97 Mandatory. Message text to send.
98  
99 =item C<< recipients => $recipients >>
100  
101 Mandatory. May be a scalar containing one phone number or an array reference
102 holding multiple scalars containing one phone number each.
103  
104 =item C<< sender => $sender >>
105  
106 Optional if already given as parameter to C<new>. Can also be set globally when construction the object with C<new()>.
107  
108 =back
109  
110 If sending fails, C<undef> is returned, otherwise a hashref with some status information:
111  
112 {
113 messages => [
114 {
115 messageDetails => undef,
116 parts => 1,
117 reference => 51314,
118 status => "Accepted",
119 to => "0049123456784510",
120 },
121 ],
122 }
123  
124 You can retrieve the error message via
125  
126 my $msg = $sms->error_message();
127  
128 =cut
129  
130 sub send {
131 8 ▶ my $self = shift;
Subroutine send: called CC=13 SCAR=26.3
Pod documented
132 8 my %params = @_;
133  
134 8 $self->_reset_error_message();
135  
136 8 ▶ my @recipients = ref $params{recipients} eq 'ARRAY' ? @{ $params{recipients} } : ($params{recipients} || ());
Branch: 1 branch
BranchTrueFalseTotal
ref $params{'recipients'} eq 'ARRAY' ? : 3 5 8
Condition66%2/3
$params{"recipients"} or ()
l!l&&r!l&&!r
401
MC/DC50%1/2
$params{"recipients"} or ()
$params{"recipients"} ()
Truth table66%2/3
A or B
A= $params{"recipients"}B= ()
ABresult
1X1
011
000
137  
138 8 ▶ return $self->_set_error_message(ref($self).'->send requires at least one recipient number') if !@recipients;
Branch: 1 branch
BranchTrueFalseTotal
unless @recipients 2 6 8
139 6 for my $recipient (@recipients) {
140 9 ▶ return $self->_set_error_message('recipient may not be undefined') if !defined $recipient;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $recipient 1 8 9
141 8 ▶ return $self->_set_error_message('recipient must be a telephone number') if ref $recipient;
Branch: 1 branch
BranchTrueFalseTotal
if ref $recipient 1 7 8
142 7 ▶ return $self->_set_error_message('recipient may not be an empty string') if $recipient eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $recipient eq "" 1 6 7
143 }
144  
145 3 ▶ my $sender = $params{sender} // $self->{sender};
Condition66%2/3
$params{"sender"} // $self->{"sender"}
l!l&&r!l&&!r
102
MC/DC50%1/2
$params{"sender"} // $self->{"sender"}
$params{"sender"} $self->{"sender"}
Truth table66%2/3
A // B
A= $params{"sender"}B= $self->{"sender"}
ABresult
1X1
011
000
146 3 ▶ return $self->_set_error_message(ref($self).'->send requires a sender number') if !defined $sender;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sender 2 1 3
147  
148 my $payload = {
149 messages => {
150 authentication => {
151 producttoken => $self->{product_token},
152 },
153 msg => [
154 {
155 from => $sender,
156 1 ▶ to => [ map { +{ number => $self->_clean_number($_) } } @recipients ],
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'reference'} ? : 1 0 1
157 body => {
158 type => 'AUTO',
159 content => $params{message},
160 },
161 1 exists $params{reference} ? (reference => $params{reference}) : (),
162 },
163 ],
164 },
165 };
166  
167 1 my $req = HTTP::Request->new(
168 POST => 'https://gw.cmtelecom.com/v1.0/message',
169 ['Content-Type' => 'application/json'],
170 encode_json $payload,
171 );
172 1 my $res = $self->{_ua}->request( $req );
173  
174 1 ▶ if ($res->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 200) 0 1 1
175 0 my $result = decode_json $res->content();
176 return {
177 messages => $result->{messages},
178 0 };
179 }
180  
181 1 my $result = eval { decode_json $res->content() };
182 1 ▶ return $self->_set_error_message($result->{details}) if ref $result eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result eq "HASH" 1 0 1
183  
184 0 return $self->_set_error_message('HTTP request returned with status '.$res->code);
185 }
186  
187 sub _clean_number {
188 1 ▶ my ($self, $number) = @_;
Subroutine _clean_number: called CC=1 SCAR=0.0
189  
190 # strip all non-number chars
191 1 $number =~ s/\D//g;
192  
193 1 return $number;
194 }
195  
196 sub _set_error_message {
197 8 ▶ my ($self, $message) = @_;
Subroutine _set_error_message: called CC=1 SCAR=0.0
198 8 $self->{error_message} = $message;
199 8 return;
200 }
201  
202 sub _reset_error_message {
203 8 ▶ my ($self, $message) = @_;
Subroutine _reset_error_message: called CC=1 SCAR=0.0
204 8 $self->{error_message} = undef;
205 8 return;
206 }
207  
208 =head2 validate_number $number
209  
210 Checks if the given phone number is valid and provides additional information,
211 e.g. how the number should be formatted. Returns 1 if the number is valid, a
212 false value otherwise.
213  
214 =cut
215  
216 sub validate_number {
217 0 ▶ my ($self, $number) = @_;
Subroutine validate_number: not called CC=4 SCAR=30.0
Pod documented
218  
219 my $req = HTTP::Request->new(
220 POST => 'https://api.cmtelecom.com/v1.1/numbervalidation',
221 [
222 'Content-Type' => 'application/json',
223 'X-CM-PRODUCTTOKEN' => $self->{product_token},
224 0 ],
225 encode_json { phonenumber => $number },
226 );
227  
228 0 my $res = $self->{_ua}->request( $req );
229 0 ▶ if ($res->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 200) 0 0 0
230 0 my $result = decode_json $res->content();
231 0 ▶ return 1 if JSON::is_bool($result->{valid_number}) and $result->{valid_number};
Branch: 1 branch
BranchTrueFalseTotal
if &JSON::is_bool($result->{"valid_number"}) and $result->{"valid_number"} 0 0 0
Condition0%0/3
&JSON::is_bool($result->{"valid_number"}) and $result->{"valid_number"}
!ll&&!rl&&r
000
MC/DC0%0/2
&JSON::is_bool($result->{"valid_number"}) and $result->{"valid_number"}
&JSON::is_bool($result->{"valid_number"}) $result->{"valid_number"}
Truth table0%0/3
A and B
A= &JSON::is_bool($result->{"valid_number"})B= $result->{"valid_number"}
ABresult
0X0
100
111
232 0 return 0;
233 }
234 0 return $self->_set_error_message('HTTP request returned with status '.$res->code);
235 }
236  
237 =head2 number_details $number
238  
239 Returns carrier, country, timezone and number type information about the given number.
240  
241 =cut
242  
243 sub number_details {
244 0 ▶ my ($self, $number) = @_;
Subroutine number_details: not called CC=2 SCAR=17.9
Pod documented
245  
246 my $req = HTTP::Request->new(
247 POST => 'https://api.cmtelecom.com/v1.1/numbervalidation',
248 [
249 'Content-Type' => 'application/json',
250 'X-CM-PRODUCTTOKEN' => $self->{product_token},
251 0 ],
252 encode_json { phonenumber => $number },
253 );
254  
255 0 my $res = $self->{_ua}->request( $req );
256  
257 0 ▶ if ($res->code == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 200) 0 0 0
258 0 return decode_json $res->content();
259 }
260 0 return $self->_set_error_message('HTTP request returned with status '.$res->code);
261 }
262  
263 =head2 error_message
264  
265 Returns the last set error message.
266  
267 =cut
268  
269 sub error_message {
270 7 ▶ return shift()->{error_message};
Subroutine error_message: called CC=1 SCAR=0.0
Pod documented
271 }
272  
273  
274 =head1 AUTHOR
275  
276 Dominic Sonntag, C<< <dominic at s5g.de> >>
277  
278 =head1 BUGS AND SUPPORT
279  
280 Please report any bugs or feature requests on Github: L<https://github.com/sonntagd/SMS-API-CMTelecom/issues>
281  
282  
283 =head1 LICENSE AND COPYRIGHT
284  
285 Copyright 2017 Dominic Sonntag.
286  
287 This program is free software; you can redistribute it and/or modify it
288 under the terms of the the Artistic License (2.0). You may obtain a
289 copy of the full license at:
290  
291 L<http://www.perlfoundation.org/artistic_license_2_0>
292  
293 =cut
294  
295 5 1; # End of SMS::API::CMTelecom
↑ Summary Send/CMTelecom.pm »