Alien-InteractiveBrokers

 view release on metacpan or  search on metacpan

lib/Alien/InteractiveBrokers.pm  view on Meta::CPAN

use Carp qw( croak confess );
use strict;
use warnings;

###
### Variables
###

use vars qw( @ISA @EXPORT_OK $VERSION $TRUE $FALSE );

BEGIN {
    require Exporter;
    @ISA        = qw( Exporter );
    @EXPORT_OK  = qw( path includes classpath version );
    $VERSION    = '9.6602';
}

*TRUE     = \1;
*FALSE    = \0;

my $versions = {
    '9.64' => {
        desc => 'IB API 9.64',
        url  => 'http://www.interactivebrokers.com/download/twsapi_unixmac_964.jar',
    },
    '9.65' => {
        desc => 'IB API 9.65',
        url  => 'http://www.interactivebrokers.com/download/twsapi_unixmac_965.jar',
    },
    '9.66' => {
        desc => 'IB API 9.66',
        url  => 'http://www.interactivebrokers.com/download/twsapi_unixmac_966.jar',
    },
    '9.65' => {
        desc => 'IB API 9.67 Beta',
        url  => 'http://www.interactivebrokers.com/download/twsapi_unixmac_967.jar',
    },
};

my $DEF_API_VERSION = '9.66';

# Global cache; either points to object ref, or just used as is.
# XXX: This may explode upon multiple Alien::SWIG objects being created,
# but we'll worry about that if anyone ever needs to actually do that.
our $CACHE = {};        # our, for testing

###
### Constructor
###

sub new
{
    my $class = shift;

    # Set up a default object
    my $self = {}; 

    # Instantiate the object.  sort of.
    bless( $self, $class );

    # Direct the global cache to us (see note above)
    $CACHE = $self;

    return( $self );
}

###
### Methods
###

sub path
{
    return( $CACHE->{path} )
        if( exists( $CACHE->{path} ) );

    my $base = $INC{ join( '/', 'Alien', 'InteractiveBrokers.pm' ) };
    $base =~ s{\.pm$}{};
    my $path = catfile( $base, 'IBJts' );

    croak( "Path $path doesn't appear to contain the IB API installation;" .
           " please re-install Alien::InteractiveBrokers." )
        unless( -d $path );

    $CACHE->{path} = $path;

    return( $path );
}

sub includes
{
    my @includes;

    if( exists( $CACHE->{includes} ) )
    {
        @includes = @{ $CACHE->{includes} };
    }
    else
    {
        my $base = path();

        for( qw( Shared PosixSocketClient ) )
        {
            my $incpath = catfile( $base, 'cpp', $_ );
            croak( "Cannot find $_ include directory under $base; please" .
                   " re-install Alien::InteractiveBrokers" )
                unless( -d $incpath );
            push( @includes, '-I' . $incpath );
        }

        $CACHE->{includes} = \@includes;
    }

    return( wantarray
              ? @includes
              : join( ' ', @includes ) );
}

sub classpath
{
    return( $CACHE->{classpath} )
        if( exists( $CACHE->{classpath} ) );



( run in 1.073 second using v1.01-cache-2.11-cpan-e1769b4cff6 )