[ic] Interchange Howto: Exchange Rate UserTag - (Canada and U.S.)

Stanley Major webmaster at calgary-ecommerce-services.com
Sun Mar 13 19:17:58 EST 2005


Quoting Paul Rogers <paul at btsonline.co.uk>:

>
>
> Hi all,
>
> I've just finished a new [exchange-rate] UserTag for Interchange which will
> fetch, parse and display
> currency exchange rates and which I hope someone may find useful.
>
> The tag allows currency exchange rates to be fetched for Euros and US
> Dollars, with UK Pounds as the base
> currency, but can be adapted for any currencies. It can be used anywhere in
> your pages as follows:
>
> [exchange-rate USD] or [exchange-rate EUR]
>
> To make the tag, copy the code below into a file called 'exchange_rate.tag'
> and place into the UserTag
> directory, then restart Interchange server:
>
> ------------------------- BEGIN
> CODE -----------------------------------------
>
> # [exchange-rate]
> #
> # This tag gets the latest exchange rate from Yahoo Finance for EUR and USD
> against
> # GBP and returns the rate to IC.
> # Usage: [exchange-rate ###]
> #
> # Copyright 2005 Business Technology Solutions
> # Updates and support available at:
> # http://www.btsonline.co.uk/ic
> # Please leave this copyright message intact if you
> # wish to use this tag.
> #
> UserTag exchange-rate Order currency
> UserTag exchange-rate PosNumber 1
> UserTag exchange-rate Routine < sub {
>
> my ($currency) = @_;
> my $url;
>
> if ($currency eq 'EUR') {
>
> $url =
> 'http://finance.yahoo.com/currency/convert?amt=1&from=GBP&to=EUR&submit=Conv
> ert';
> }
> else {
>
> $url =
> 'http://finance.yahoo.com/currency/convert?amt=1&from=GBP&to=USD&submit=Conv
> ert';
> }
> use LWP::Simple;
> my $content = get $url;
> die "Couldn't get $url" unless defined $content;
> $content =~ /(d.d{2,3})(......)/;
> return $1;
> exit;
> # $Session->{mv_perl_result} = $1;
> }
> EOR
>
> ------------------------------------ END
> CODE -------------------------------------------
>
>
> The base currency can be changed from UK Pounds and set to any currency
> simply by changing the
> 'from=###' part of the lookup URL's to any 3 letter international currency
> code.
>
> The tag can also be easily adapted to convert to other currencies simply by
> changing the 'to=###'
> part of lookup URL's as above.
>
> Note:
> Since there is a slight delay between fetching the URL and parsing it before
> returning the exchange
> rate. Therefore it's perhaps not recommended to use this tag in frequently
> used pages.
>
> If you need to display the currency exchange rate in your pages or use it in
> your code to determine prices,
> a better solution is to use the exchange-rate tag to update your locale
> table, and then query the locale
> table to get the data from there. A simple method for achieving this
> detailed on my website at the url below
> for those interested.
>
>

I've tried to adapt this for displaying both U.S. and Canadian currencies and am
having some issues. My exchange_rate.tag contains:

if ($currency eq 'EUR') {

                                            $url =
'http://finance.yahoo.com/currency/convert?amt=1&from=USD&to=EUR&submit=Convert';
}
else {

                                            $url =
'http://finance.yahoo.com/currency/convert?amt=1&from=USD&to=CAD&submit=Convert';
}

I changed the default locale.txt by adding an en_CD column next to en_US with
all the same values. The default locale.txt did not have a column for the Euro,
so I used the de_DE column. My rates.html page contains:

[set_no_login_required]1[/set]
[set update_database]
[set mv_data_enable]1[/set]
[/set]
<html>
<head></head>
<body onLoad="rates.submit()">
<FORM METHOD=POST ACTION="[process]" ENCTYPE="multipart/form-data" NAME="rates">
<INPUT TYPE="hidden" NAME="mv_session_id" VALUE="[data session id]">
<INPUT TYPE="hidden" NAME="mv_click" VALUE="process_filter">
<INPUT TYPE="hidden" NAME="code" VALUE="PriceDivide">
<INPUT TYPE="hidden" NAME="mv_data_fields" VALUE="code,en_US,de_DE">
<INPUT TYPE="hidden" NAME="mv_data_table" VALUE="locale">
<INPUT TYPE="hidden" NAME="mv_todo" VALUE="set">
<INPUT TYPE="hidden" NAME="mv_data_key" VALUE="code">
<INPUT TYPE="hidden" NAME="mv_action" VALUE="set">
<INPUT TYPE="hidden" NAME="mv_data_function" VALUE="update">
<INPUT TYPE="hidden" NAME="mv_update_empty" VALUE="1">
<INPUT TYPE="hidden" NAME="en_US" VALUE="[exchange-rate USD]">
<INPUT TYPE="hidden" NAME="de_DE" VALUE="[exchange-rate EUR]">
<INPUT TYPE="hidden" NAME="mv_ui" VALUE="1">
<INPUT TYPE="hidden" NAME="mv_auto_export" VALUE="locale">
<INPUT TYPE="hidden" NAME="mv_click" VALUE="update_database">
</form>
</body>
</html>

What I'm seeing is that the PriceDivide fields for en_US and de_DE in locale.txt
are being blanked out.
I have LWP installed and when I run the following code

my $currency;
my $url;
$currency='EUR';
if ($currency eq 'EUR') {
$url =
'http://finance.yahoo.com/currency/convert?amt=1&from=USD&to=EUR&submit=Convert';
}
else {
$url =
'http://finance.yahoo.com/currency/convert?amt=1&from=USD&to=CAD&submit=Convert';
}
use LWP::Simple;
my $content = get $url;
die "Couldn't get $url" unless defined $content;
$content =~ /(d.d{2,3})(......)/;
print $content;
#return $1;
exit;

by #perl testLWP.pl , it produces what looks like the content of the Yahoo page.
Any pointers here as to why the values in locale.txt are being blanked out, but
not overwritten with the fetched values?


Stanley Major
Calgary Ecommerce Services
http://www.calgary-ecommerce-services.com
Phone: 403-255-4701


More information about the interchange-users mailing list