[ic] Interchange Howto: Exchange Rate UserTag

Paul Rogers paul at btsnet.co.uk
Wed Mar 9 09:35:50 EST 2005


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.


Paul Rogers
Interchange Consultant
Business Technology Solutions
=============================
Interchange Resource Pages
http://www.btsonline.co.uk/ic
=============================


--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.6.4 - Release Date: 07/03/05



More information about the interchange-users mailing list