Variations on currency conversion (using xe.com)
This project dates back to 2008 when I was given a task to maintain currency exchange information for a logistic system written in Rails.
Cutting off the fluff, the core problem is getting a ratio for a pair of currencies.
http://xe.com provides exactly such a service, but by default it throws back a fancy HTML page. I can parse it of course but my instincts told me maybe I don't have to... After some exploration I found a WAP version of it which gave a lot simpler result page (and probably imposed less load on the server too).
So the question is how to download and parse out the number.
My manager envisioned some ruby solution which would be a daily background process pushing the results through ActiveRecord into the model which already exists there for acquiring the exchange ratio and we can use something like hpricot to transform the markup into an easily accessible ruby data structure so any ruby developer could understand it. Language homogeneity would come with one more benefit: no separate maintenance and deployment. I would argue about the maintenance, since we should have introduced a background processor and an XML parser. Deployment of the solution would be a bit divergent though, I agree. But all this for getting a number from another machine?...
The xe.com guys worked hard to wrap the actual info into a human consumable format, so when a machine needs it we have to work hard to take away the part prepared for the human eyes.
I was using a Mac at that time for the development, but the production servers were Linux. So keeping the solution cross-platform I choose REBOL (1 executable file, 360kbyte, Win/Lin/Mac/OBSD/Solaris) for getting and exploring the result page and parsing interactively. But this task seemed simple enough to solve with UNIX "primitives".
The result was 2 lines (plus 1 line for supplying a User-Agent HTTP header, so the service considers us a human browser and was kind enough to please us with an answer). For the sake of being cross platform I have rearranged the parameters so I had one line for cURL on Mac and one for Wget on Linux. No Windows at that time (already for many many years)...
#!/bin/shcurl "http://www.xe.com/wap/2co/convert.cgi?Amount=$1&From=$2&To=$3" \-A "Mozilla/7 [en] (X11; U; Linux 2.6.1 ia64)" -s | \sed -n "s/.*>\(.*\) `echo $3 | tr '[a-z]' '[A-Z]'`<.*/\1/p"
I didn't see much chance for the resulting page have the 3 letter currency abbreviations as lowercase substrings, so I switched for incase sensitive search in sed:
sed -n "s/.*>\(.*\) $3<.*/\1/ip"
I used this for more than a year for converting between HUF, JPY, USD, NZD, HKD, SGD, MYR, EUR, PHP, GBP, THB, HKT (oh, no, that's the 3 letter ID of the airport at Phuket :), INR, BDT, KHR, VND
Because...
- I used HUngarianForint for 31 years of my life,
- but I got a job under the rising sun, where they measure mundane value with JaPaneseYen.
- The hungarian banks could accept only a few currencies, so I received my salary in USaDollars.
- At that time the Japanese company was still exporting cars to NewZealandDollar users.
- My friend transfered some money from his HongKongDollar account to help me buying a plane ticket.
- Later the Japanese company's child company moved to Singapore where I had to renegotiate my salary in SinGaporeDollars.
- I couldn't get my work permit within 3 months, so I must have left Singapore for a while just to make sure... The obvious option was the immediate neighbor, where people trade with MalaYsianRingget. Next year I visited Sibu Islands. Wonderful for snorkling ;)
- A girl maintained a close relationship with me and I bought her a plane ticket to Manila for PHilippinePesos.
- I had some hungarian friend working in Dublin where they pay with GreatBritainPounds.
- A bit later I formally "bought" a Thai wife for 10'000 (nüng mün) THaiBhat.
- She worked around Little India district of Singapore, where people carry INdianRupee around.
- Sometimes workers from Bangladesh pop up there too possessing some BanglaDeshiTaka.
- Later I participated in the Cambodian barcamp at Phnom Penh where people expect KHmerRiel in exchange for food.
- Then flew to Sai Gon also for a barcamp, so my wallet saw some VietNameseDong too.
But my laptop was stolen in Cambodia... Unfortunately I bought a netbook which has an unstable video driver for Linux, hence I'm forced to use it with Windows.. Since I work through GPRS or 3G most of the time, it's inconvenient to use my script through SSH (putty) and hunting down wget and sed for this task is just too ugly. So back to the roots.. I pulled out REBOL again. Here is the result:
REBOL[ Title: "Currency converter" Author: onetom@rumli.org Date: 2010-01-10 ] forever [ set [amt src dst x] load ask "Amount source-currency destination-currency? " pg: read rejoin [http://www.xe.com/wap/2co/convert.cgi?Amount= amt '&From= src '&To= dst] print copy/part x: find/case pg uppercase join " " dst find/reverse/tail x ">"]
Which is an interactive REPL interface for currency conversion...
CON:
- A bit more verbose than the shell script version.
- The result extraction code is not very functional or declarative, so it's not easy to replay in mind.
- You have to learn REBOL if you want to extend it. (which might take a week. terrible...)
PRO:
- The only prerequisite to run this on any platform is one, 360kbyte executable file, so it's quite easy to "install".
- It can be conveniently extended with
- a smarter and still safe input interpretation like setting a default destination or source currency
- a graphical user interface (for representing trends and cross currency info...)
- an offline version which works from cached rates
- You name it ;)
869 views and 2 responses
-
Aug 25 2010, 11:18 PMsanya2010 responded:Interesting stuff!! It’s a very good post I have come across.I really like your stuff very much.It’s a very appreciated .Thanks for sharing.Keep up the great job!
-
Dec 27 2012, 8:00 AMcropimmycir1986 responded: