blib/lib/Net/SMS/160By2.pm

filter: Statementstmt 26.3% 29 / 110 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.1% 8 / 14 Podpod 100% 2 / 2 totaltotal 24.5% 39 / 159 CC 16 SCAR 32.5
CC
16
Cov
21%
_send
56.0
send_sms
42.0
new
20.0
CRAP 25.8
↑ Summary
1 package Net::SMS::160By2;
2
3 2 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # Load this to handle exceptions nicely
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 # Load this to make HTTP Requests
10 2 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTML::TagParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 # Load this to uncompress the gzip content of http response.
15 2 ▶use Compress::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 =head1 NAME
18
19 Net::SMS::160By2 - Send SMS using your 160By2 account!
20
21 =head1 VERSION
22
23 Version 0.03
24
25 =cut
26
27 2 our $VERSION = '0.03';
28
29 2 our $HOME_URL = 'http://160by2.com/Login';
30 2 our $SENDSMS_URL = 'http://160by2.com/FebSendSMS';
31 2 our $SENDSMS_SUBMIT_URL = 'http://160by2.com/Feb7SendSMS';
32
33 =head1 SYNOPSIS
34
35 This module provides a wrapper around 160By2.com to send an SMS to any mobile number in
36
37 India, Kuwait, UAE, Saudi, Singapore, Philippines & Malaysia at present.
38
39 you can use this as follows.
40
41 use Net::SMS::160By2;
42
43 my $obj = Net::SMS::160By2->new($username, $password);
44
45 $obj->send_sms($msg, $to);
46
47 # send additional params will print WWW::Mechanize detailed request and
48 # responses
49
50 my $debug_obj = Net::SMS::160By2->new($username, $password, {debug => 1});
51
52 $debug_obj->send_sms($msg, $to);
53
54 Thats it!
55
56 =head1 SUBROUTINES/METHODS
57
58 =head2 new
59
60 This is constructor method.
61
62 input: username, password
63
64 A new object will be created with username, password attributes.
65
66 You can send additional params in a hash ref as 3rd parameter.
67
68 at present only debug option is handled in additional params.
69
70 output: Net::SMS::160By2 object
71
72 =cut
73
74 sub new {
75 0 ▶ my $class = shift;
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
76
77 # read username and password
78 0 my $username = shift;
79 0 my $password = shift;
80 0 my $extra = shift;
81 0 ▶ $extra = {} unless ref($extra) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $extra eq "HASH" 0 0 0
82
83 # Throw error in case of no username or password
84 0 ▶ croak("No username provided") unless ($username);
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 0 0
85 0 ▶ croak("No password provided") unless ($password);
Branch: 1 branch
BranchTrueFalseTotal
unless $password 0 0 0
86
87 # return blessed object
88 my $self = bless {
89 'username' => $username,
90 'password' => $password,
91 'mobile' => undef,
92 'message' => undef,
93 'query_form' => undef,
94 0 %{$extra}
95 }, $class;
96 0 return $self;
97 }
98
99 =head2 send_sms
100
101 This method is used to send an SMS to any mobile number.
102 input : message, to
103
104 where message contains the information you want to send.
105 to is the recipient mobile number
106
107 =cut
108
109 sub send_sms {
110 0 ▶ my ($self, $msg, $to) = @_;
Subroutine send_sms: not called CC=6 SCAR=37.4
Pod documented
111 0 ▶ croak("Message or mobile number are missing") unless ($msg || $to);
Branch: 1 branch
BranchTrueFalseTotal
unless $msg or $to 0 0 0
Condition0%0/3
$msg or $to
l!l&&r!l&&!r
000
MC/DC0%0/2
$msg or $to
$msg $to
Truth table0%0/3
A or B
A= $msgB= $to
ABresult
1X1
011
000
112
113 # trim spaces
114 0 $msg =~ s/^\s+|\s+$//;
115 0 $to =~ s/^\s+|\s+$//;
116
117 # set message and mobile number
118 0 $self->{message} = $msg;
119 0 $self->{mobile} = $to;
120
121 # create mechanize object
122 0 my $mech = WWW::Mechanize->new(autocheck => 1);
123 0 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
124 0 ▶ $mech->add_handler("request_send", sub { shift->dump; return });
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 ▶ $mech->add_handler("response_done", sub { shift->dump; return });
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 }
127 0 $mech->agent_alias( 'Windows Mozilla' );
128
129 # Now connect to 160By2 Website login page
130 0 $mech->get($HOME_URL);
131
132 # handle gzip content
133 0 my $response = $mech->response->content;
134 0 ▶ if ( $mech->response->header('Content-Encoding') eq 'gzip' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->response->header("Content-Encoding") eq "gzip") 0 0 0
135 0 $response = Compress::Zlib::memGunzip($response );
136 0 $mech->update_html( $response )
137 }
138
139 # login to 160By2
140 0 my $status = $self->_login($mech);
141
142 0 ▶ die "Login Failed" unless $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 0 0 0
143
144 # sendsms from 160by2
145 0 return $self->_send($mech);
146 }
147
148 sub _login {
149 0 ▶ my ($self, $mech) = @_;
Subroutine _login: not called CC=2 SCAR=17.9
150
151 # Get login form with htxt_UserName, txt_Passwd
152 0 $mech->form_with_fields('username', 'password');
153
154 # set htxt_UserName, txt_Passwd
155 0 $mech->field('username', $self->{username});
156 0 $mech->field('password', $self->{password});
157
158 # submit form
159 0 $mech->submit_form();
160
161 # Verify login success/failed
162 # handle gzip content
163 0 my $response = $mech->response->content;
164 0 ▶ if ( $mech->response->header('Content-Encoding') eq 'gzip' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->response->header("Content-Encoding") eq "gzip") 0 0 0
165 0 $response = Compress::Zlib::memGunzip( $response );
166 0 $mech->update_html( $response )
167 }
168 0 my $home_uri = URI->new($mech->base());
169 0 my @q = $home_uri->query_form;
170 0 $self->{query_form} = \@q;
171 0 return $mech;
172 }
173
174 sub _send {
175 0 ▶ my ($self, $mech) = @_;
Subroutine _send: not called CC=7 SCAR=40.3
176
177 # Try to go to Home Page
178 0 my $sendsms_uri = URI->new($SENDSMS_URL);
179 0 $sendsms_uri->query_form(@{$self->{query_form}});
180
181 0 my $sendsms_submit_uri = URI->new($SENDSMS_SUBMIT_URL);
182 0 $sendsms_submit_uri->query_form(@{$self->{query_form}});
183
184 # Get content of SendSMS form page
185 0 $mech->get($sendsms_uri->as_string);
186
187 0 my $response;
188 0 ▶ if ( $mech->response->header('Content-Encoding') eq 'gzip' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->response->header("Content-Encoding") eq "gzip") 0 0 0
189 # handle gzip content
190 0 $response = $mech->response->content;
191 0 $response = Compress::Zlib::memGunzip( $response );
192 0 $mech->update_html( $response );
193 }
194 # set form action
195 0 my $form = $mech->form_name('frm_sendsms');
196 0 $form->action($sendsms_submit_uri);
197
198 # Use TagParser here, this will make our job easier
199 0 my $tp = HTML::TagParser->new($response);
200 0 my $sm_form = $tp->getElementById( "frm_sendsms" );
201 0 my $sm_form_tree = $sm_form->subTree();
202
203 # User HTML::TagParser to recognize dynamically generated mobile number input, message textarea element id/names
204 0 my ($mob_elem) = $sm_form_tree->getElementsByAttribute('tabindex', "1");
205 0 ▶ if ($mob_elem) { # we have to consider tabindex=1 as mobile number input
Branch: 1 branch
BranchTrueFalseTotal
if ($mob_elem) 0 0 0
206 0 $mech->field($mob_elem->getAttribute('id'), $self->{mobile});
207 }
208 0 my ($msg_elem) = $sm_form_tree->getElementsByAttribute('tabindex', "2");
209 0 ▶ if ($msg_elem) { # we have to consider tabindex=2 as message textarea
Branch: 1 branch
BranchTrueFalseTotal
if ($msg_elem) 0 0 0
210 0 $mech->field($msg_elem->getAttribute('id'), $self->{message});
211 }
212
213 # set additional params
214 0 my %params = @{$self->{query_form}};
215 0 $mech->field('hid_exists', "no");
216 0 $mech->field('feb2by2session', $params{id});
217 # submit form
218 0 $mech->submit();
219
220
221 # is URL call Success?
222 0 ▶ if ($mech->success()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->success) 0 0 0
223
224 # Check sms sent successfully
225 0 my $response = $mech->response->content;
226 0 ▶ if($mech->response->header("Content-Encoding") eq "gzip") {
Branch: 1 branch
BranchTrueFalseTotal
if ($mech->response->header("Content-Encoding") eq "gzip") 0 0 0
227 0 $response = Compress::Zlib::memGunzip($response) ;
228 }
229 # return 1(true) in case of success
230 0 ▶ return 1 if($response =~ m/Your message has been Sent/sig);
Branch: 1 branch
BranchTrueFalseTotal
if $response =~ /Your message has been Sent/sig 0 0 0
231 }
232 # return undef as failure
233 0 return;
234 }
235
236 =head1 AUTHOR
237
238 Mohan Prasad Gutta, C<< <mohanprasadgutta at gmail.com> >>
239
240 =head1 BUGS
241
242 Please report any bugs or feature requests to C<bug-net-sms-160by2 at rt.cpan.org>, or through
243 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-SMS-160By2>. I will be notified, and then you'll
244 automatically be notified of progress on your bug as I make changes.
245
246
247
248
249 =head1 SUPPORT
250
251 You can find documentation for this module with the perldoc command.
252
253 perldoc Net::SMS::160By2
254
255
256 You can also look for information at:
257
258 =over 4
259
260 =item * RT: CPAN's request tracker
261
262 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-SMS-160By2>
263
264 =item * AnnoCPAN: Annotated CPAN documentation
265
266 L<http://annocpan.org/dist/Net-SMS-160By2>
267
268 =item * CPAN Ratings
269
270 L<http://cpanratings.perl.org/d/Net-SMS-160By2>
271
272 =item * Search CPAN
273
274 L<http://search.cpan.org/dist/Net-SMS-160By2/>
275
276 =back
277
278
279 =head1 ACKNOWLEDGEMENTS
280
281
282 =head1 LICENSE AND COPYRIGHT
283
284 Copyright 2010 Mohan Prasad Gutta.
285
286 This program is free software; you can redistribute it and/or modify it
287 under the terms of either: the GNU General Public License as published
288 by the Free Software Foundation; or the Artistic License.
289
290 See http://dev.perl.org/licenses/ for more information.
291
292
293 =cut
294
295 2 1; # End of Net::SMS::160By2
↑ Summary