Rails Currency Conversion Plugin - Cash handler
Looking at the state and how the current currency conversion gems and plugins for Rails work, I decided to roll my own very small and niche solution to currency conversion, named Cash Handler. The core functionality it provides is to convert a value from one currency to another (there are only 23 currencies listed, values are updated from scrapped data from x-rates.com). The plugin also incorporates a simple time-expiry cache for exchanges rates.
Here’s an example of Cash Handler in operation:
c = CashHandler::Base.new
# Get the current exchange rate of the AUD against the USD (currency code strings are case in-sensitive, can also be symbols)
c.get('AUD')
=> 0.619099
# Get the current exchange rate of the AUD against the GBP
c.get('AUD', :against => 'GBP')
=> 0.418621272567449
# Convert
c.convert(10, :aud, :usd)
=> 6.19099
# Force the converstion rates cache to reload currencies
c.cache.expire
# The CashHandler::Cache has a default cache life of one day, this can be overridden in one of two ways
# Upon creation
c = CashHandler::Base.new(30.minutes)
# During operation
c.cache.ttl = 30.minutes
