Finance::Currency::Convert::BChile - Currency conversion module


Finance-Currency-Convert-BChile documentation Contained in the Finance-Currency-Convert-BChile distribution.

Index


Code Index:

NAME

Top

Finance::Currency::Convert::BChile - Currency conversion module between Chilean Pesos (CLP) and USA Dollars (USD).

VERSION

Top

Version 0.032

SYNOPSIS

Top

Currency conversion module between Chilean Pesos (CLP) and USA Dollars (USD). The conversion rate is obtained from the official source in Chile: the central bank "Banco Central de Chile", from their webpage http://www.bcentral.cl

    use Finance::Currency::Convert::BChile;

    my $conversor = Finance::Currency::Convert::BChile->new();
    $conversor->update;
    print $conversor->CLP2USD(20170);

FUNCTIONS

Top

new

Creates a new Finance::Currency::Convert::BChile object. Initializes the web robot. You can pass a user agent string as a parameter.

update

Obtains the data from bcentral page. You can pass a fake value as a parameter. In this case there's no update from bcentral site.

date

Return the date obtained from Banco Central's site. There's no formatting at all.

dollarValue

Return the value of 1 dollar in CLP. There's no formatting.

rate

Return the rate of transforming CLP in USD. If there's no value data, return -1

CLP2USD

Convert the amount received as parameter (CLP) to USD, rounded at two decimals. Returns -1 on error.

USD2CLP

Convert the amount received as parameter (USD) to CLP, rounded as integer (no decimals). Returns -1 on error.

AUTHOR

Top

Hugo Salgado, <hsalgado at vulcano.cl>

BUGS

Top

Please report any bugs or feature requests to bug-finance-currency-convert-bchile at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Finance-Currency-Convert-BChile. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Finance::Currency::Convert::BChile




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Finance-Currency-Convert-BChile

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Finance-Currency-Convert-BChile

* CPAN Ratings

http://cpanratings.perl.org/d/Finance-Currency-Convert-BChile

* Search CPAN

http://search.cpan.org/dist/Finance-Currency-Convert-BChile

SEE ALSO

Top

  HTML::TokeParser
  LWP::UserAgent

TERMS OF USE

Top

The data obtained from bcentral site is under this policy: http://www.bcentral.cl/sitio/condiciones-uso.htm

No liability is accepted by the author for abuse or miuse of the software herein. Use of this software is only permitted under the terms stipulated by bcentral.cl.

ACKNOWLEDGEMENTS

Top

This module was built for NIC Chile (http://www.nic.cl), who granted its liberation as free software.

COPYRIGHT & LICENSE

Top


Finance-Currency-Convert-BChile documentation Contained in the Finance-Currency-Convert-BChile distribution.
package Finance::Currency::Convert::BChile;

use warnings;
use strict;

use Carp;

$Finance::Currency::Convert::BChile::VERSION = '0.032';

use LWP::UserAgent;
use HTML::TokeParser;

my $BCENTRAL_URL = 'http://www.bcentral.cl/index.htm';
my $DEFAULT_UA   = 'Finance::Currency::Convert::BChile perl module';

sub new {
	my ($this, @args) = @_;

	my $class = ref($this) || $this;

	my $ua_string = $args[0] || $DEFAULT_UA;

	my $self = {};
	bless $self, $class;

	$self->{'ua'} = LWP::UserAgent->new
		or croak "Unable to create user agent";
	$self->{'ua'}->agent($ua_string);

	$self->{'dolar'} = '';
	$self->{'fecha'} = '';

	return $self;
}

sub update {
	my $self     = shift;
	my $simulate = shift;

	if ($simulate) {
		$self->{'dolar'} = $simulate;
		$self->{'fecha'} = 'UNKNOWN';

		return 1;
	}

	my $response = $self->{'ua'}->get($BCENTRAL_URL);

	unless ($response->is_success) {
		carp "Unable to get page: " . $response->status_line;

		$self->{'dolar'} = '';
		$self->{'fecha'} = '';
		return 0;
	}

	my $p = HTML::TokeParser->new(\$response->content)
		or croak "Unable to parse response: $!";

	my ($dolar, $fecha);

	while (my $token = $p->get_tag("div")) {
		next unless defined $token->[1]{'id'};
		next unless $token->[1]{'id'} eq 'ind-dia';

		while (my $token2 = $p->get_tag("p")) {
			next unless $token2->[1]{class} eq 'published-date';
			last;
		}
		$fecha = $p->get_text;

		while (my $token2 = $p->get_tag("td")) {
			next unless $p->get_text eq 'Dólar Observado';
			$p->get_tag("td");
			$dolar = $p->get_text;
		}
	}

	return 0 unless $dolar and $fecha;
    return 0 unless $dolar =~ /^\d+[,.]{0,1}\d+$/;

	$self->{'dolar'} = $dolar;
	$self->{'fecha'} = $fecha;

	return 1;
}

sub date {
	my $self = shift;

	return $self->{'fecha'};
}

sub dollarValue {
	my $self = shift;

	return $self->{'dolar'};
}

sub rate {
	my $self = shift;

	my $valor = $self->{'dolar'};

	# Changing decimal separator
	$valor =~ s/,/./;

    return -1 unless $valor;
    return -1 unless $valor =~ /^\d+[,.]{0,1}\d+$/;

	return -1 unless $valor >= 0;

	return 1 / $valor;
}

sub CLP2USD {
	my $self  = shift;
	my $pesos = shift;

	if ($pesos !~ /^\d+$/) {
		carp "Argument to CLP2USD must be an integer";
		return -1;
	}

	my $rate = $self->rate;
	return -1 unless $rate >= 0;

	return sprintf("%.2f", $pesos * $rate);
}

sub USD2CLP {
	my $self    = shift;
	my $dolares = shift;

	if ($dolares !~ /^\d+(\.\d{0,2}){0,1}$/) {
		carp "Wrong format in argument to USD2CLP";
		return -1;
	}

	my $valor = $self->{'dolar'};

	# Changing decimal separator
	$valor =~ s/,/./;

	return -1 unless $valor >= 0;

	return sprintf("%.0f", $dolares * $valor);
}

1; # End of Finance::Currency::Convert::BChile