Finance-Quote
view release on metacpan or search on metacpan
lib/Finance/Quote/ASX.pm view on Meta::CPAN
$status = 0;
$error = "Cannot parse content from ASX server '$url'. Expected a top level JSON element named data.";
### ASX.pm Error: $error
return $status, $error;
}
# Map the Finance::Quote labels (left) to the corresponding ASX labels (right)
%label_map = (
'name' => 'displayName',
'ask' => 'priceAsk',
'bid' => 'priceBid',
'net' => 'priceChange',
'p_change' => 'priceChangePercent',
'last' => 'priceLast',
'type' => 'securityType',
'volume' => 'volume',
);
process_asx_data($symbol, $data->{data}, \%label_map, $info);
return 1, '';
}
# Internal function to fetch, validate, and decode data from an ASX URL using LWP User Agent
# Handle any errors
sub get_asx_data {
my ($url, $ua) = @_;
my($data, $error, $json, $response, $status);
### ASX.pm Retrieving data from ASX URL: $url
$response = $ua->get($url);
if (! $response->is_success) {
$status = 0;
$error = "Unable to fetch data from the ASX server '$url'. Status: " . $response->status_line;
### ASX.pm Error: $error
return $status, $error, undef;
}
if ($response->header('content-type') !~ m|application/json|i) {
$status = 0;
$error = "Invalid content-type from ASX server '$url'. Expected: application/json, received: " . $response->header('content-type');
### ASX.pm Error: $error
return $status, $error, undef;
}
$json = $response->content;
# The JSON module will croak on errors, so use eval to trap this.
$data = eval{ decode_json($json) };
if ($@) {
$status = 0;
$error = "Failed to parse JSON data from ASX server '$url'. Error: '$@'.";
### ASX.pm Error: $error
return $status, $error, undef;
}
# Return valid, decoded data
$status = 1;
return $status, $error, $data;
}
# Internal function to push the ASX data elements into the Finance::Quote structure (%info)
sub process_asx_data {
my ($symbol, $data, $label_map, $info) = @_;
foreach my $label (sort(keys %{$label_map})) {
if ((exists $data->{$label_map->{$label}}) &&
(defined $data->{$label_map->{$label}})) {
# Concatenate Primary and Alternate Names
if (($label eq 'name') &&
(exists $info->{$symbol, $label}) &&
(uc($info->{$symbol, $label}) ne uc($data->{$label_map->{$label}}))) {
$info->{$symbol, $label} = $data->{$label_map->{$label}} . ' ' . $info->{$symbol, $label};
}
# Overwrite all other labels
else {
$info->{$symbol, $label} = $data->{$label_map->{$label}};
}
### ASX.pm Mapped ASX data element to Finance-Quote: sprintf("%-22s%-15s%-s", $label_map->{$label}, $label, $data->{$label_map->{$label}})
}
else {
$info->{$symbol,$label} = '';
}
}
return;
}
1;
__END__
=head1 NAME
Finance::Quote::ASX - Obtain quotes from the Australian Stock Exchange.
=head1 SYNOPSIS
use Finance::Quote;
$q = Finance::Quote->new;
%stockinfo = $q->fetch("asx","BHP"); # Only query ASX.
%stockinfo = $q->fetch("australia","BHP"); # Failover to other sources OK.
=head1 DESCRIPTION
This module obtains information from the Australian Stock Exchange
http://www.asx.com.au/. Data for all Australian listed securities and indices
is available. Indexes start with the letter 'X'. For example, the
All Ordinaries is "XAO". But some securities also start with the letter 'X'.
This module is loaded by default on a Finance::Quote object. It's
also possible to load it explicitly by placing "ASX" in the argument
list to Finance::Quote->new().
( run in 0.735 second using v1.01-cache-2.11-cpan-5837b0d9d2c )