In the rest of this post we will cover:
Late last year a Snowplow customer asked us if we could add currency conversions into their custom Snowplow implementation. This seemed like a great idea to explore, and we started to sketch out an approach:
The big missing piece for the above was a Scala library to handle currency conversions. We had some very specific requirements for this library:
Custom building a Scala library calling out to the Open Exchange Rates web service seemed the right way to meet these requirements, and so Scala Forex was born!
Scala Forex makes heavy use of the Joda-Money and Joda-Time libraries in its public API. These are enterprise-grade Java libraries for working with currency, money and time, and we were keen not to re-invent the wheel!
Under the covers, Jiawen’s library makes heavy use of two “least recently used” (LRU) caches, one to hold historic exchange rates and one to hold recent rates. It is these LRU caches which minimize calls out to the Open Exchange Rates web service, which is critical for performance (and cost) reasons.
The library is thoroughly tested using Specs2 (including Specs2 tables), and Mockito to verify cache behaviour. The library is integrated into Travis CI for continuous testing.
Using the library is straightforward: you initialize Scala Forex with general and OER-specific configuration, and then use a simple Scala DSL to both look up exchange rates and perform currency conversions.
For detailed guidance on configuring the library, please see the Configuration section of the Scala Forex README.
Once initialized, an exchange rate lookup is as simple as:
Here’s a slightly more complex example, of looking up a historic rate:
For detailed help on currency lookups, please see the Rate Lookup section of the Scala Forex README.
A currency conversion can be as simple as:
Here’s a slightly more complex example, of converting a trade using the end of day rate:
For detailed help on currency conversions, please see the Currency Conversion section of the Scala Forex README.
And that’s it; huge thanks to Jiawen Zhou for delivering such a sophisticated library in such a short time - and in a language she had never used before!
We hope that you find the Scala Forex library helpful - and stay tuned for when we can integrate it into Snowplow as part of our planned new currency enrichment capabilities!