add debug flag
[Net-Vitelity.git] / lib / Net / Vitelity.pm
index b43818b..0715b91 100644 (file)
@@ -2,20 +2,15 @@ package Net::Vitelity;
 
 use warnings;
 use strict;
+use LWP::UserAgent;
 
 =head1 NAME
 
 Net::Vitelity - Interface to Vitelity API
 
-=head1 VERSION
-
-Version 0.01
-
 =cut
 
-our $VERSION = '0.01';
-
-our $URL = 'http://70.84.157.157/api.php';
+our $VERSION = '0.04';
 
 our $AUTOLOAD;
 
@@ -24,16 +19,30 @@ our $AUTOLOAD;
     use Net::Vitelity;
 
     my $vitelity = Net::Vitelity->new(
-                                       'login' => $your_login,
-                                       'pass'  => $your_pass,
+                                       'login'   => $your_login,
+                                       'pass'    => $your_pass,
+                                       'apitype' => 'api', #api, fax or lnp
+                                       'debug'   => 0,
                                      );
 
 =head1 METHODS
 
 =cut
 
+=head2 new
+
+Create a new Net::Vitelity object.  login and pass are required.
+
+=cut
+
 sub new {
-  #XXX need a generic new
+    my ($class,%data) = @_;
+    die "missing user and/or password" unless defined $data{'login'} && defined $data{'pass'};
+    my $self = { 'login' => $data{'login'}, 'pass' => $data{'pass'} };
+    $self->{apitype} = $data{'apitype'} ? $data{'apitype'} : 'api';
+    $self->{debug} = $data{'debug'} ? 1 : 0;
+    bless $self, $class;
+    return $self;
 }
 
 sub AUTOLOAD {
@@ -47,7 +56,21 @@ sub AUTOLOAD {
 
   #XXX md5 encrypt pass
 
-  my $response = $ua->post($URL, {
+  my $URL_API = 'http://api.vitelity.net/api.php';
+  my $URL_FAX = 'http://api.vitelity.net/fax.php';
+  my $URL_LNP = 'http://api.vitelity.net/lnp.php';
+
+  my $url = $URL_API;
+  $url = $URL_FAX if $self->{apitype} eq 'fax';
+  $url = $URL_LNP if $self->{apitype} eq 'lnp';
+
+  if ( $self->{debug} ) {
+    warn "Vitelity $cmd ($url)\n";
+    my %hash = @_;
+    warn "  $_: $hash{$_}\n" foreach keys %hash;
+  }
+
+  my $response = $ua->post($url, {
                     login => $self->{login}, 
                     pass  => $self->{pass},
                     cmd   => $cmd,
@@ -57,14 +80,10 @@ sub AUTOLOAD {
 
   die $response->status_line unless $response->is_success;
 
-  #hmm
-  #my $content = $response->content;
   my $content = $response->decoded_content;
 
-  #dear vitelity api: why does this make it easier to parse?
-  $content =~ s/^x\[\[//;
-  #$content =~ s/\[\[x$//;
-  $content =~ s/\]\]x$//;
+  $content =~ /x\[\[(.*)\[\[x/s;
+  $content = $1;
 
   wantarray ? split("\n", $content) : $content;
 
@@ -284,6 +303,8 @@ If you asked for a list of numbers and we had some available, they will be liste
 In a list contect, all entries will be returned in a list.  In a scalar
 scalar context, entries will be separated by newlines.
 
+=back
+
 =head1 AUTHOR
 
 Ivan Kohler, C<< <ivan-vitelity at freeside.biz> >>
@@ -322,18 +343,19 @@ L<http://search.cpan.org/dist/Net-Vitelity>
 
 =back
 
-=head1 ACKNOWLEDGEMENTS
+=head1 ADVERTISEMENTS
 
 This module was developed by Freeside Internet Services, Inc.
-If you need a complete, open-source web-based application to manage your
-customers, billing and trouble ticketing, please visit http://freeside.biz/
+Need a complete, open-source back-office and customer self-service solution?
+The Freeside software includes Vitelity integration, CDR rating,
+invoicing, credit card and electronic check processing, integrated trouble
+ticketing and customer signup and self-service web interfaces.
 
-Development sponsored by Ring Carrier LLC.  If you need a hosted or on-site
-PBX, please visit http://www.ringcarrier.com/
+http://freeside.biz/
 
 =head1 COPYRIGHT & LICENSE
 
-Copyright 2009 Freeside Internet Services, Inc.
+Copyright 2009-2017 Freeside Internet Services, Inc.
 All rights reserved.
 
 This program is free software; you can redistribute it and/or modify it