Finance-Bank-Schwab

 view release on metacpan or  search on metacpan

lib/Finance/Bank/Schwab.pm  view on Meta::CPAN

package Finance::Bank::Schwab;

# ABSTRACT: Check your account balances at Charles Schwab

use strict;
use warnings;

use Carp;
use WWW::Mechanize;
use HTML::TableExtract;

our $VERSION = '2.03';

our $ua = WWW::Mechanize->new(
    env_proxy  => 1,
    keep_alive => 1,
    timeout    => 30,
    cookie_jar => {},
);

# Debug logging:
# $ua->default_header( 'Accept-Encoding' => scalar HTTP::Message::decodable() );
# $ua->add_handler( "request_send",  sub { shift->dump; return } );
# $ua->add_handler( "response_done", sub { shift->dump; return } );

sub check_balance {
    my ( $class, %opts ) = @_;

    my $content = retrieve_summary_page(%opts);

    my $te = HTML::TableExtract->new(
        headers => [
            'Account',                       'Name',
            '(?:Value|Available\s+Balance)', '(?:Cash|Balance\sOwed)'
        ],
        keep_html => 1,
        ## decode    => 0,
    );

    {    # HTML::TableExtract warns about undef value with keep_html option
        $SIG{__WARN__} = sub {
            warn @_ unless $_[0] =~ /uninitialized value in subroutine entry/;
        };
        $te->parse($content);
    }

    my @accounts;
    for my $ts ( $te->tables ) {

        # print "Table (", join( ',', $ts->coords ), "):\n";

        for my $row ( $ts->rows ) {
            next if $row->[1] =~ /Totals/;    # Skip total rows

            ## strip_superscript( @$row[0..3] );
            strip_html( @$row[ 0 .. 3 ] );
            trim_whitespace(@$row);
            remove_currency_symbol( @$row[ 2 .. 3 ] );
            $row->[0] =~ s{^([\d.-]+).*$}{$1}s;    # Strip all but num from name

            # If this is an account with positions, go grab that data.
            # If not it is probably a bank account, ignore for now.
            my @positions =
              ( $row->[0] =~ /\d{4}-\d{4}/ )
              ? get_positions( $row->[0], %opts )
              : ();

            push @accounts, (
                bless {
                    cash       => $row->[3],
                    balance    => $row->[2],
                    name       => $row->[1],
                    sort_code  => $row->[1],
                    account_no => $row->[0],
                    statement  => undef,
                    positions  => \@positions,



( run in 3.557 seconds using v1.01-cache-2.11-cpan-302cb4679cc )