package MetalData;
use strict;
use warnings;
use LWP::Simple;
use CGI::Carp qw/croak/;
# constructor
sub new {
my $class = shift;
my $self = {@_};
bless $self, $class;
} # new
sub get_metal_data {
my $self = shift;
$self->set_exchange_price();
#$self->set_bullion_price();
return $self->metal_data();
} # get_metal_data
sub set_exchange_price {
my $self = shift;
my $content = get('http://www.goldmoney.com');
if ($content) {
# get single html-parts, which includes the price of metals
if($content =~ /
.+>Gold:(.+?)><\/table>.*.+>Silver:(.+?)><\/table>.*.+>Platinum:(.+?)><\/table>/s) {
$self->price_breakdown('au', $1);
$self->price_breakdown('ag', $2);
$self->price_breakdown('pt', $3);
} else {
croak "Cannot parse goldmoney.com senseful.\n";
}
} else {
croak "Cannot fetch goldmoney.com.\n";
}
} # set_exchange_price
sub price_breakdown {
my $self = shift;
my ($metal, $html) = @_;
$html =~ /(€.*)?€(.+?)<\/p>/s;
my ($price, undef) = split /\//, $2; # price in html has an '/unit' at the end
$self->metal_data($metal, $price);
} # price_breakdown
sub metal_data {
my $self = shift;
my $metal = shift;
if (@_) {
$self->{$metal}{'oz_price'} = shift;
$self->{$metal}{'oz_price'}{'ts'} = time();
} else {
my @data;
for ($self->metals) {
#print;
push(@data, [$_, $self->{$_}{'oz_price'}, $self->{$_}{'oz_price'}{'ts'}]);
}
return \@data;
}
} # metal_data
## getter/setter ##
sub metals {
my $self = shift;
if (@_) {
$self->{'-metals'} = shift;
} else {
return(split/,/, $self->{'-metals'});
}
} # metals
1;