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

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