blib/lib/SMS/Send/UK/123txt.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 14 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 85 CC 8 SCAR 22.3
CC
8
Cov
0%
clean_number
20.0
new
12.0
send_sms
12.0
CRAP 9.3
1 package SMS::Send::UK::123txt;
2  
3 use warnings;
4 use strict;
5 use Carp;
6  
7 =head1 NAME
8  
9 SMS::Send::UK::123txt
10  
11 =head1 VERSION
12  
13 Version 0.0.2
14  
15 =cut
16  
17 our $VERSION = '0.0.2';
18  
19 =head1 SYNOPSIS
20  
21 # Instantiate a sender object
22 my $sender = SMS::Send->new(
23 'UK::123txt',
24 _login => 'username',
25 _password => 'password',
26 _from => 'source_number',
27 );
28  
29 # Send a message
30 my $sent = $sender->send_sms(
31 text => 'This is my message',
32 to => '0000000000' # In the format: 447312345679
33 );
34  
35 =head1 DESCRIPTION
36  
37 L<SMS::Send::UK::123txt> is a L<SMS::Send> driver that provides
38 SMS message sending via the 123-txt service
39  
40 =head2 CAVEATS
41  
42 You need a 123-txt account in order to use this driver
43  
44 =head1 METHODS
45  
46 =cut
47  
48 use base 'SMS::Send::Driver';
49 use LWP::UserAgent();
50 use JSON qw(encode_json);
51 use URI::Escape qw(uri_escape);
52 use Scalar::Util qw( looks_like_number );
53  
54 =head2 new
55  
56 # Instantiate a sender object
57 my $sender = SMS::Send->new(
58 'UK::123txt',
59 _login => 'username',
60 _password => 'password',
61 _from => 'source_number',
62 );
63  
64 The C<new> constructor accepts three parameters, all of which are required:
65  
66 The return value is a C<SMS::Send::UK::123txt> object.
67  
68 =over 4
69  
70 =item 'UK::123txt'
71  
72 The parameter identifying the driver name
73  
74 =item _login
75  
76 The C<_login> parameter as supplied by 123-txt
77  
78 =item _password
79  
80 The C<_password> parameter as supplied by 123-txt
81  
82 =item _from
83  
84 The C<_from> parameter as specified in your 123-txt
85 account as a source number or source id
86  
87 =back
88  
89 =cut
90  
91 sub new {
92 my $class = shift;
93 my %params = @_;
94  
95 # Ensure we've been passed the parameters we're expecting
96 my @expected = ( '_login', '_password', '_from' );
97 foreach my $expect(@expected) {
98 if (!exists $params{$expect}) {
99 croak join(', ', @expected) . ' parameters must be supplied';
100 }
101 }
102  
103 # Instantiate the object
104 my $self = bless {
105 user => $params{'_login'},
106 pass => $params{'_password'},
107 from => looks_like_number($params{'_from'}) ?
108 scalar clean_number($params{'_from'}) : $params{'_from'},
109 base_url => 'http://api.123-txt.com/Api123WCF.svc/rest/SendSms'
110 }, $class;
111  
112 return $self;
113 }
114  
115 =head2 clean_to
116  
117 # Examine a passed phone number and attempt to return the number
118 # in a format supported by the API,
119 # croak if a conversion is not possible
120 my $intl_number = clean_to($source_number);
121  
122 The C<clean_to> method accepts a single required parameter:
123  
124 The return value is the number in international format
125  
126 =over 4
127  
128 =item source_number
129  
130 A string containing the number to be cleaned
131  
132 =back
133  
134 =cut
135  
136 sub clean_number {
137 my $source_number = shift;
138  
139 # The number may already be in the format we want
140 if ($source_number =~ /^44/) {
141 return $source_number;
142 }
143 # Strip any leading +
144 elsif ($source_number =~ /^\+44/) {
145 $source_number =~ s/^\+//;
146 return $source_number;
147 }
148 # Replace a leading 0 with 44
149 elsif ($source_number =~ /^0/) {
150 $source_number =~ s/^0/44/;
151 return $source_number;
152 }
153 # We can't do anything with this number
154 else {
155 croak 'Unrecognised number format: ' . $source_number;
156 }
157 }
158  
159 =head2 send_sms
160  
161 # Send a message
162 my $sent = $sender->send_sms(
163 text => 'This is my message',
164 to => '0000000000' # In the format: 447312345679
165 );
166  
167 The C<send_sms> method accepts two parameters, both of which are required:
168  
169 The return value is a 0 or 1 representing false or true, indicating whether
170 the send was successful.
171  
172 =over 4
173  
174 =item text
175  
176 A string of text representing the message, limited to 160 characters
177  
178 =item to
179  
180 A numeric string representing the phone number of the recipient, in a valid
181 international format (i.e. country code followed by the number)
182  
183 =back
184  
185 =cut
186  
187 sub send_sms {
188 my $self = shift;
189 my %params = @_;
190  
191 # Ensure we've been passed the parameters we're expecting
192 my @expected = ('text', 'to');
193 foreach my $expect(@expected) {
194 if (!exists $params{$expect}) {
195 croak join(', ', @expected) . ' parameters must be supplied';
196 }
197 }
198  
199 my $ua = LWP::UserAgent->new;
200 my $to = clean_number($params{to});
201 my $text = $params{text};
202  
203 # Add our authorisation credentials
204 # Send the request
205 my $body = encode_json({
206 user => $self->{'user'},
207 pass => $self->{'pass'},
208 source => $self->{'from'},
209 destination => $to,
210 sms => uri_escape($text),
211 });
212  
213 my $response = $ua->post(
214 $self->{'base_url'},
215 'Content-Type' => 'application/json',
216 Content => $body
217 );
218  
219 # Check the send succeded
220 if (!$response->is_success()) {
221 croak('API request failed: ' . $response->status_line());
222 return 0;
223 }
224  
225 return 1;
226 }
227  
228 =head1 AUTHOR
229  
230 Andrew Isherwood C<< <andrew.isherwood at ptfs-europe.com> >>
231  
232 =head1 BUGS
233  
234 Please report any bugs or features to C<andrew.isherwood at ptfs-europe.com>
235  
236 =head1 SUPPORT
237  
238 You can find documentation for this module with the perldoc command.
239  
240 perldoc SMS::Send::UK::123txt
241  
242 =head1 COPYRIGHT & LICENSE
243  
244 Copyright (C) 2018 PTFS Europe L<https://www.ptfs-europe.com/>
245  
246 This library is free software; you can redistribute it and/or modify
247 it under the same terms as Perl itself
248
249 Additionally, you are again reminded that this software comes with
250 no warranty of any kind, including but not limited to the implied
251 warranty of merchantability.
252
253 ANY use may result in charges on your 123-txt bill, and you should
254 use this software with care. The author takes no responsibility for
255 any such charges accrued.
256  
257 =head1 ACKNOWLEDGEMENTS
258  
259 Many thanks to the authors of the following modules that served as
260 inspiration for this one:
261  
262 =over 4
263  
264 =item SMS::Send::US::TMobile
265  
266  
267 =item SMS::Send::US::Ipipi
268  
269  
270 =item SMS::Send::UK::Kapow
271  
272 =back
273  
274 =cut
275  
276 1; # End of SMS::Send::UK::123txt
277