Metabrik-Repository

 view release on metacpan or  search on metacpan

lib/Metabrik/Lookup/Service.pm  view on Meta::CPAN

#
# $Id$
#
# lookup::service Brik
#
package Metabrik::Lookup::Service;
use strict;
use warnings;

use base qw(Metabrik::File::Csv);

sub brik_properties {
   return {
      revision => '$Revision$',
      tags => [ qw(unstable iana) ],
      author => 'GomoR <GomoR[at]metabrik.org>',
      license => 'http://opensource.org/licenses/BSD-3-Clause',
      attributes => {
         datadir => [ qw(datadir) ],
         input => [ qw(input) ],
         _load => [ qw(INTERNAL) ],
      },
      attributes_default => {
         separator => ',',
         input => 'service-names-port-numbers.csv',
      },
      commands => {
         update => [ qw(output|OPTIONAL) ],
         load => [ qw(input|OPTIONAL) ],
         from_dec => [ qw(dec_number) ],
         from_hex => [ qw(hex_number) ],
         from_string => [ qw(service_string) ],
      },
      require_modules => {
         'Metabrik::Client::Www' => [ ],
         'Metabrik::File::Compress' => [ ],
         'Metabrik::File::Text' => [ ],
      },
   };
}

sub update {
   my $self = shift;
   my ($output) = @_;

   my $url = 'http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.csv';

   my $input = $self->input;
   my $datadir = $self->datadir;
   $output ||= $input;

   my $cw = Metabrik::Client::Www->new_from_brik_init($self) or return;
   my $files = $cw->mirror($url, "$output.gz", $datadir) or return;

   # If files were modified, we uncompress and save
   if (@$files > 0) {
      my $fc = Metabrik::File::Compress->new_from_brik_init($self) or return;
      $fc->uncompress($datadir."/$output.gz", $output, $datadir) or return;

      # We have to rewrite the CSV file, cause some entries are multiline.
      my $ft = Metabrik::File::Text->new_from_brik_init($self) or return;
      $ft->overwrite(1);
      $ft->append(0);
      my $text = $ft->read($datadir.'/'.$output) or return;

      # Some lines are split on multi-lines, we put into a single line
      # for each record.
      my @new = split(/\r\n/, $text);
      for (@new) {
         s/\n/ /g;
      }

      $ft->write(\@new, $datadir.'/'.$output);
   }

   return $datadir.'/'.$output;
}

sub load {
   my $self = shift;
   my ($input) = @_;

   $input ||= $self->datadir.'/'.$self->input;
   $self->brik_help_run_file_not_found('load', $input) or return;

   my $data = $self->read($input) or return;

   return $self->_load($data);
}

sub from_dec {
   my $self = shift;
   my ($dec) = @_;

   $self->brik_help_run_undef_arg('from_dec', $dec) or return;

   my $data = $self->_load || $self->load;
   if (! defined($data)) {
      return $self->log->error("from_dec: load failed");
   }

   for my $this (@$data) {



( run in 1.134 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )