Finance-Quote-IEX

 view release on metacpan or  search on metacpan

lib/Finance/Quote/IEX.pm  view on Meta::CPAN

    return (
        iex    => \&iex,
        usa    => \&iex,
        nasdaq => \&iex,
        nyse   => \&iex,
    );
}

sub labels {
    my @labels = qw/
        name
        last
        date
        isodate
        time
        net
        p_change
        volume
        close
        open
        year_range
        pe
        cap
        exchange
        method
        price
        currency
        /;
    return (
        iex    => \@labels,
        usa    => \@labels,
        nasdaq => \@labels,
        nyse   => \@labels,
    );
}

sub iex {
    my $quoter = shift;
    my @stocks = @_;

    my $iex_url  = 'https://api.iextrading.com/1.0/stock/%s/quote';
    my $errormsg = 'Error retrieving quote for "%s": GET "%s" resulted in'
        . ' HTTP response %d (%s)';

    my $ua = $quoter->user_agent();
    my %info;

    foreach my $symbol (@stocks) {
        my $url = sprintf( $iex_url, $symbol );
        my $response = $ua->get($url);

        if ( !$response->is_success ) {
            my $code = $response->code;
            my $desc = status_message($code);
            $info{ $symbol, 'success' } = 0;
            $info{ $symbol, 'errormsg' }
                = sprintf( $errormsg, $symbol, $url, $code, $desc );
            next;
        }

        my $data = decode_json( $response->decoded_content );

        if ( !defined $data->{latestPrice} ) {
            my $code = $response->code;
            my $desc = status_message($code);
            $info{ $symbol, 'success' }  = 0;
            $info{ $symbol, 'errormsg' } = sprintf(
                'Error retrieving quote for "%s":'
                    . ' no price found in response data',
                $symbol
            );
            next;
        }

        if ( !defined $data->{latestUpdate} ) {
            my $code = $response->code;
            my $desc = status_message($code);
            $info{ $symbol, 'success' }  = 0;
            $info{ $symbol, 'errormsg' } = sprintf(
                'Error retrieving quote for "%s":'
                    . ' no date found in response data',
                $symbol
            );
            next;
        }

        $info{ $symbol, 'success' }  = 1;
        $info{ $symbol, 'method' }   = 'iex';
        $info{ $symbol, 'source' }   = 'Finance::Quote::IEX';
        $info{ $symbol, 'currency' } = 'USD';
        $info{ $symbol, 'symbol' }   = $data->{symbol};
        $info{ $symbol, 'name' }
            = $symbol . ' (' . $data->{companyName} . ')';
        $info{ $symbol, 'last' }     = $data->{latestPrice};
        $info{ $symbol, 'price' }    = $data->{latestPrice};
        $info{ $symbol, 'net' }      = $data->{change};
        $info{ $symbol, 'p_change' } = $data->{changePercent};
        $info{ $symbol, 'volume' }   = $data->{latestVolume};
        $info{ $symbol, 'close' }    = $data->{close};
        $info{ $symbol, 'open' }     = $data->{open};
        $info{ $symbol, 'year_range' }
            = $data->{week52Low} . ' - ' . $data->{week52High};
        $info{ $symbol, 'pe' }       = $data->{peRatio};
        $info{ $symbol, 'cap' }      = $data->{marketCap};
        $info{ $symbol, 'exchange' } = $data->{exchange};

        # The Finance::Quote documentation indicates that the date shouldn't
        # be parsed, but store_date does not support epoch time.
        my $dt
            = DateTime->from_epoch( epoch => $data->{latestUpdate} / 1000 );
        $info{ $symbol, 'time' }    = $dt->hms;
        $info{ $symbol, 'date' }    = $dt->strftime('%m/%d/%y');
        $info{ $symbol, 'isodate' } = $dt->ymd;
    }

    return wantarray() ? %info : \%info;
}

1;

__END__



( run in 0.416 second using v1.01-cache-2.11-cpan-354807fb38d )