Business-Shipping-DataTools

 view release on metacpan or  search on metacpan

lib/Business/Shipping/DataTools.pm  view on Meta::CPAN

=item * do_update

=cut

sub do_update
{
    my ( $self ) = @_;
    
    if ( not $self->data_dir ) {
        $self->data_dir( Business::Shipping::Config::data_dir() );
    }
    
    if ( $self->update ) {
        $self->download( 1 );
        $self->unzip( 1 );
        $self->convert( 1 );
    }
    
    debug "data_dir = " . $self->data_dir();
    
    if ( $self->download ) {
        print "Downloading, please wait...\n";
        $self->do_download;
    }
    
    if ( $self->unzip ) {
        print "Unzipping, please wait...\n";
        $self->do_unzip;
    }
    
    if ( $self->convert ) {
        print "Converting, please wait...\n";
        $self->do_convert_data;
    }
    
    if ( $self->create_bin ) {
        print "Creating storables, please wait...\n";
        $self->do_create_bin if $self->create_bin;
    }
    
    print "Done.\n";
    
    return;
}
    
    
=item * download_to_file( $url, $file )

=cut

sub download_to_file
{
    my ( $url, $file ) = @_;
    trace "( $url, $file )";
    
    return unless $url and $file;
    
    eval {
        use LWP::UserAgent;
        my $ua = LWP::UserAgent->new;
        my $req = HTTP::Request->new(GET => $url);
        open( NEW_ZONE_FILE, "> $file" );
        print( NEW_ZONE_FILE $ua->request($req)->content() );        
        close( NEW_ZONE_FILE );
    };
    warn $@ if $@;
    
    return;
}

=item * _unzip_file( $zipName, $destination_directory )

=cut

# Extracts all files from the given zip

sub _unzip_file
{
    my ( $zipName, $destination_directory ) = @_;
    $destination_directory ||= './';
    
    my $zip = Archive::Zip->new();
    my $status = $zip->read( $zipName );
    if ( $status != AZ_OK )  {
        my $error = "Read of $zipName failed";
        #$self->user_error( $error );
        logdie $error;
    }
    if ( $@ ) { logdie "_unzip_file error: $@"; }
    
    $zip->extractTree( '', $destination_directory );
    
    debug( "Done extracting." );
    
    return;
}

=item * filename_only( $path )

  Was filename_only.
  
=cut

sub filename_only_old 
{
    my ( $base, $path ) = fileparse( $_[ 0 ] );
    return $path . $base;
}

=item * split_dir_file( $path )

=cut

# Return ( directory_path, file_name ) from any path.
# TODO: Use correct File:: Module, and be Windows-compatible

sub split_dir_file
{
    my $path = shift;
    
    my @path_components = split( '/', $path );



( run in 0.470 second using v1.01-cache-2.11-cpan-6aa56a78535 )