blib/lib/Asterisk/LCR/Importer.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 257 CC 10 SCAR 14.4
CC
10
Cov
0%
rates
12.0
prefix
6.0
prefix_pos
6.0
CRAP 4.2
1 =head1 NAME
2  
3 Asterisk::LCR::Importer - Provider's rates importer base class
4  
5 =head1 SYNOPSIS
6  
7 Asterisk::LCR::Importer is just a base class. To write your own importer:
8  
9 package MyOwnImporter;
10 use base qw /Asterisk::LCR::Importer/;
11 use warnings;
12 use strict;
13
14 sub new
15 {
16 my $class = shift;
17 my $self = $class->SUPER::new (@_);
18 $self->{prefix_locale} = 'us'
19 $self->{prefix_position} = '0'
20 $self->{label_position} = '1'
21 $self->{rate_position} = '4'
22 $self->{first_increment_position} = '2'
23 $self->{increment_position} = '3'
24 $self->{connection_fee} = '0'
25 $self->{currency} = 'USD'
26 $self->{uri} = 'http://www.plainvoip.com/ratedump.php'
27 $self->{separator} = '(?:,|(?<=\d)\/(?=\d))'
28 return $self;
29 }
30
31 1;
32
33 __END__
34  
35  
36 In your config file:
37  
38 [import:myownprovider]
39 package = MyOwnProvider
40 dialstring = IAX2/jhiver@plainvoip/REPLACEME
41  
42
43 =head1 METHODS
44  
45 =cut
46 package Asterisk::LCR::Importer;
47 use base qw /Asterisk::LCR::Object/;
48 use Asterisk::LCR::Locale;
49 use Asterisk::LCR::Route;
50 use LWP::Simple;
51 use warnings;
52 use strict;
53  
54  
55 =head2 $self->uri();
56  
57 Returns the URI which $self should fetch.
58 If not overriden, returns $self->{uri}
59  
60 =cut
61 sub uri { shift->{uri} || 'http://example.com/YOU_FORGOT_TO_SPECIFY_THE_RATES_URI' }
62  
63  
64  
65 =head2 $self->target();
66  
67 Returns a 'target', i.e. a name file to fill with
68 the current Importer's rates.
69  
70 Returns $self->provider() + ".csv" by default.
71 If $self->{target} is defined, returns it instead.
72  
73 =cut
74 sub target
75 {
76 my $self = shift;
77 return $self->{target} || do { $self->provider() . '.csv' }
78 }
79  
80  
81 =head2 $self->get_data();
82  
83 Fetches the data contained in $self->uri(). Returns an
84 array of lines.
85  
86 =cut
87 sub get_data
88 {
89 my $self = shift;
90 my $data = LWP::Simple::get ($self->uri());
91 $data || die "Could not retrieve " . $self->uri();
92
93 my @data = split /\n\r|\r\n|\n|\r/, $data;
94 return \@data;
95 }
96  
97  
98 =head2 $self->separator();
99  
100 Returns the CSV separator, which is ',' by default.
101 If $self->{separator} is defined, returns it instead.
102  
103 =cut
104 sub separator { my $self = shift; $self->{separator} || '\,' }
105  
106  
107 =head2 $self->prefix ($rec);
108  
109 Extracts and returns the prefix from $rec.
110  
111 =cut
112 sub prefix
113 {
114 my $self = shift;
115 my $rec = shift;
116 my $pos = $self->prefix_pos();
117 my $loc = $self->prefix_locale();
118
119 my $res = $rec->[$pos];
120 if ($loc)
121 {
122 $res = $loc->local_to_global ($res);
123 $res = $loc->normalize ($res);
124 }
125
126 return $res;
127 }
128  
129  
130 =head2 $self->prefix_pos();
131  
132 Returns the position of the field which contains the prefix in the CSV data.
133 By default, returns 0.
134 If $self->{prefix_position} is defined, returns it instead.
135  
136 =cut
137 sub prefix_pos { my $self = shift; return defined $self->{prefix_position} ? $self->{prefix_position} : 0 }
138  
139  
140 =head2 $self->prefix_locale();
141  
142 Returns the locale which should be used for normalizing / translating the prefix.
143  
144 Returns undef unless $self->{prefix_locale} is defined.
145  
146 See L<Asterisk::LCR::Locale> for more details.
147  
148 =cut
149 sub prefix_locale
150 {
151 my $self = shift;
152 $self->{prefix_locale} || return;
153 $self->{prefix_locale_obj} ||= Asterisk::LCR::Locale->new ( $self->{prefix_locale} );
154 return $self->{prefix_locale_obj};
155 }
156  
157  
158 =head2 $self->label ($rec);
159  
160 Extracts and returns the label from $rec.
161  
162 =cut
163 sub label
164 {
165 my $self = shift;
166 my $rec = shift;
167 my $pos = $self->label_pos();
168 return $rec->[$pos];
169 }
170  
171  
172 =head2 $self->label_pos();
173  
174 Returns the position of the field which contains the prefix in the CSV data.
175 By default, returns 1.
176 If $self->{prefix_position} is defined, returns it instead.
177  
178 =cut
179 sub label_pos { my $self = shift; return defined $self->{label_position} ? $self->{label_position} : 1 }
180  
181  
182  
183 =head2 $self->provider();
184  
185 Returns a sensible string to designate the provider.
186  
187 For example, for 'VoIPJet' should return something called "voipjet'.
188  
189 By default, the result is derived from the domain name contained in $self->uri().
190 If $self->{provider} is defined, returns it instead.
191  
192 =cut
193 sub provider
194 {
195 my $self = shift;
196 $self->{provider} ||= do {
197 my $uri = $self->uri();
198 $uri =~ s/^.*\:\/\/(www\.)?//;
199 $uri =~ s/\..*//;
200 $uri =~ s/.*\///;
201 $uri;
202 };
203
204 return $self->{provider};
205 }
206  
207  
208 =head2 $self->currency();
209  
210 Returns the currency which this Importer's provider uses.
211  
212 Returns 'EUR' unless $self->{currency} is defined, in which
213 case it returns the latter instead.
214  
215 =cut
216 sub currency
217 {
218 my $self = shift;
219 return $self->{currency} || 'EUR';
220 }
221  
222  
223 =head2 $self->rate ($rec);
224  
225 Extracts and return the rate from $rec.
226  
227 =cut
228 sub rate
229 {
230 my $self = shift;
231 my $rec = shift;
232 my $pos = $self->rate_pos();
233 return $rec->[$pos];
234 }
235  
236  
237 =head2 $self->rate_pos();
238  
239 Returns the position of the field which contains the rate in the CSV data.
240 By default, returns 2.
241 If $self->{rate_position} is defined, returns it instead.
242  
243 =cut
244 sub rate_pos { my $self = shift; return defined $self->{rate_position} ? $self->{rate_position} : 2 }
245  
246  
247 =head2 $self->connection_fee ($rec);
248  
249 Extracts and returns the connection_fee for $rec.
250 If $self->{connection_fee} is defined, returns it instead.
251  
252 =cut
253 sub connection_fee
254 {
255 my $self = shift;
256 defined $self->{connection_fee} and return $self->{connection_fee};
257
258 my $rec = shift;
259 my $pos = $self->connection_fee_pos();
260 return $rec->[$pos];
261 }
262  
263  
264 =head2 $self->connection_fee_pos();
265  
266 Returns the position of the field which contains the connection fee in the CSV data.
267 By default, returns 3.
268  
269 =cut
270 sub connection_fee_pos { my $self = shift; return defined $self->{connection_fee_position} ? $self->{connection_fee_position} : 3 }
271  
272  
273 =head2 $self->first_increment ($rec);
274  
275 Extracts and returns the first_increment for $rec.
276 If $self->{first_increment} is defined, returns it instead.
277  
278 =cut
279 sub first_increment
280 {
281 my $self = shift;
282 defined $self->{first_increment} and return $self->{first_increment};
283 my $rec = shift;
284 my $pos = $self->first_increment_pos();
285 return $rec->[$pos];
286 }
287  
288  
289 =head2 $self->first_increment_pos();
290  
291 Returns the position of the field which contains the first increment in the CSV data.
292 By default, returns 3.
293  
294 =cut
295 sub first_increment_pos { my $self = shift; return defined $self->{first_increment_position} ? $self->{first_increment_position} : 4 }
296  
297  
298 =head2 $self->increment ($rec);
299  
300 Extracts and returns the increment for $rec.
301 If $self->{increment} is defined, returns it instead.
302  
303 =cut
304 sub increment
305 {
306 my $self = shift;
307 defined $self->{increment} and return $self->{increment};
308
309 my $rec = shift;
310 my $pos = $self->increment_pos();
311 return $rec->[$pos];
312 }
313  
314  
315 =head2 $self->increment_pos();
316  
317 Returns the position of the field which contains the increment in the CSV data.
318 By default, returns 3.
319  
320 =cut
321 sub increment_pos { my $self = shift; return defined $self->{increment_position} ? $self->{increment_position} : 5 }
322  
323  
324 =head2 $self->filter();
325  
326 Returns a filter which matches all the CSV lines which are valid.
327 Returns $self->{filter}, or '^\d+,' by default
328  
329 =cut
330 sub filter { return shift->{filter} || '^\d+,' }
331  
332  
333 =head2 $self->rates();
334  
335 Imports and returns this Importer's rates.
336  
337 =cut
338 sub rates
339 {
340 my $self = shift;
341 $self->{rates} and return $self->{rates};
342
343 my $data = $self->get_data();
344 my $filter = $self->filter();
345 my $comma = $self->separator();
346  
347 my $locale = Config::Mini::get ("dialer", "locale");
348 my $loc = $locale ? Asterisk::LCR::Locale->new ($locale) : undef;
349 my $res = {};
350  
351 for (@{$data})
352 {
353 /$filter/ or do {
354 print "IGNORED: $_ (doesn't match /$filter/)\n";
355 next;
356 };
357 my $rec = [ split /\s*$comma\s*/, $_ ];
358 my $pfx = $self->prefix ($rec);
359 $pfx = $loc->normalize ($pfx) if ($loc);
360 $res->{$pfx} = Asterisk::LCR::Route->new (
361 prefix => $pfx,
362 label => $self->label ($rec),
363 provider => $self->provider ($rec),
364 currency => $self->currency ($rec),
365 rate => $self->rate ($rec),
366 connection_fee => $self->connection_fee ($rec),
367 first_increment => $self->first_increment ($rec),
368 increment => $self->increment ($rec),
369 );
370 }
371
372 $self->{rates} = $res;
373 return $res;
374 }
375  
376  
377 =head2 $self->prefixes();
378  
379 Returns a list of all available prefixes.
380  
381 =cut
382 sub prefixes
383 {
384 my $self = shift;
385 my $rates = $self->rates();
386 return keys %{$rates};
387 }
388  
389  
390 =head2 $self->fetch_rate ($prefix);
391  
392 Returns a list of rates matching $prefix exactly.
393  
394 =cut
395 sub fetch_rate
396 {
397 my $self = shift;
398 my $prefix = shift;
399 my $rates = $self->rates();
400 return $rates->{$prefix};
401 }
402  
403  
404 =head2 $self->search_rate ($prefix);
405  
406 Say $prefix = 12345. If 12345 has a list of rates, return the list.
407 If 1234 has a list of rates, return the list.
408 If 123 has a list of rates, return the list.
409 etc.
410  
411 =cut
412 sub search_rate
413 {
414 my $self = shift;
415 my $prefix = shift;
416  
417 defined $prefix or return;
418 $prefix ne '' or return;
419  
420 return $self->fetch_rate ($prefix) || do {
421 chop ($prefix);
422 $self->search_rate ($prefix);
423 };
424 }
425  
426  
427 1;
428  
429  
430 __END__