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