File Coverage

blib/lib/SMS/Send/BudgetSMS.pm
Criterion Covered Total %
statement 24 31 77.4
branch 3 8 37.5
condition 8 15 53.3
subroutine 7 7 100.0
pod 2 2 100.0
total 44 63 69.8


line stmt bran cond sub pod time code
1             package SMS::Send::BudgetSMS;
2              
3 2     2   450565 use strict;
  2         5  
  2         103  
4 2     2   8 use warnings FATAL => 'all';
  2         5  
  2         221  
5              
6 2     2   1840 use LWP::UserAgent;
  2         151556  
  2         96  
7 2     2   1510 use Number::Phone::Normalize;
  2         3808  
  2         141  
8              
9 2     2   16 use base 'SMS::Send::Driver';
  2         3  
  2         1043  
10              
11             our $VERSION = '0.05';
12              
13             sub new {
14 6     6 1 171431 my ( $class, %args ) = @_;
15              
16 6 100 100     40 unless ( $args{'_login'} && $args{'_password'} && $args{'_userid'} ) {
      100        
17 3         24 die '_login, _password and _userid are required';
18             }
19              
20 3         18 my $self = bless {
21             _endpoint => 'https://api.budgetsms.net/sendsms/',
22             _timeout => 20,
23             %args
24             }, $class;
25              
26             $self->{_ua} = LWP::UserAgent->new(
27             agent => join( '/', $class, $VERSION ),
28             timeout => $self->{_timeout}
29 3         33 );
30              
31 3         3393 return $self;
32             }
33              
34             sub send_sms {
35 2     2 1 6 my ( $self, %args ) = @_;
36              
37 2 50 66     11 unless ( $args{'to'} && $args{'text'} ) {
38 2         30 die 'to and text are required';
39             }
40              
41 0 0 0       if ( !$args{'_from'} && !$self->{'_from'} ) {
42 0           die 'SMS::Send::BudgetSMS->new() or send_sms() requires parameter _from';
43             }
44              
45             my $to_number = Number::Phone::Normalize->new(
46             IntlPrefix => '+'
47 0           )->intl( $args{'to'} );
48 0           $to_number =~ s/^\+//;
49              
50             my $res = $self->{_ua}->post(
51             $self->{'_endpoint'},
52             Content => {
53             username => $self->{'_login'}, # field 'username' in HTTP API
54             userid => $self->{'_userid'},
55             handle => $self->{'_password'}, # field 'handle' in HTTP API
56             msg => $args{'text'},
57 0   0       from => $args{'_from'} || $self->{'_from'},
58             to => $to_number,
59             },
60             );
61              
62 0 0         return $res->decoded_content if ($res->decoded_content =~ m/^OK \d*$/);
63              
64 0           return 0;
65             }
66              
67             1;
68              
69             =pod
70              
71             =for stopwords ACKNOWLEDGEMENTS CPAN Centre Unicode homepage
72              
73             =head1 NAME
74              
75             SMS::Send::BudgetSMS - SMS::Send driver to send messages via BudgetSMS, L
76              
77             =head1 VERSION
78              
79             version 0.04
80              
81             =head1 SYNOPSIS
82              
83             use SMS::Send;
84              
85             # Create a sender
86             my $sender = SMS::Send->new(
87             'BudgetSMS',
88             _login => 'budgetsms_username',
89             _userid => 'budgetsms_userid',
90             _password => 'budgetsms_handle',
91             );
92              
93             # Send a message
94             my $sent = $sender->send_sms(
95             text => 'This is a test message',
96             to => '+61 (4) 1234 5678',
97             );
98              
99             if ($sent) {
100             print "Message sent ok\n";
101             }
102             else {
103             print "Failed to send message\n";
104             }
105              
106             =head1 DESCRIPTION
107              
108             SMS::Send driver for BudgetSMS - L
109              
110             This is not intended to be used directly, but instead called by SMS::Send (see
111             synopsis above for a basic illustration, and see SMS::Send's documentation for
112             further information).
113              
114             The driver uses the BudgetSMS HTTP API mechanism. This is documented at
115             L
116              
117             =head1 METHODS
118              
119             =head2 new
120              
121             Constructor, takes argument pairs passed by SMS::Send, returns an
122             SMS::Send::BudgetSMS object. See usage synopsis for example, and see SMS::Send
123             documentation for further info on using SMS::Send drivers.
124              
125             Additional arguments that may be passed include:-
126              
127             =over 3
128              
129             =item _userid
130              
131             BudgetSMS userid
132              
133             =item _endpoint
134              
135             The HTTP API endpoint. Defaults to
136             C
137              
138             For development purposes, you may also use test API
139             C
140              
141             =item _timeout
142              
143             The timeout in seconds for HTTP operations. Defaults to 20 seconds.
144              
145             =back
146              
147             =head2 send_sms
148              
149             Send the message - see SMS::Send for details. Additionally the following
150             options can be given - these have the same meaning as they do in the C
151             method:-
152              
153             =over 1
154              
155             =item _from
156              
157             Alphanumeric or Numeric senderid (shown as the sender of SMS)
158              
159             =back
160              
161             =head1 INSTALLATION
162              
163             See perlmodinstall for information and options on installing Perl modules.
164              
165             =head1 BUGS AND LIMITATIONS
166              
167             You can make new bug reports, and view existing ones, through GitHub
168             at L.
169              
170             =head1 AVAILABILITY
171              
172             The project homepage is L.
173              
174             The latest version of this module is available from the Comprehensive Perl
175             Archive Network (CPAN). Visit L to find a CPAN
176             site near you, or see L.
177              
178             =head1 AUTHOR
179              
180             Lari Taskula
181             Hypernova Oy, L
182              
183             =head1 COPYRIGHT AND LICENSE
184              
185             This software is copyright (c) 2019 by Hypernova Oy.
186              
187             This is free software; you can redistribute it and/or modify it under
188             the same terms as the Perl 5 programming language system itself.
189              
190             =cut