line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::SMS::ViaNett; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Net::SMS::ViaNett - Perl Interface to Vianett HTTP API |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=cut |
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
219040
|
use strict; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
116
|
|
10
|
3
|
|
|
3
|
|
26
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
100
|
|
11
|
3
|
|
|
3
|
|
18
|
use Carp; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
805
|
|
12
|
3
|
|
|
3
|
|
27
|
use Scalar::Util qw/blessed reftype/; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
1629
|
|
13
|
3
|
|
|
3
|
|
13952
|
use LWP::UserAgent; |
|
3
|
|
|
|
|
420759
|
|
|
3
|
|
|
|
|
143
|
|
14
|
3
|
|
|
3
|
|
16255
|
use XML::Simple; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Data::URIEncode; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use constant VIANETT_URL => 'http://smsc.vianett.no/ActiveServer/MT/'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = q(0.02); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _validate { |
22
|
|
|
|
|
|
|
shift; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $args = shift; |
25
|
|
|
|
|
|
|
my $transform = { }; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $allowed = { |
28
|
|
|
|
|
|
|
to => { |
29
|
|
|
|
|
|
|
translate => 'destinationaddr', |
30
|
|
|
|
|
|
|
required => 1, |
31
|
|
|
|
|
|
|
default => undef, |
32
|
|
|
|
|
|
|
}, |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
from => { |
35
|
|
|
|
|
|
|
translate => 'sourceaddr', |
36
|
|
|
|
|
|
|
required => 1, |
37
|
|
|
|
|
|
|
default => sub { int( rand( 9999 ) ) } |
38
|
|
|
|
|
|
|
}, |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
msg => { |
41
|
|
|
|
|
|
|
translate => 'message', |
42
|
|
|
|
|
|
|
required => 1, |
43
|
|
|
|
|
|
|
default => undef |
44
|
|
|
|
|
|
|
}, |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
refno => { |
47
|
|
|
|
|
|
|
translate => 'refno', |
48
|
|
|
|
|
|
|
required => 1, |
49
|
|
|
|
|
|
|
default => sub { int( rand( 999999 ) ) } |
50
|
|
|
|
|
|
|
}, |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
origin => { |
53
|
|
|
|
|
|
|
translate => 'fromalpha', |
54
|
|
|
|
|
|
|
required => 0, |
55
|
|
|
|
|
|
|
default => undef |
56
|
|
|
|
|
|
|
}, |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
header => { |
59
|
|
|
|
|
|
|
translate => 'messageheader', |
60
|
|
|
|
|
|
|
required => 0, |
61
|
|
|
|
|
|
|
default => undef |
62
|
|
|
|
|
|
|
}, |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
operator => { |
65
|
|
|
|
|
|
|
translate => 'operator', |
66
|
|
|
|
|
|
|
required => 0, |
67
|
|
|
|
|
|
|
default => 0 |
68
|
|
|
|
|
|
|
}, |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
pricegroup => { |
71
|
|
|
|
|
|
|
translate => 'pricegroup', |
72
|
|
|
|
|
|
|
required => 0, |
73
|
|
|
|
|
|
|
default => 0 |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
}; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
foreach my $k ( keys %$allowed ) { |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
__PACKAGE__->_error( "$k is a required parameter" ) |
80
|
|
|
|
|
|
|
if( |
81
|
|
|
|
|
|
|
$allowed->{$k}->{required} && # A Required Key |
82
|
|
|
|
|
|
|
! defined( $allowed->{$k}->{default} ) && # With no defaults Given |
83
|
|
|
|
|
|
|
! exists( $args->{$k} ) # Must Exist in the parameters |
84
|
|
|
|
|
|
|
); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
if( exists( $args->{$k} ) && defined( $args->{$k} ) ) { |
88
|
|
|
|
|
|
|
$transform->{ $allowed->{$k}->{translate} } = $args->{$k}; |
89
|
|
|
|
|
|
|
} elsif( $allowed->{$k}->{required} && ! exists( $args->{$k} ) ) { |
90
|
|
|
|
|
|
|
if( reftype $allowed->{$k}->{default} eq 'CODE' ) { |
91
|
|
|
|
|
|
|
$transform->{ $allowed->{$k}->{translate} } = &{$allowed->{$k}->{default}}(); |
92
|
|
|
|
|
|
|
} else { |
93
|
|
|
|
|
|
|
$transform->{ $allowed->{$k}->{translate} } = $allowed->{$k}->{default}; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
return $transform; |
99
|
|
|
|
|
|
|
}; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub new { |
104
|
|
|
|
|
|
|
my $class = shift; |
105
|
|
|
|
|
|
|
my %args = @_; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
__PACKAGE__->_error("Can't call from instantiated object") if blessed $class; |
108
|
|
|
|
|
|
|
__PACKAGE__->_error("Can't call as static method") unless( $class->isa( __PACKAGE__ ) ); |
109
|
|
|
|
|
|
|
__PACKAGE__->_error("Invalid arguments passed to the constructor") |
110
|
|
|
|
|
|
|
unless( %args && ( defined $args{username} && defined $args{password} ) ); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
my $this = { %args }; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
return bless $this, $class; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub agent { |
119
|
|
|
|
|
|
|
my ( $this, $agent ) = @_; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
__PACKAGE__->error( "Cant call as static method" ) unless blessed $this; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
return ( $this->{_agent} || __PACKAGE__ . '/' . $VERSION ) unless $agent; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
$this->{_agent} = $agent; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _to_url { |
130
|
|
|
|
|
|
|
my ( $this, $params ) = @_; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
__PACKAGE__->error( "Cant call as static method" ) unless blessed $this; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# username and password |
135
|
|
|
|
|
|
|
@{$params}{qw/username password/} = ( $this->{username}, $this->{password} ); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
return VIANETT_URL . '?' . Data::URIEncode::complex_to_query( $params ) |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub send { |
142
|
|
|
|
|
|
|
my $this = shift; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
__PACKAGE__->_error( "Cant call as static method" ) unless blessed $this; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
my %args = @_; |
147
|
|
|
|
|
|
|
my $response = $this->_call( $this->_to_url( $this->_validate( { %args } ) ) ); |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
return $response->{errorcode} == 0 ? 1 : 0; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub _call { |
155
|
|
|
|
|
|
|
my $this = shift; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__PACKAGE__->_error( "Cant call as static method" ) unless blessed $this; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
my ( $url, $ua, $response ) = ( shift, LWP::UserAgent->new, ); |
160
|
|
|
|
|
|
|
$ua->agent( $this->agent ); |
161
|
|
|
|
|
|
|
$response = $ua->get( $url ); |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
__PACKAGE__->_error( "Error Making the request, server responded with " . $response->status_line ) |
164
|
|
|
|
|
|
|
if $response->is_error; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
return XML::Simple::XMLin( $response->content ); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub _error { |
172
|
|
|
|
|
|
|
shift; |
173
|
|
|
|
|
|
|
confess shift; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
1; |
179
|
|
|
|
|
|
|
__END__ |