blib/lib/SMS/Send/UK/BTSmartMessaging.pm untested

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