line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nexmo::SMS::GetBalance; |
2
|
|
|
|
|
|
|
|
3
|
8
|
|
|
8
|
|
57
|
use strict; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
169
|
|
4
|
8
|
|
|
8
|
|
27
|
use warnings; |
|
8
|
|
|
|
|
12
|
|
|
8
|
|
|
|
|
140
|
|
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
29
|
use LWP::UserAgent; |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
110
|
|
7
|
8
|
|
|
8
|
|
27
|
use JSON::PP; |
|
8
|
|
|
|
|
65
|
|
|
8
|
|
|
|
|
615
|
|
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
|
|
40
|
no strict 'refs'; |
|
8
|
|
|
|
|
27
|
|
|
8
|
|
|
|
|
2662
|
|
21
|
|
|
|
|
|
|
*{ __PACKAGE__ . '::' . $attr } = sub { |
22
|
6
|
|
|
6
|
|
10
|
my ($self,$value) = @_; |
23
|
|
|
|
|
|
|
|
24
|
6
|
|
|
|
|
9
|
my $key = '__' . $attr . '__'; |
25
|
6
|
100
|
|
|
|
12
|
$self->{$key} = $value if @_ == 2; |
26
|
6
|
|
|
|
|
14
|
return $self->{$key}; |
27
|
|
|
|
|
|
|
}; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub new { |
32
|
1
|
|
|
1
|
1
|
4
|
my ($class,%param) = @_; |
33
|
|
|
|
|
|
|
|
34
|
1
|
|
|
|
|
3
|
my $self = bless {}, $class; |
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
|
|
3
|
for my $attr ( keys %attrs ) { |
37
|
3
|
50
|
|
|
|
7
|
if ( exists $param{$attr} ) { |
38
|
3
|
|
|
|
|
9
|
$self->$attr( $param{$attr} ); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$self->user_agent( |
43
|
1
|
|
|
|
|
11
|
LWP::UserAgent->new( |
44
|
|
|
|
|
|
|
agent => 'Perl module ' . __PACKAGE__ . ' ' . $VERSION, |
45
|
|
|
|
|
|
|
), |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
1
|
|
|
|
|
3
|
return $self; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub user_agent { |
53
|
2
|
|
|
2
|
1
|
2081
|
my ($self,$ua) = @_; |
54
|
|
|
|
|
|
|
|
55
|
2
|
100
|
|
|
|
5
|
$self->{__ua__} = $ua if @_ == 2; |
56
|
2
|
|
|
|
|
4
|
return $self->{__ua__}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub get_balance { |
61
|
1
|
|
|
1
|
1
|
2
|
my ($self) = shift; |
62
|
|
|
|
|
|
|
|
63
|
1
|
|
|
|
|
2
|
my $url = sprintf "%saccount/get-balance/%s/%s", |
64
|
|
|
|
|
|
|
$self->server, |
65
|
|
|
|
|
|
|
$self->username, |
66
|
|
|
|
|
|
|
$self->password; |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
3
|
my $ua = $self->user_agent; |
69
|
1
|
|
|
|
|
3
|
$ua->default_header( 'Accept' => 'application/json' ); |
70
|
|
|
|
|
|
|
|
71
|
1
|
|
|
|
|
62
|
my $response = $ua->get( |
72
|
|
|
|
|
|
|
$url, |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
|
75
|
1
|
50
|
33
|
|
|
9
|
if ( !$response || !$response->is_success ) { |
76
|
0
|
|
|
|
|
0
|
return; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
1
|
|
|
|
|
19
|
my $json = $response->content; |
80
|
1
|
|
|
|
|
14
|
my $coder = JSON::PP->new->utf8->pretty->allow_nonref; |
81
|
1
|
|
|
|
|
111
|
my $perl = $coder->decode( $json ); |
82
|
|
|
|
|
|
|
|
83
|
1
|
50
|
33
|
|
|
318
|
return if !$perl || ref $perl ne 'HASH'; |
84
|
1
|
|
|
|
|
55
|
return $perl->{'value'}; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
__END__ |