Alien-InteractiveBrokers

 view release on metacpan or  search on metacpan

inc/MY/Build.pm  view on Meta::CPAN

package MY::Build;
#
#   Alien::InteractiveBrokers -- Installer to download or install IB API
#
#   Copyright (c) 2010-2012 Jason McManus
#

use base qw( Module::Build );
use strict;
use warnings;
use vars qw( $VERSION );
BEGIN {
    $VERSION = '9.6602';
}

sub ACTION_code {
    my $self = shift;
    $self->SUPER::ACTION_code;
    $self->fetch_ibapi();
    $self->install_ibapi();
}

sub ibapi_archive {
    return 'twsapi_unixmac_966.jar';
}

sub ibapi_dir {
    return 'IBJts';
}

sub ibapi_target_dir {
    return 'blib/lib/Alien/InteractiveBrokers';
}

sub ibapi_url {
    my $self = shift;
    return 'http://www.interactivebrokers.com/download/'
            . $self->ibapi_archive();
}

sub fetch_ibapi {
    my $self = shift;
    return if( -f $self->ibapi_archive() );
    $|=1;

    print 'Local copy of ', $self->ibapi_archive(), " not found.\n";
    print 'GET ', $self->ibapi_url(), '... ';

    # Grab the file
    require HTTP::Tiny;
    my $http = HTTP::Tiny->new( timeout => 30 );
    my $response = $http->get(
        $self->ibapi_url(),
        {
            headers => {
                Connection => 'close',
                Accept     => '*/*',
            }
        }
    );

    unless( $response->{success} )
    {
        my $content = ( exists( $response->{content} ) and
                        defined( $response->{content} ) and
                        length( $response->{content} ) )
                          ? substr( $response->{content}, 0, 8*1024 )
                          : "empty";
        chomp $content;
        die sprintf( "\nUnable to fetch archive: %s %s; Content was%s\n",
                     $response->{status}, $response->{reason},
                     ":\n'" . $content . "'\n" );
    }

    # Write it to disk
    open my $fd, '>', $self->ibapi_archive()
        or die "\nCannot write to " . $self->ibapi_archive() . ": $!";
    binmode( $fd );



( run in 0.575 second using v1.01-cache-2.11-cpan-39bf76dae61 )