Skip to content

Commit

Permalink
[WIP] Scala Common Enrich: bump scala-forex to 0.7.0 (closes #4031)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenFradet authored and chuwy committed Nov 5, 2019
1 parent a91be11 commit 17e956c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion 3-enrich/scala-common-enrich/project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object Dependencies {
val circeOptics = "0.11.0"
val circeJackson = "0.11.1"
val igluClient = "0.6.0-M3"
val scalaForex = "0.5.0"
val scalaForex = "0.7.0-M4"
val scalaWeather = "0.3.0"
val scalaj = "2.3.0"
val gatlingJsonpath = "0.6.14"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ object EnrichmentManager {
// Finalize the currency conversion
val currency: Either[NonEmptyList[String], Unit] = {
registry.getCurrencyConversionEnrichment match {
case Some(currency) => {
event.base_currency = currency.baseCurrency
case Some(currency) =>
event.base_currency = currency.baseCurrency.getCode
// Note that stringToMaybeDouble is applied to either-valid-or-null event POJO
// properties, so we don't expect any of these four vals to be a Failure
val trTax = CU.stringToMaybeDouble("tr_tx", event.tr_tax).toValidatedNel
Expand Down Expand Up @@ -374,7 +374,6 @@ object EnrichmentManager {
event.ti_price_base = price.orNull
}
().asRight
}
case None => ().asRight
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ package com.snowplowanalytics.snowplow.enrich.common
package enrichments.registry

import java.net.UnknownHostException
import java.time.ZonedDateTime

import scala.util.control.NonFatal

import cats.data.{NonEmptyList, ValidatedNel}
import cats.implicits._
import com.snowplowanalytics.forex.oerclient._
import com.snowplowanalytics.forex.{Forex, ForexConfig}
import com.snowplowanalytics.forex.Forex
import com.snowplowanalytics.forex.model._
import com.snowplowanalytics.iglu.core.{SchemaCriterion, SchemaKey}
import io.circe._
import org.joda.money.CurrencyUnit
import org.joda.time.DateTime

import utils.CirceUtils
Expand All @@ -48,7 +50,10 @@ object CurrencyConversionEnrichmentConfig extends ParseableEnrichment {
.flatMap { _ =>
(
CirceUtils.extract[String](c, "parameters", "apiKey").toValidatedNel,
CirceUtils.extract[String](c, "parameters", "baseCurrency").toValidatedNel,
CirceUtils.extract[String](c, "parameters", "baseCurrency")
.toEither
.flatMap(bc => Either.catchNonFatal(CurrencyUnit.of(bc)).leftMap(_.getMessage))
.toValidatedNel,
CirceUtils
.extract[String](c, "parameters", "accountType")
.toEither
Expand Down Expand Up @@ -79,10 +84,10 @@ object CurrencyConversionEnrichmentConfig extends ParseableEnrichment {
final case class CurrencyConversionEnrichment(
accountType: AccountType,
apiKey: String,
baseCurrency: String,
baseCurrency: CurrencyUnit,
rateAt: String
) extends Enrichment {
val fx = Forex(ForexConfig(), OerClientConfig(apiKey, accountType))
val fx = Forex.unsafeGetForex(ForexConfig(apiKey, accountType, baseCurrency = baseCurrency)).value

/**
* Attempt to convert if the initial currency and value are both defined
Expand All @@ -92,15 +97,16 @@ final case class CurrencyConversionEnrichment(
* otherwise Validation[Option[_]] boxing the result of the conversion
*/
private def performConversion(
initialCurrency: Option[String],
initialCurrency: Option[CurrencyUnit],
value: Option[Double],
tstamp: DateTime
tstamp: ZonedDateTime
): Either[String, Option[String]] =
(initialCurrency, value) match {
case (Some(ic), Some(v)) =>
fx.convert(v, ic)
.to(baseCurrency)
.at(tstamp)
.value
.bimap(
l => {
val errorType = l.errorType.getClass.getSimpleName.replace("$", "")
Expand Down Expand Up @@ -134,10 +140,11 @@ final case class CurrencyConversionEnrichment(
collectorTstamp match {
case Some(tstamp) =>
try {
val newCurrencyTr = performConversion(trCurrency, trTotal, tstamp)
val newCurrencyTi = performConversion(tiCurrency, tiPrice, tstamp)
val newTrTax = performConversion(trCurrency, trTax, tstamp)
val newTrShipping = performConversion(trCurrency, trShipping, tstamp)
val zdt = tstamp.toGregorianCalendar().toZonedDateTime()
val newCurrencyTr = performConversion(trCurrency.map(CurrencyUnit.of), trTotal, zdt)
val newCurrencyTi = performConversion(tiCurrency.map(CurrencyUnit.of), tiPrice, zdt)
val newTrTax = performConversion(trCurrency.map(CurrencyUnit.of), trTax, zdt)
val newTrShipping = performConversion(trCurrency.map(CurrencyUnit.of), trShipping, zdt)
(
newCurrencyTr.toValidatedNel,
newTrTax.toValidatedNel,
Expand Down

0 comments on commit 17e956c

Please sign in to comment.