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