Finance::Bank::IE - shared functions for the Finance::Bank::IE module tree


Finance-Bank-IE documentation Contained in the Finance-Bank-IE distribution.

Index


Code Index:

NAME

Top

Finance::Bank::IE - shared functions for the Finance::Bank::IE module tree

DESCRIPTION

Top

This module implements a few shared functions for Finance::Bank::IE::*

* $self->reset()
 Take necessary steps to reset the object to a pristine state, such as deleting cached configuration, etc.

* $self->_agent
 Return the WWW::Mechanize object currently in use, or create one if
 no such object exists.

* $self->cached_config( [config] )
  Get or set the cached config

* $class = $self->_get_class()
 Return the bottom level class of $self

* $scrubbed = $self->_scrub_page( $content )
 Scrub the supplied content for PII.

* $self->_save_page()
 Save the current page if $ENV{SAVEPAGES} is set. The pages are
 anonymised before saving so that they can be used as test pages
 without fear of divulging any information.

* $self->_dprintf( ... )
 Print to STDERR using printf formatting if $ENV{DEBUG} is set.


Finance-Bank-IE documentation Contained in the Finance-Bank-IE distribution.
package Finance::Bank::IE;

use File::Path;
use WWW::Mechanize;
use Carp qw( confess );

use strict;
use warnings;

# Class state. Each of these is keyed by the hash value of $self to
# provide individual class-level variables. Ideally I'd just hack the
# namespace of the subclass, though.
my %agent;
my %cached_config;

sub reset {
    my $self = shift;

    $agent{$self} = undef;
    $cached_config{$self} = undef;;

    return 1;
}

sub _agent {
    my $self = shift;

    if ( !$agent{$self} ) {
        $agent{$self} = WWW::Mechanize->new( env_proxy => 1,
                                             autocheck => 0,
                                             keep_alive => 10 )
          or confess( "can't create agent" );
        $agent{$self}->quiet( 0 );
    }

    $agent{$self};
}

sub cached_config {
    my ( $self, $config ) = @_;
    if ( defined( $config )) {
        $cached_config{$self} = $config;
    }

    return $cached_config{$self};
}

sub _get_class {
    my $self = shift;
    my $class = ref( $self );

    if ( !$class ) {
        $class = $self;
    }

    # clean it up
    my $basename = ( split /::/, $class )[-1];
    $basename =~ s/\.[^.]*$//;

    $basename;
}

sub _scrub_page {
    my ( $self, $content ) = @_;

    return $content;
}

sub _save_page {
    my $self = shift;
    return unless $ENV{SAVEPAGES};

    # get a filename from the agent
    my $res = $self->_agent()->response();
    my $filename = $res->request->uri();
    $filename =~ s@^.*/@@;
    if ( !$filename ) {
        $filename = "index.html";
    }

    # embed the code if it's a failed page
    if ( !$res->is_success()) {
        $filename = $res->code() . "-$filename";
    }

    my $path = 'data/savedpages/' . $self->_get_class();
    mkpath( [ $path ], 0, 0700 );
    $filename = "$path/$filename";

    $self->_dprintf( "writing data to $filename\n" );

    # we'd like to anonymize this content before saving it.
    my $content = $self->_agent()->content();

    $content = $self->_scrub_page( $content );

    if ( open( my $FILEHANDLE, ">", $filename )) {
        binmode $FILEHANDLE, ':utf8';
        print $FILEHANDLE $content;
        close( $FILEHANDLE );
    } else {
        warn "Failed to create $filename: $!";
    }
}

sub _dprintf {
    my $self = shift;
    binmode( STDERR, ':utf8' );
    print STDERR @_ if $ENV{DEBUG};
}

1;