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