snowplow / scala-forex

High-performance Scala library for performing exchange rate lookups and currency conversions
http://snowplow.github.io/scala-forex/
46 stars 12 forks source link

Replace configurableBaseCurrency with an ADT (alebraic data type) for accountlevel #74

Closed alexanderdean closed 10 years ago

alexanderdean commented 10 years ago

AccountLevel = DeveloperAccount, EnterpriseAccount, UnlimitedAccount

If Enterprise or Unlimited, then configurableBaseCurrency = true

jz4112 commented 10 years ago
/**
 * URI link base
 */  
private val base   = oerConfig.accountLevel match {
      case UnlimitedAccount  => "&base=" + config.baseCurrency 
      case EnterpriseAccount => "&base=" + config.baseCurrency 
      case DeveloperAccount  => ""
  } 

/**
 * Populating cache for different types of accounts
 */
 oerConfig.accountLevel match {
              // If the user is using Developer account, 
              // then base currency returned from the API is USD.
              // To store user-defined base currency into the cache, 
              // we need to convert the forex rate between target currency and USD
              // to target currency and user-defined base currency 
                  case DeveloperAccount 
                    => {
                      val usdOverBase = node.findValue(config.baseCurrency).getDecimalValue
                      while (currencyNameIterator.hasNext) {  
                        val currencyName = currencyNameIterator.next
                        val keyPair   = (config.baseCurrency, currencyName)
                        val usdOverCurr  = node.findValue(currencyName).getDecimalValue
                        val fromCurrIsBaseCurr = (config.baseCurrency == "USD")
                        val baseOverCurr = Forex.getForexRate(fromCurrIsBaseCurr, usdOverBase, usdOverCurr) 
                        val valPair = (DateTime.now, baseOverCurr)
                        cache.put(keyPair, valPair)
                      }
                    }
              // For Enterprise and Unlimited users, OER allows them to configure the base currencies.
              // So the exchange rate returned from the API is between target currency
             // and the base currency they defined.
                  case _ 
                    => {
                      while (currencyNameIterator.hasNext) {  
                        val currencyName = currencyNameIterator.next
                        val keyPair = (config.baseCurrency, currencyName)
                        val valPair = (DateTime.now, node.findValue(currencyName).getDecimalValue)
                        cache.put(keyPair, valPair)
                      }
                    }
                }   

/**
 * Define account types
 */
sealed trait AccountType
object DeveloperAccount extends AccountType
object EnterpriseAccount extends AccountType
object UnlimitedAccount extends AccountType