initial commit
[Net-VoIP_Innovations.git] / lib / Net / VoIP_Innovations.pm
1 package Net::VoIP_Innovations;
2
3 use warnings;
4 use strict;
5 use Data::Dumper;
6 use XML::Simple;
7 use XML::Writer;
8 use Net::HTTPS::Any qw( 0.10 https_post );
9
10 =head1 NAME
11
12 Net::VoIP_Innovations - Interface to VoIP_Innovations API
13
14 =cut
15
16 our $VERSION = '2.00';
17 our $URL     = 'https://www.loginto.us/VOIP/api2.pl';
18 #could be parsed from URL, if it mattered...
19 our $HOST    = 'www.loginto.us';
20 our $PATH    = '/VOIP/api2.pl';
21 our $PORT    = 443; #to allow testing to override
22
23 our $AUTOLOAD;
24 our $errstr = '';
25
26 =head1 SYNOPSIS
27
28     use Net::VoIP_Innovations;
29
30     my $handle = Net::VoIP_Innovations->new(
31         'login'    => 'tofu',
32         'password' => 'beast',
33         'debug'    => 1,
34     );
35
36     #DID functions
37     #auditDIDs
38     #queryDID
39     #reserveDID
40     #assignDID
41     #configDID
42     #releaseDID
43
44     #911 Functions
45
46     #Locator Functions
47
48     ...
49
50 =head1 METHODS
51
52 =head2 new HASHREF | OPTION, VALUE ...
53
54 Creates a new Net::VoIP_Innovations object.  Options may be passed
55 as a hash reference or a flat list of names and values.
56
57 =over 4
58
59 =item login (required)
60
61 =item password (required)
62
63 =item login (required)
64
65 =back
66
67 =cut
68
69 #  If there is an error,
70 #returns false and sets an error string which may be queried with the I<errstr>
71 #class method.
72
73 sub new {
74   my $proto = shift;
75   my $class = ref($proto) || $proto;
76   my $self = ref($_[0]) ? shift : { @_ };
77   $self->{'debug'} ||= 0;
78   bless($self, $class);
79 }
80
81 =head2 errstr
82
83 =cut
84
85 sub errstr {
86   my $class = shift;
87
88   return $errstr
89     unless ref($class) && $class->isa('Net::VoIP_Innovations');
90
91   my $self = $class;
92   $self->{errstr};
93 }
94
95 sub DESTROY { }; # no-op
96
97 sub AUTOLOAD {
98   my $self = shift;
99   my $opts = ref($_[0]) ? shift : { @_ };
100
101   $AUTOLOAD =~ /(^|::)(\w+)$/ or die "unparsable AUTOLOAD: $AUTOLOAD";
102   my $function = $2;
103
104   my $output;
105   my $w = new XML::Writer(OUTPUT => \$output, DATA_MODE => 1, DATA_INDENT => 3);
106
107   $w->xmlDecl('ISO-8859-1');
108   $w->doctype('request', undef, $URL);
109
110   $w->startTag('request', 'id' => '');  #XXX request ID???
111     $w->startTag('header');
112       $w->startTag('sender');
113
114         if ( $self->{'sessionid'} ) {
115
116           #$w->dataElement( 'login'     => ''                   );
117           #$w->dataElement( 'password'  => ''                   );
118           $w->dataElement( 'sessionid' => $self->{'sessionid'} );
119
120         } else {
121
122           $w->dataElement( 'login'     => $self->{'login'}     );
123           $w->dataElement( 'password'  => $self->{'password'}  );
124           #$w->dataElement( 'sessionid' => ''                   );
125
126         }
127
128       $w->endTag('sender');
129     $w->endTag('header');
130
131     $w->startTag('body');
132
133       $w->dataElement( 'requesttype' => $function );
134
135       if ( keys %$opts ) {
136         $w->startTag('item');
137           foreach my $opt ( keys %$opts ) {
138             $w->dataElement( $opt => $opts->{$opt} );
139           }
140         $w->endTag('item');
141       }
142
143     $w->endTag('body');
144
145   $w->endTag('request');
146
147   #$output =~ s/\n+/\n/g;
148
149   warn "XML Request for $function function:\n$output"
150     if $self->{'debug'} > 1;
151
152   my( $page, $response, %reply_headers ) = https_post(
153     'host'         => $HOST,
154     'port'         => $PORT,
155     'path'         => ($PATH||443),
156     'args'         => { 'apidata' => $output, },
157     #'content'      => $output,
158     #'Content-Type' => 'text/plain',
159     #'Content-Type' => 'text/xml',
160     'Content-Type' => 'application/xml',
161     #'headers' => {},
162     'debug'        => ( $self->{'debug'} > 1 ? 1 : 0 ),
163   );
164
165   unless ( $response =~ /^200/i ) {
166     $self->{'errstr'} = $response;
167     return '';
168   }
169
170   warn "XML Response for $function function:\n: $page"
171     if $self->{'debug'} > 1;
172
173   my $hashref = XMLin( $page );
174
175   warn "Parsed response for $function funtion:\n". Dumper($hashref)
176     if $self->{'debug'} > 1;
177
178   my $return = $hashref->{'body'};
179
180   warn "Returning data:\n". Dumper($return)
181     if $self->{'debug'} > 1;
182
183   $return;
184
185 }
186
187 =head1 AUTHOR
188
189 Ivan Kohler, C<< <ivan-net-voipinnovations at freeside.biz> >>
190
191 =head1 BUGS
192
193 Please report any bugs or feature requests to C<bug-net-voip_innovations at rt.cpan.org>, or through
194 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-VoIP_Innovations>.  I will be notified, and then you'll
195 automatically be notified of progress on your bug as I make changes.
196
197
198
199
200 =head1 SUPPORT
201
202 You can find documentation for this module with the perldoc command.
203
204     perldoc Net::VoIP_Innovations
205
206
207 You can also look for information at:
208
209 =over 4
210
211 =item * RT: CPAN's request tracker
212
213 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-VoIP_Innovations>
214
215 =item * AnnoCPAN: Annotated CPAN documentation
216
217 L<http://annocpan.org/dist/Net-VoIP_Innovations>
218
219 =item * CPAN Ratings
220
221 L<http://cpanratings.perl.org/d/Net-VoIP_Innovations>
222
223 =item * Search CPAN
224
225 L<http://search.cpan.org/dist/Net-VoIP_Innovations>
226
227 =back
228
229
230 =head1 ACKNOWLEDGEMENTS
231
232
233 =head1 COPYRIGHT & LICENSE
234
235 Copyright 2008-2012 Freeside Internet Services, Inc. (http://freeside.biz/)
236
237 This program is free software; you can redistribute it and/or modify it
238 under the same terms as Perl itself.
239
240
241 =head1 ADVERTISEMENT
242
243 Need a complete, open-source back-office and customer self-service solution?
244 The Freeside software includes support for VoIP Innovations integration, CDR
245 rating, invoicing, credit card and electronic check processing, integrated
246 trouble ticketing, and customer signup and self-service web interfaces.
247
248 http://freeside.biz/freeside/
249
250 =cut
251
252 1; # End of Net::VoIP_Innovations