| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Asterisk::LCR::Importer::CanonicalCSV | ||
| 4 | |||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | use Asterisk::LCR::Importer::CanonicalCSV; | ||
| 9 | my $import = Asterisk::LCR::Importer::CanonicalCSV->new ( file => 'myfile.csv' ); | ||
| 10 | my $rates = $import->rates(); | ||
| 11 | |||
| 12 | =cut | ||
| 13 | package Asterisk::LCR::Importer::CanonicalCSV; | ||
| 14 | use base qw /Asterisk::LCR::Importer/; | ||
| 15 | use Asterisk::LCR::Route; | ||
| 16 | use Asterisk::LCR::Locale; | ||
| 17 | use warnings; | ||
| 18 | use strict; | ||
| 19 | |||
| 20 | |||
| 21 | sub validate | ||
| 22 | { | ||
| 23 | my $self = shift; | ||
| 24 | |||
| 25 | my $file = $self->target(); | ||
| 26 | -f $file or do { | ||
| 27 | die 'asterisk/lcr/importer/canonicalcsv/file/inexistent'; | ||
| 28 | return 0; | ||
| 29 | }; | ||
| 30 | |||
| 31 | return 1; | ||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | sub get_data | ||
| 36 | { | ||
| 37 | my $self = shift; | ||
| 38 | my $file = $self->target(); | ||
| 39 | my @res = (); | ||
| 40 | |||
| 41 | open FP, "<$file" or die "Cannot read-open $file. Reason: $!"; | ||
| 42 | $_ = <FP>; | ||
| 43 | while (<FP>) | ||
| 44 | { | ||
| 45 | chomp; | ||
| 46 | push @res, $_; | ||
| 47 | } | ||
| 48 | close FP; | ||
| 49 | |||
| 50 | return \@res; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | ## | ||
| 55 | # $self->rates(); | ||
| 56 | # --------------- | ||
| 57 | # Returns a { <international_code> => <rate> } hash reference | ||
| 58 | ## | ||
| 59 | sub rates | ||
| 60 | { | ||
| 61 | my $self = shift; | ||
| 62 | my $locale = Asterisk::LCR::Locale->new ("fix_intl"); | ||
| 63 | $self->{rates} ||= do { | ||
| 64 | my $data = $self->get_data(); | ||
| 65 | my $res = {}; | ||
| 66 | for (@{$data}) | ||
| 67 | { | ||
| 68 | my ($prefix, $label, $provider, $currency, $rate, $connection_fee, $first_increment, $increment) = split /\s*,\s*/, $_; | ||
| 69 | $prefix = $locale->local_to_global ($prefix); | ||
| 70 | $res->{$prefix} = Asterisk::LCR::Route->new ( | ||
| 71 | connection_fee => $connection_fee, | ||
| 72 | first_increment => $first_increment, | ||
| 73 | increment => $increment, | ||
| 74 | currency => $currency, | ||
| 75 | rate => $rate, | ||
| 76 | provider => $provider, | ||
| 77 | label => $label, | ||
| 78 | prefix => $prefix, | ||
| 79 | ); | ||
| 80 | } | ||
| 81 | $res; | ||
| 82 | }; | ||
| 83 | |||
| 84 | return $self->{rates}; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | 1; | ||
| 89 | |||
| 90 | |||
| 91 | __END__ |