line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nexmo::SMS::TextMessage; |
2
|
|
|
|
|
|
|
|
3
|
8
|
|
|
8
|
|
37
|
use strict; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
281
|
|
4
|
8
|
|
|
8
|
|
31
|
use warnings; |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
179
|
|
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
28
|
use Nexmo::SMS::Response; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
108
|
|
7
|
|
|
|
|
|
|
|
8
|
8
|
|
|
8
|
|
25
|
use LWP::UserAgent; |
|
8
|
|
|
|
|
8
|
|
|
8
|
|
|
|
|
106
|
|
9
|
8
|
|
|
8
|
|
30
|
use JSON::PP; |
|
8
|
|
|
|
|
7
|
|
|
8
|
|
|
|
|
807
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# ABSTRACT: Module that respresents a text message for the Nexmo SMS API! |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my %attrs = ( |
16
|
|
|
|
|
|
|
text => 'required', |
17
|
|
|
|
|
|
|
from => 'required', |
18
|
|
|
|
|
|
|
to => 'required', |
19
|
|
|
|
|
|
|
server => 'required', |
20
|
|
|
|
|
|
|
username => 'required', |
21
|
|
|
|
|
|
|
password => 'required', |
22
|
|
|
|
|
|
|
type => 'optional', |
23
|
|
|
|
|
|
|
status_report_req => 'optional', |
24
|
|
|
|
|
|
|
client_ref => 'optional', |
25
|
|
|
|
|
|
|
network_code => 'optional', |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
for my $attr ( keys %attrs ) { |
29
|
8
|
|
|
8
|
|
39
|
no strict 'refs'; |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
3589
|
|
30
|
|
|
|
|
|
|
*{ __PACKAGE__ . '::' . $attr } = sub { |
31
|
48
|
|
|
48
|
|
64
|
my ($self,$value) = @_; |
32
|
|
|
|
|
|
|
|
33
|
48
|
|
|
|
|
79
|
my $key = '__' . $attr . '__'; |
34
|
48
|
100
|
|
|
|
115
|
$self->{$key} = $value if @_ == 2; |
35
|
48
|
|
|
|
|
219
|
return $self->{$key}; |
36
|
|
|
|
|
|
|
}; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 SYNOPSIS |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
This module simplifies sending SMS through the Nexmo API. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
use Nexmo::SMS::TextMessage; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $nexmo = Nexmo::SMS::TextMessage->new( |
47
|
|
|
|
|
|
|
server => 'http://rest.nexmo.com/sms/json', |
48
|
|
|
|
|
|
|
username => 'testuser1', |
49
|
|
|
|
|
|
|
password => 'testpasswd2', |
50
|
|
|
|
|
|
|
text => 'This is a test', |
51
|
|
|
|
|
|
|
from => 'Test02', |
52
|
|
|
|
|
|
|
to => '452312432', |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $response = $sms->send || die $sms->errstr; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
if ( $response->is_success ) { |
58
|
|
|
|
|
|
|
print "SMS was sent...\n"; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 METHODS |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head2 new |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
create a new object |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my $message = Nexmo::SMS::TextMessage->new( |
68
|
|
|
|
|
|
|
server => 'http://rest.nexmo.com/sms/json', |
69
|
|
|
|
|
|
|
username => 'testuser1', |
70
|
|
|
|
|
|
|
password => 'testpasswd2', |
71
|
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
This method recognises these parameters: |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
text => 'required', |
76
|
|
|
|
|
|
|
from => 'required', |
77
|
|
|
|
|
|
|
to => 'required', |
78
|
|
|
|
|
|
|
server => 'required', |
79
|
|
|
|
|
|
|
username => 'required', |
80
|
|
|
|
|
|
|
password => 'required', |
81
|
|
|
|
|
|
|
type => 'optional', |
82
|
|
|
|
|
|
|
status_report_req => 'optional', |
83
|
|
|
|
|
|
|
client_ref => 'optional', |
84
|
|
|
|
|
|
|
network_code => 'optional', |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub new { |
89
|
3
|
|
|
3
|
1
|
12
|
my ($class,%param) = @_; |
90
|
|
|
|
|
|
|
|
91
|
3
|
|
|
|
|
12
|
my $self = bless {}, $class; |
92
|
|
|
|
|
|
|
|
93
|
3
|
|
|
|
|
12
|
for my $attr ( keys %attrs ) { |
94
|
30
|
100
|
|
|
|
56
|
if ( exists $param{$attr} ) { |
95
|
18
|
|
|
|
|
54
|
$self->$attr( $param{$attr} ); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
$self->user_agent( |
100
|
3
|
|
|
|
|
43
|
LWP::UserAgent->new( |
101
|
|
|
|
|
|
|
agent => 'Perl module ' . __PACKAGE__ . ' ' . $VERSION, |
102
|
|
|
|
|
|
|
), |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
3
|
|
|
|
|
23
|
return $self; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head2 user_agent |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Getter/setter for the user_agent attribute of the object. By default a new |
111
|
|
|
|
|
|
|
object of LWP::UserAgent is used, but you can use your own class as long as it |
112
|
|
|
|
|
|
|
is compatible to LWP::UserAgent. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
$sms->user_agent( MyUserAgent->new ); |
115
|
|
|
|
|
|
|
my $ua = $sms->user_agent; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub user_agent { |
120
|
6
|
|
|
6
|
1
|
9451
|
my ($self,$ua) = @_; |
121
|
|
|
|
|
|
|
|
122
|
6
|
100
|
|
|
|
29
|
$self->{__ua__} = $ua if @_ == 2; |
123
|
6
|
|
|
|
|
19
|
return $self->{__ua__}; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 errstr |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
return the "last" error as string. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
print $sms->errstr; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=cut |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub errstr { |
135
|
2
|
|
|
2
|
1
|
6
|
my ($self,$message) = @_; |
136
|
|
|
|
|
|
|
|
137
|
2
|
100
|
|
|
|
13
|
$self->{__errstr__} = $message if @_ == 2; |
138
|
2
|
|
|
|
|
13
|
return $self->{__errstr__}; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 send |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
This actually calls the Nexmo SMS API. It returns a L object or |
144
|
|
|
|
|
|
|
C (on failure). |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
my $sms = Nexmo::SMS::TextMessage->new( ... ); |
147
|
|
|
|
|
|
|
$sms->send or die $sms->errstr; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=cut |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub send { |
152
|
3
|
|
|
3
|
1
|
1666
|
my ($self) = shift; |
153
|
|
|
|
|
|
|
|
154
|
3
|
|
|
|
|
6
|
my %optional; |
155
|
3
|
50
|
|
|
|
20
|
$optional{'client-ref'} = $self->client_ref if $self->client_ref; |
156
|
3
|
50
|
|
|
|
14
|
$optional{'status-report-req'} = $self->status_report_req if $self->status_report_req; |
157
|
3
|
50
|
|
|
|
15
|
$optional{'network-code'} = $self->network_code if $self->network_code; |
158
|
3
|
50
|
|
|
|
12
|
$optional{'type'} = $self->type if $self->type; |
159
|
|
|
|
|
|
|
|
160
|
3
|
|
|
|
|
12
|
my $response = $self->user_agent->post( |
161
|
|
|
|
|
|
|
$self->server, |
162
|
|
|
|
|
|
|
{ |
163
|
|
|
|
|
|
|
%optional, |
164
|
|
|
|
|
|
|
username => $self->username, |
165
|
|
|
|
|
|
|
password => $self->password, |
166
|
|
|
|
|
|
|
from => $self->from, |
167
|
|
|
|
|
|
|
to => $self->to, |
168
|
|
|
|
|
|
|
text => $self->text, |
169
|
|
|
|
|
|
|
}, |
170
|
|
|
|
|
|
|
); |
171
|
|
|
|
|
|
|
|
172
|
3
|
50
|
33
|
|
|
48
|
if ( !$response || !$response->is_success ) { |
173
|
0
|
|
|
|
|
0
|
$self->errstr("Request was not successful: " . $response->status_line); |
174
|
0
|
0
|
|
|
|
0
|
warn $response->content if $response; |
175
|
0
|
|
|
|
|
0
|
return; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
3
|
|
|
|
|
48
|
my $json = $response->content; |
179
|
3
|
|
|
|
|
75
|
my $response_object = Nexmo::SMS::Response->new( json => $json ); |
180
|
|
|
|
|
|
|
|
181
|
3
|
100
|
|
|
|
17
|
if ( $response_object->is_error ) { |
182
|
1
|
|
|
|
|
2
|
$self->errstr( $response_object->errstr ); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
3
|
|
|
|
|
73
|
return $response_object; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 check_needed_params |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
This method checks if all needed parameters are passed. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
my $params_not_ok = Nexmo::SMS::TextMessage->check_needed_params( ... ); |
193
|
|
|
|
|
|
|
if ( $params_not_ok ) { |
194
|
|
|
|
|
|
|
print "Please check $params_not_ok"; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=cut |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub check_needed_params { |
200
|
4
|
|
|
4
|
1
|
15
|
my ($class,%params) = @_; |
201
|
|
|
|
|
|
|
|
202
|
4
|
|
|
|
|
7
|
my @params_not_ok; |
203
|
|
|
|
|
|
|
|
204
|
4
|
|
|
|
|
22
|
for my $attr ( keys %attrs ) { |
205
|
40
|
100
|
100
|
|
|
144
|
if ( $attrs{$attr} eq 'required' and !$params{$attr} ) { |
206
|
1
|
|
|
|
|
1
|
push @params_not_ok, $attr; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
4
|
|
|
|
|
22
|
return join ", ", @params_not_ok; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=head1 Attributes |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
These attributes are available for C objects: |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=over 4 |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item * client_ref |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=item * from |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=item * network_code |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=item * password |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=item * server |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=item * status_report_req |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=item * text |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=item * to |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=item * type |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=item * username |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=back |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=cut |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
1; |
244
|
|
|
|
|
|
|
|