Skip to content

Instantly share code, notes, and snippets.

@IndiceeCoder
Created July 19, 2011 23:17
Show Gist options
  • Save IndiceeCoder/1093982 to your computer and use it in GitHub Desktop.
Save IndiceeCoder/1093982 to your computer and use it in GitHub Desktop.
iBIOS Customization Example - Currency Exchange 4
/**
* Obtain a converted currency value, using a cached for exchange rates
* The cached value of the exchange rate is used (if available) so long as it is no older than ageSeconds
* @arg ageSeconds the maximum age of the cached value of the exchange rate that will be used in the conversion
* @arg fromCurrency the originating currency ISO 4217 trigraph abbreviation in any case (e.g. EUR, cad, usd, Gbp)
* @arg toCurrency the target currency ISO 4217 trigraph abbrebiation in any case (e.g. Eur, CAD, Usd, GBP)
* @arg amount the amount to convert, denominated in the originating currency unit
* @return the equivalent amount, denominated in the target currency unit
*/
cachedCurrencyConversion :: Double -> String -> String -> Double -> Double;
public cachedCurrencyConversion ageSeconds fromCurrency toCurrency amount =
let
present = now;
oldConversion :: Maybe CachedConversion;
oldConversion = fetch "exchangeRates" (fromCurrency ++ toCurrency);
newConversion :: CachedConversion;
newConversion = store "exchangeRates" (fromCurrency ++ toCurrency) $ CachedConversion (unitExchangeRate fromCurrency toCurrency) present;
converted cachedValue amount = amount * cachedValue.CachedConversion.rate;
in
case oldConversion of
Nothing -> converted newConversion amount;
Just cachedConversion ->
if (durationToSeconds $ subtractTimeFromTime present cachedConversion.CachedConversion.time) > ageSeconds then
// Timed out
converted newConversion amount
else
// Cache still valid
converted cachedConversion amount;
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment