line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nexmo::SMS::GetBalance; |
2
|
|
|
|
|
|
|
|
3
|
8
|
|
|
8
|
|
30
|
use strict; |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
238
|
|
4
|
8
|
|
|
8
|
|
32
|
use warnings; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
161
|
|
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
30
|
use LWP::UserAgent; |
|
8
|
|
|
|
|
11
|
|
|
8
|
|
|
|
|
118
|
|
7
|
8
|
|
|
8
|
|
28
|
use JSON::PP; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
702
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# ABSTRACT: Module to ask for the balance for the Nexmo SMS API! |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my %attrs = ( |
14
|
|
|
|
|
|
|
server => 'required', |
15
|
|
|
|
|
|
|
username => 'required', |
16
|
|
|
|
|
|
|
password => 'required', |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
for my $attr ( keys %attrs ) { |
20
|
8
|
|
|
8
|
|
50
|
no strict 'refs'; |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
2588
|
|
21
|
|
|
|
|
|
|
*{ __PACKAGE__ . '::' . $attr } = sub { |
22
|
6
|
|
|
6
|
|
7
|
my ($self,$value) = @_; |
23
|
|
|
|
|
|
|
|
24
|
6
|
|
|
|
|
10
|
my $key = '__' . $attr . '__'; |
25
|
6
|
100
|
|
|
|
16
|
$self->{$key} = $value if @_ == 2; |
26
|
6
|
|
|
|
|
17
|
return $self->{$key}; |
27
|
|
|
|
|
|
|
}; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 SYNOPSIS |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
This module simplifies sending SMS through the Nexmo API. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
use Nexmo::SMS::GetBalance; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $nexmo = Nexmo::SMS::GetBalance->new( |
38
|
|
|
|
|
|
|
server => 'http://rest.nexmo.com/sms/json', |
39
|
|
|
|
|
|
|
username => 'testuser1', |
40
|
|
|
|
|
|
|
password => 'testpasswd2', |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $balance = $sms->get_balance; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 METHODS |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 new |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
create a new object |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $object = Nexmo::SMS::GetBalance->new( |
52
|
|
|
|
|
|
|
server => 'http://rest.nexmo.com/sms/json', |
53
|
|
|
|
|
|
|
username => 'testuser1', |
54
|
|
|
|
|
|
|
password => 'testpasswd2', |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
This method recognises these parameters: |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
server => 'required', |
60
|
|
|
|
|
|
|
username => 'required', |
61
|
|
|
|
|
|
|
password => 'required', |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=cut |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub new { |
66
|
1
|
|
|
1
|
1
|
3
|
my ($class,%param) = @_; |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
3
|
my $self = bless {}, $class; |
69
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
3
|
for my $attr ( keys %attrs ) { |
71
|
3
|
50
|
|
|
|
6
|
if ( exists $param{$attr} ) { |
72
|
3
|
|
|
|
|
9
|
$self->$attr( $param{$attr} ); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
$self->user_agent( |
77
|
1
|
|
|
|
|
14
|
LWP::UserAgent->new( |
78
|
|
|
|
|
|
|
agent => 'Perl module ' . __PACKAGE__ . ' ' . $VERSION, |
79
|
|
|
|
|
|
|
), |
80
|
|
|
|
|
|
|
); |
81
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
4
|
return $self; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 user_agent |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Getter/setter for the user_agent attribute of the object. By default a new |
88
|
|
|
|
|
|
|
object of LWP::UserAgent is used, but you can use your own class as long as it |
89
|
|
|
|
|
|
|
is compatible to LWP::UserAgent. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
$sms->user_agent( MyUserAgent->new ); |
92
|
|
|
|
|
|
|
my $ua = $sms->user_agent; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=cut |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub user_agent { |
97
|
2
|
|
|
2
|
1
|
2393
|
my ($self,$ua) = @_; |
98
|
|
|
|
|
|
|
|
99
|
2
|
100
|
|
|
|
8
|
$self->{__ua__} = $ua if @_ == 2; |
100
|
2
|
|
|
|
|
3
|
return $self->{__ua__}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 get_balance |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
This actually calls the Nexmo SMS API. It returns the balance of the account. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
my $balance = $object->get_balance; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub get_balance { |
112
|
1
|
|
|
1
|
1
|
2
|
my ($self) = shift; |
113
|
|
|
|
|
|
|
|
114
|
1
|
|
|
|
|
3
|
my $url = sprintf "%saccount/get-balance/%s/%s", |
115
|
|
|
|
|
|
|
$self->server, |
116
|
|
|
|
|
|
|
$self->username, |
117
|
|
|
|
|
|
|
$self->password; |
118
|
|
|
|
|
|
|
|
119
|
1
|
|
|
|
|
3
|
my $ua = $self->user_agent; |
120
|
1
|
|
|
|
|
4
|
$ua->default_header( 'Accept' => 'application/json' ); |
121
|
|
|
|
|
|
|
|
122
|
1
|
|
|
|
|
46
|
my $response = $ua->get( |
123
|
|
|
|
|
|
|
$url, |
124
|
|
|
|
|
|
|
); |
125
|
|
|
|
|
|
|
|
126
|
1
|
50
|
33
|
|
|
9
|
if ( !$response || !$response->is_success ) { |
127
|
0
|
|
|
|
|
0
|
return; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
1
|
|
|
|
|
12
|
my $json = $response->content; |
131
|
1
|
|
|
|
|
13
|
my $coder = JSON::PP->new->utf8->pretty->allow_nonref; |
132
|
1
|
|
|
|
|
143
|
my $perl = $coder->decode( $json ); |
133
|
|
|
|
|
|
|
|
134
|
1
|
50
|
33
|
|
|
197
|
return if !$perl || ref $perl ne 'HASH'; |
135
|
1
|
|
|
|
|
52
|
return $perl->{'value'}; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 Attributes |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
These attributes are available for C objects. For each |
141
|
|
|
|
|
|
|
attribute there is a getter/setter: |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$nexmo->server( 'servername' ); |
144
|
|
|
|
|
|
|
my $server = $nexmo->server; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=over 4 |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item * password |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item * server |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item * username |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=back |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=cut |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
1; |
159
|
|
|
|
|
|
|
|