diff --git a/project/BuildSettings.scala b/project/BuildSettings.scala index 916ed16..523a21f 100644 --- a/project/BuildSettings.scala +++ b/project/BuildSettings.scala @@ -20,8 +20,8 @@ object BuildSettings { organization := "com.snowplowanalytics", version := "0.4.0", description := "High-performance Scala library for performing currency conversions using Open Exchange Rates", - scalaVersion := "2.10.1", - crossScalaVersions := Seq("2.9.3", "2.10.1", "2.11.5"), + scalaVersion := "2.10.6", + crossScalaVersions := Seq("2.9.3", "2.10.6", "2.11.8"), scalacOptions := Seq("-deprecation", "-encoding", "utf8"), resolvers ++= Dependencies.resolutionRepos ) diff --git a/src/main/scala/com.snowplowanalytics/forex/oerclient/OerClient.scala b/src/main/scala/com.snowplowanalytics/forex/oerclient/OerClient.scala index 9ba65b2..a749381 100644 --- a/src/main/scala/com.snowplowanalytics/forex/oerclient/OerClient.scala +++ b/src/main/scala/com.snowplowanalytics/forex/oerclient/OerClient.scala @@ -26,12 +26,12 @@ import org.joda.time._ /** * Implements Json for Open Exchange Rates(http://openexchangerates.org) * @param config - a configurator for Forex object - * @param oerConfig - a configurator for OER Client object + * @param oerConfig - a configurator for OER Client object * @param nowishCache - user defined nowishCache * @param eodCache - user defined eodCache */ class OerClient( - config: ForexConfig, + config: ForexConfig, oerConfig: OerClientConfig, nowishCache: MaybeNowishCache = None, eodCache: MaybeEodCache = None @@ -41,15 +41,15 @@ class OerClient( private val oerUrl = "http://openexchangerates.org/api/" /** Sets the base currency in the url - * according to the API, only Unlimited and Enterprise accounts + * according to the API, only Unlimited and Enterprise accounts * are allowed to set the base currency in the HTTP URL */ private val base = oerConfig.accountLevel match { - case UnlimitedAccount => "&base=" + config.baseCurrency - case EnterpriseAccount => "&base=" + config.baseCurrency + case UnlimitedAccount => "&base=" + config.baseCurrency + case EnterpriseAccount => "&base=" + config.baseCurrency case DeveloperAccount => "" - } - + } + /** * The constant that will hold the URL for * a live exchange rate lookup from OER @@ -57,7 +57,7 @@ class OerClient( private val latest = "latest.json?app_id=" + oerConfig.appId + base /** - * The constant will hold the URI for a + * The constant will hold the URI for a * historical-exchange rate lookup from OER */ private val historical = "historical/%04d-%02d-%02d.json?app_id=" + oerConfig.appId + base @@ -69,8 +69,8 @@ class OerClient( private val oerDataFrom = new DateTime(1999,1,1,0,0) /** - * Gets live currency value for the desired currency, - * silently drop the currency types which Joda money does not support. + * Gets live currency value for the desired currency, + * silently drop the currency types which Joda money does not support. * If cache exists, update nowishCache when an API request has been done, * else just return the forex rate * @param currency - The desired currency we want to look up from the API @@ -85,39 +85,39 @@ class OerClient( case Some(cache) => { val currencyNameIterator = node.getFieldNames oerConfig.accountLevel match { - // If the user is using Developer account, + // 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, + // 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 + // to target currency and user-defined base currency + case DeveloperAccount => { val usdOverBase = node.findValue(config.baseCurrency).getDecimalValue - while (currencyNameIterator.hasNext) { + while (currencyNameIterator.hasNext) { val currencyName = currencyNameIterator.next val keyPair = (config.baseCurrency, currencyName) val usdOverCurr = node.findValue(currencyName).getDecimalValue // flag indicating if the base currency has been set to USD val fromCurrIsBaseCurr = (config.baseCurrency == "USD") - val baseOverCurr = Forex.getForexRate(fromCurrIsBaseCurr, usdOverBase, usdOverCurr) + 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 _ + case _ => { - while (currencyNameIterator.hasNext) { + while (currencyNameIterator.hasNext) { val currencyName = currencyNameIterator.next val keyPair = (config.baseCurrency, currencyName) val valPair = (DateTime.now, node.findValue(currencyName).getDecimalValue) cache.put(keyPair, valPair) } } - } + } } - case None => // do nothing + case None => // do nothing } val currencyNode = node.findValue(currency) if (currencyNode == null) { @@ -131,9 +131,9 @@ class OerClient( /** * Builds the historical link for the URI according to the date - * @param date - The historical date for the currency look up, + * @param date - The historical date for the currency look up, * which should be the same as date argument in the getHistoricalCurrencyValue method below - * @return the link in string format + * @return the link in string format */ private def buildHistoricalLink(date: DateTime) : String = { val dateCal = date.toGregorianCalendar @@ -144,7 +144,7 @@ class OerClient( } /** * Gets historical forex rate for the given currency and date - * return error message if the date is invalid + * return error message if the date is invalid * silently drop the currency types which Joda money does not support * if cache exists, update the eodCache when an API request has been done, * else just return the look up result @@ -153,7 +153,7 @@ class OerClient( * @return result returned from API */ def getHistoricalCurrencyValue(currency: String, date: DateTime): ApiRequestResult = { - + /** * Return OerResponseError if the date given is not supported by OER */ @@ -168,15 +168,15 @@ class OerClient( case Some(cache) => { val currencyNameIterator = node.getFieldNames oerConfig.accountLevel match { - // If the user is using Developer account, + // 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, + // 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 + // to target currency and user-defined base currency + case DeveloperAccount => { val usdOverBase = node.findValue(config.baseCurrency).getDecimalValue - while (currencyNameIterator.hasNext) { + while (currencyNameIterator.hasNext) { val currencyName = currencyNameIterator.next val keyPair = (config.baseCurrency, currencyName, date) val usdOverCurr = node.findValue(currencyName).getDecimalValue @@ -186,15 +186,15 @@ class OerClient( } // 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 _ + case _ => { - while (currencyNameIterator.hasNext) { + while (currencyNameIterator.hasNext) { val currencyName = currencyNameIterator.next val keyPair = (config.baseCurrency, currencyName, date) cache.put(keyPair, node.findValue(currencyName).getDecimalValue) } } - } + } } case None => // do nothing } @@ -215,7 +215,7 @@ class OerClient( * @param downloadPath - The URI link for the API request * @return JSON node which contains currency information obtained from API * or OerResponseError object which carries the error message returned by the API - */ + */ private def getJsonNodeFromApi(downloadPath: String): Either[OerResponseError, JsonNode] = { val url = new URL(oerUrl + downloadPath) val conn = url.openConnection @@ -228,11 +228,7 @@ class OerClient( while (root.hasNext) { resNode = root.next } - if (resNode.getTextValue.contains("Invalid App ID")) { - Left(OerResponseError(resNode.getTextValue, InvalidAppId)) - } else { - Left(OerResponseError(resNode.getTextValue, OtherErrors)) - } + Left(OerResponseError(resNode.getTextValue, OtherErrors)) } else { val inputStream = httpUrlConn.getInputStream val root = mapper.readTree(inputStream).getElements diff --git a/src/main/scala/com.snowplowanalytics/forex/oerclient/OerResponseError.scala b/src/main/scala/com.snowplowanalytics/forex/oerclient/OerResponseError.scala index 0c7d73d..6811c00 100644 --- a/src/main/scala/com.snowplowanalytics/forex/oerclient/OerResponseError.scala +++ b/src/main/scala/com.snowplowanalytics/forex/oerclient/OerResponseError.scala @@ -21,7 +21,7 @@ package oerclient case class OerResponseError(errorMessage: String, errorType: OerError) /** - * User defined error types + * User defined error types */ sealed trait OerError @@ -31,11 +31,6 @@ sealed trait OerError */ object ResourcesNotAvailable extends OerError -/** - * Invalid appId - */ -object InvalidAppId extends OerError - /** * Currency not supported by API or * Joda Money or both diff --git a/src/test/scala/com.snowplowanalytics.forex/InvalidAppIDSpec.scala b/src/test/scala/com.snowplowanalytics.forex/InvalidAppIDSpec.scala deleted file mode 100644 index 4de66f6..0000000 --- a/src/test/scala/com.snowplowanalytics.forex/InvalidAppIDSpec.scala +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2013-2015 Snowplow Analytics Ltd. All rights reserved. - * - * This program is licensed to you under the Apache License Version 2.0, - * and you may not use this file except in compliance with the Apache License Version 2.0. - * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the Apache License Version 2.0 is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. - */ -package com.snowplowanalytics.forex - -// Specs2 -import org.specs2.mutable.Specification -// oerclient -import oerclient.InvalidAppId -import oerclient.OerResponseError -// oerclient -import oerclient._ - -/** - * Testing validity of appID - */ -class InvalidAppIdSpec extends Specification { - val fxWithInvalidID = Forex.getForex(ForexConfig(), OerClientConfig("this is invalid appId", DeveloperAccount)) - fxWithInvalidID.rate.to("GBP").now must beLike { - case Left(OerResponseError(_, InvalidAppId)) => ok - } -} \ No newline at end of file