File Coverage

blib/lib/Alien/InteractiveBrokers.pm
Criterion Covered Total %
statement 24 58 41.3
branch 0 20 0.0
condition n/a
subroutine 7 11 63.6
pod 5 5 100.0
total 36 94 38.3


line stmt bran cond sub pod time code
1             package Alien::InteractiveBrokers;
2             #
3             # Alien::InteractiveBrokers - Provides config info for IB API files
4             #
5             # Copyright (c) 2010-2012 Jason McManus
6             #
7             # Full POD documentation after __END__
8             #
9              
10 2     2   106500 use File::Spec::Functions qw( catdir catfile );
  2         1181  
  2         505  
11 2     2   16 use Carp qw( croak confess );
  2         28  
  2         189  
12 2     2   12 use strict;
  2         9  
  2         66  
13 2     2   12 use warnings;
  2         3  
  2         72  
14              
15             ###
16             ### Variables
17             ###
18              
19 2     2   304 use vars qw( @ISA @EXPORT_OK $VERSION $TRUE $FALSE );
  2         6  
  2         303  
20              
21             BEGIN {
22 2     2   13 require Exporter;
23 2         47 @ISA = qw( Exporter );
24 2         6 @EXPORT_OK = qw( path includes classpath version );
25 2         3337 $VERSION = '9.6602';
26             }
27              
28             *TRUE = \1;
29             *FALSE = \0;
30              
31             my $versions = {
32             '9.64' => {
33             desc => 'IB API 9.64',
34             url => 'http://www.interactivebrokers.com/download/twsapi_unixmac_964.jar',
35             },
36             '9.65' => {
37             desc => 'IB API 9.65',
38             url => 'http://www.interactivebrokers.com/download/twsapi_unixmac_965.jar',
39             },
40             '9.66' => {
41             desc => 'IB API 9.66',
42             url => 'http://www.interactivebrokers.com/download/twsapi_unixmac_966.jar',
43             },
44             '9.65' => {
45             desc => 'IB API 9.67 Beta',
46             url => 'http://www.interactivebrokers.com/download/twsapi_unixmac_967.jar',
47             },
48             };
49              
50             my $DEF_API_VERSION = '9.66';
51              
52             # Global cache; either points to object ref, or just used as is.
53             # XXX: This may explode upon multiple Alien::SWIG objects being created,
54             # but we'll worry about that if anyone ever needs to actually do that.
55             our $CACHE = {}; # our, for testing
56              
57             ###
58             ### Constructor
59             ###
60              
61             sub new
62             {
63 1     1 1 18 my $class = shift;
64              
65             # Set up a default object
66 1         3 my $self = {};
67              
68             # Instantiate the object. sort of.
69 1         5 bless( $self, $class );
70              
71             # Direct the global cache to us (see note above)
72 1         3 $CACHE = $self;
73              
74 1         10 return( $self );
75             }
76              
77             ###
78             ### Methods
79             ###
80              
81             sub path
82             {
83 0 0   0 1   return( $CACHE->{path} )
84             if( exists( $CACHE->{path} ) );
85              
86 0           my $base = $INC{ join( '/', 'Alien', 'InteractiveBrokers.pm' ) };
87 0           $base =~ s{\.pm$}{};
88 0           my $path = catfile( $base, 'IBJts' );
89              
90 0 0         croak( "Path $path doesn't appear to contain the IB API installation;" .
91             " please re-install Alien::InteractiveBrokers." )
92             unless( -d $path );
93              
94 0           $CACHE->{path} = $path;
95              
96 0           return( $path );
97             }
98              
99             sub includes
100             {
101 0     0 1   my @includes;
102              
103 0 0         if( exists( $CACHE->{includes} ) )
104             {
105 0           @includes = @{ $CACHE->{includes} };
  0            
106             }
107             else
108             {
109 0           my $base = path();
110              
111 0           for( qw( Shared PosixSocketClient ) )
112             {
113 0           my $incpath = catfile( $base, 'cpp', $_ );
114 0 0         croak( "Cannot find $_ include directory under $base; please" .
115             " re-install Alien::InteractiveBrokers" )
116             unless( -d $incpath );
117 0           push( @includes, '-I' . $incpath );
118             }
119              
120 0           $CACHE->{includes} = \@includes;
121             }
122              
123             return( wantarray
124             ? @includes
125 0 0         : join( ' ', @includes ) );
126             }
127              
128             sub classpath
129             {
130 0 0   0 1   return( $CACHE->{classpath} )
131             if( exists( $CACHE->{classpath} ) );
132              
133 0           my $jarfile = catfile( path(), 'jtsclient.jar' );
134              
135 0 0         croak( "Cannot find jtsclient.jar; please re-install" .
136             " Alien::InteractiveBrokers." )
137             unless( -f $jarfile );
138              
139 0           $CACHE->{classpath} = $jarfile;
140              
141 0           return( $jarfile );
142             }
143              
144             sub version
145             {
146 0 0   0 1   return( $CACHE->{version} )
147             if( exists( $CACHE->{version} ) );
148              
149 0           my $verfile = catfile( path(), 'API_VersionNum.txt' );
150 0 0         open my $fd, '<', $verfile or
151             croak( "Cannot read API version file; please re-install" .
152             " Alien::InteractiveBrokers." );
153 0           my $contents = do { local $/; <$fd> };
  0            
  0            
154 0           close( $fd );
155              
156 0           my( $vernum ) = ( $contents =~ m/API_Version=([\d\.]*)/mi );
157 0 0         croak( "Could not read version number; please" .
158             " reinstall Alien::InteractiveBrokers." )
159             unless( defined( $vernum ) );
160              
161 0           $CACHE->{version} = $vernum;
162              
163 0           return( $vernum );
164             }
165              
166             1;
167              
168             __END__