summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/fibernetics_did.pm
blob: 9fd996805cad27c3a1227ce536f2316af24ffddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package FS::part_export::fibernetics_did;
use base qw( FS::part_export );

use strict;
use vars qw( %info $DEBUG );
use Data::Dumper;
use URI::Escape;
#use Locale::SubCountry;
#use FS::Record qw(qsearch dbh);
use XML::Simple;
#use Net::HTTPS::Any qw( 0.10 https_get );
use LWP::UserAgent;
use HTTP::Request::Common;

$DEBUG = 0;

tie my %options, 'Tie::IxHash',
  'country' => { 'label' => 'Country', 'default' => 'CA', size=>2, },
;

%info = (
  'svc'        => 'svc_phone',
  'desc'       => 'Provision phone numbers to Fibernetics web services API',
  'options'    => \%options,
  'notes'      => '',
);

sub rebless { shift; }

sub can_get_dids { 1; }
sub get_dids_can_tollfree { 0; };
sub get_dids_can_manual   { 1; };
sub get_dids_can_edit     { 1; };
sub get_dids_npa_select   { 0; };

# i guess we could get em from the API, but since its returning states without
#  availability, there's no advantage
    # not really needed, we maintain our own list of provinces, but would
    #  help to hide the ones without availability (need to fix the selector too)
our @states = (
  'Alberta',
  'British Columbia',
  'Ontario',
  'Quebec',
  #'Saskatchewan',
  #'The Territories',
  #'PEI/Nova Scotia',
  #'Manitoba',
  #'Newfoundland',
  #'New Brunswick',
);

sub get_dids {
  my $self = shift;
  my %opt = ref($_[0]) ? %{$_[0]} : @_;

  if ( $opt{'tollfree'} ) {
    warn 'Fibernetics DID provisioning does not yet support toll-free numbers';
    return [];
  }

  my %query_hash = ();

  #ratecenter + state: return numbers (more structured names, npa selection)
  #areacode + exchange: return numbers
  #areacode: return city/ratecenter/whatever
  #state: return areacodes

  #region + state: return numbers (arbitrary names, no npa selection)
  #state: return regions

#  if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
#
#    $query_hash{'region'} = $opt{'exchange'};
#
#  } elsif ( $opt{'areacode'} ) {
#
#    $query_hash{'npa'} = $opt{'areacode'};

  #if ( $opt{'state'} && $opt{'region'} ) { #return numbers
  if ( $opt{'region'} ) { #return numbers

    #$query_hash{'province'} = $country->full_name($opt{'state'});
    $query_hash{'region'}   = $opt{'region'}

  } elsif ( $opt{'state'} ) { #return regions

    #my $country = new Locale::SubCountry( $self->option('country') );
    #$query_hash{'province'}   = $country->full_name($opt{'state'});
    $query_hash{'province'}   = $opt{'state'};
    $query_hash{'listregion'} = 1;

  } else { #nothing passed, return states (provinces)

    return \@states;

  }


  my $url = 'http://'. $self->machine. '/porta/cgi-bin/porta_query.cgi';
  if ( keys %query_hash ) {
    $url .= '?'. join('&', map "$_=". uri_escape($query_hash{$_}),
                             keys %query_hash
                     );
  }
  warn $url if $DEBUG;

  #my( $page, $response, %reply_headers) = https_get(
  #  'host' => $self->machine,
  #);

  my $ua = LWP::UserAgent->new;
  #my $response = $ua->$method(
  #  $url, \%data,
  #  'Content-Type'=>'application/x-www-form-urlencoded'
  #);
  my $req = HTTP::Request::Common::GET( $url );
  my $response = $ua->request($req);

  die $response->error_as_HTML if $response->is_error;

  my $page = $response->content;

  my $data = XMLin( $page );

  warn Dumper($data) if $DEBUG;

#  if ( $opt{'areacode'} && $opt{'exchange'} ) { #return numbers
#
#    [ map $_->{'number'}, @{ $data->{'item'} } ];
#
#  } elsif ( $opt{'areacode'} ) {
#
#    [ map $_->{'region'}, @{ $data->{'item'} } ];
#
#  } elsif ( $opt{'state'} ) { #return areacodes
#
#    [ map $_->{'npa'}, @{ $data->{'item'} } ];

  #if ( $opt{'state'} && $opt{'region'} ) { #return numbers
  if ( $opt{'region'} ) { #return numbers

    [ map { $_ =~ /^(\d?)(\d{3})(\d{3})(\d{4})$/
              #? ($1 ? "$1 " : ''). "$2 $3 $4"
              ? "$2 $3 $4"
              : $_;
          }
        sort { $a <=> $b }
          map $_->{'phone'},
            @{ $data->{'item'} }
    ];

  } elsif ( $opt{'state'} ) { #return regions

    #[ map $_->{'region'}, @{ $data->{'item'} } ];
    my %regions = map { $_ => 1 } map $_->{'region'}, @{ $data->{'item'} };
    [ sort keys %regions ];

  #} else { #nothing passed, return states (provinces)
    # not really needed, we maintain our own list of provinces, but would
    #  help to hide the ones without availability (need to fix the selector too)
  }


}

#insert, delete, etc... handled with shellcommands

sub _export_insert {
  #my( $self, $svc_phone ) = (shift, shift);
}
sub _export_delete {
  #my( $self, $svc_phone ) = (shift, shift);
}

sub _export_replace  { ''; }
sub _export_suspend  { ''; }
sub _export_unsuspend  { ''; }

1;