Emily Wilson is an engineering intern with Privy for the summer of 2018. She is part of the Georgia Institute of Technology’s class of 2021.
Earlier this month we published a new Ruby Gem that handles fetching updated currency conversion rates. We previously used the GoogleCurrency gem to fetch the exchange rates, but the Google endpoint that the gem relies on is no longer supported. This caused errors when we attempted to exchange currencies. After looking at replacements for the gem, we decided it would be best to fork the GoogleCurrency gem and modify it to meet our needs.
Why it was needed
The biggest feature we found lacking when looking at alternative gems was the support for lazy loading. Without this feature, we would have needed to place the call to fetch exchange rates in an initializer so that rates would be present should currency conversion be necessary. As a result, on every server spin-up — in production or development — an API call would have been made to get the most recent rates from the bank. This is problematic because most of these gems make requests to services that limit the number of API calls an account can make in one month. Due to this restriction there would have been a possibility that we run out of API calls during a production server spin-up, resulting in initialization errors.
After looking at the code where currency conversion was required, we decided that lazy loading was absolutely necessary in our case. Most user interactions don’t require currency exchange, so it would have been wasteful to make so many eager API calls. Furthermore, the dependency on a third-party service during server initialization would be a big risk given the probability of network errors — we wouldn’t want our servers’ uptime to be affected by a failing third-party service.
Building the gem
After forking the GoogleCurrency gem, we modified the source code to read currency exchange rates from fixer.io: an API for current and historical exchange rates. Using our new gem, we only make requests to Fixer when we need to exchange currencies, caching the results for up to 24 hours. This way, we never make eager API calls, saving us from many of the problems discussed earlier.