Skip to content

Commit

Permalink
Scala Common Enrich: currency conversion enrichment is now configured…
Browse files Browse the repository at this point in the history
… with account type (see #370)
  • Loading branch information
fblundun committed Mar 4, 2015
1 parent fd08b47 commit e2c5d14
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ import iglu.client.{
SchemaKey,
SchemaCriterion
}
import iglu.client.validation.ProcessingMessageMethods._

// Joda-Time
import org.joda.time.DateTime

// Scala-Forex
import com.snowplowanalytics.forex.oerclient.{
OerClientConfig,
AccountType,
DeveloperAccount,
EnterpriseAccount,
UnlimitedAccount,
OerResponseError
}
import com.snowplowanalytics.forex.{
Expand All @@ -68,8 +72,17 @@ object CurrencyConversionEnrichmentConfig extends ParseableEnrichment {
(for {
apiKey <- ScalazJson4sUtils.extract[String](config, "parameters", "apiKey")
baseCurrency <- ScalazJson4sUtils.extract[String](config, "parameters", "baseCurrency")
accountType <- (ScalazJson4sUtils.extract[String](config, "parameters", "accountType") match {
case Success("developer") => DeveloperAccount.success
case Success("enterprise") => EnterpriseAccount.success
case Success("unlimited") => UnlimitedAccount.success

// These failures should be prevented by the schema
case Success(s) => "accountType [%s] is not one of developer, enterprise, and unlimited".format(s).toProcessingMessage.fail
case Failure(f) => Failure(f)
})
rateAt <- ScalazJson4sUtils.extract[String](config, "parameters", "rateAt")
enrich = CurrencyConversionEnrichment(apiKey, baseCurrency, rateAt)
enrich = CurrencyConversionEnrichment(accountType, apiKey, baseCurrency, rateAt)
} yield enrich).toValidationNel
})
}
Expand All @@ -83,13 +96,14 @@ object CurrencyConversionEnrichmentConfig extends ParseableEnrichment {
* @param rateAt Which exchange rate to use - "EOD_PRIOR" for "end of previous day".
*/
case class CurrencyConversionEnrichment(
accountType: AccountType,
apiKey: String,
baseCurrency: String,
rateAt: String) extends Enrichment {

val version = new DefaultArtifactVersion("0.1.0")

val fx = Forex(ForexConfig(nowishCacheSize = 0, nowishSecs = 0, eodCacheSize = 0), OerClientConfig(apiKey, DeveloperAccount))
val fx = Forex(ForexConfig(nowishCacheSize = 0, nowishSecs = 0, eodCacheSize = 0), OerClientConfig(apiKey, accountType))

/**
* Attempt to convert if the initial currency and value are both defined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ package com.snowplowanalytics.snowplow.enrich.common
package enrichments
package registry

// Scala-Forex
import com.snowplowanalytics.forex.oerclient.DeveloperAccount

// Specs2
import org.specs2.Specification
import org.specs2.matcher.DataTables
Expand Down Expand Up @@ -56,7 +59,7 @@ class CurrencyConversionEnrichmentSpec extends Specification with DataTables { d
"Invalid transaction item currency" !! None ! appId ! Some(12.00) ! Some(0.7) ! Some(0.00) ! Some("HUL") ! Some(1.99) ! Some(coTstamp) ! currencyInvalidHul |
"Invalid OER API key" !! None ! "8A8A8A8A8A8A8A8A8A8A8A8AA8A8A8A8" ! Some(13.00) ! Some(3.67) ! Some(0.00) ! Some("GBP") ! Some(2.99) ! Some(coTstamp) ! appIdInvalid |> {
(_, trCurrency, apiKey, trAmountTotal, trAmountTax, trAmountShipping, tiCurrency, tiPrice, dateTime, expected) =>
CurrencyConversionEnrichment(apiKey, "EUR", "EOD_PRIOR").convertCurrencies(trCurrency, trAmountTotal, trAmountTax, trAmountShipping, tiCurrency, tiPrice, dateTime) must_== expected
CurrencyConversionEnrichment(DeveloperAccount, apiKey, "EUR", "EOD_PRIOR").convertCurrencies(trCurrency, trAmountTotal, trAmountTax, trAmountShipping, tiCurrency, tiPrice, dateTime) must_== expected
}

def e2 =
Expand All @@ -71,7 +74,7 @@ class CurrencyConversionEnrichmentSpec extends Specification with DataTables { d
"Both Currency Null" !! None ! appId ! Some(11.00) ! Some(2.67) ! Some(0.00) ! None ! Some(12.99) ! Some(coTstamp) ! (None,None,None,None).success |
"Valid APP ID and API key" !! Some("GBP") ! appId ! Some(16.00) ! Some(2.67) ! Some(0.00) ! None ! Some(10.00) ! Some(coTstamp) ! (Some("18.54"), Some("3.09"), Some("0.00"),None).success |> {
(_, trCurrency, apiKey, trAmountTotal, trAmountTax, trAmountShipping, tiCurrency, tiPrice, dateTime, expected) =>
CurrencyConversionEnrichment(apiKey, "EUR", "EOD_PRIOR").convertCurrencies(trCurrency, trAmountTotal, trAmountTax, trAmountShipping, tiCurrency, tiPrice, dateTime) must_== expected
CurrencyConversionEnrichment(DeveloperAccount, apiKey, "EUR", "EOD_PRIOR").convertCurrencies(trCurrency, trAmountTotal, trAmountTax, trAmountShipping, tiCurrency, tiPrice, dateTime) must_== expected
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import org.json4s.jackson.JsonMethods.parse
// Iglu
import com.snowplowanalytics.iglu.client.SchemaKey

// Scala-Forex
import com.snowplowanalytics.forex.oerclient.DeveloperAccount

// Specs2
import org.specs2.mutable.Specification
import org.specs2.scalaz.ValidationMatchers
Expand Down Expand Up @@ -193,6 +196,7 @@ class EnrichmentConfigsSpec extends Specification with ValidationMatchers {
val currencyConversionEnrichmentJson = parse("""{
"enabled": true,
"parameters": {
"accountType": "developer",
"apiKey": "---",
"baseCurrency": "EUR",
"rateAt": "EOD_PRIOR"
Expand All @@ -202,7 +206,7 @@ class EnrichmentConfigsSpec extends Specification with ValidationMatchers {
val schemaKey = SchemaKey("com.snowplowanalytics.snowplow", "currency_conversion_config", "jsonschema", "1-0-0")

val result = CurrencyConversionEnrichmentConfig.parse(currencyConversionEnrichmentJson, schemaKey)
result must beSuccessful(CurrencyConversionEnrichment("---", "EUR", "EOD_PRIOR"))
result must beSuccessful(CurrencyConversionEnrichment(DeveloperAccount, "---", "EUR", "EOD_PRIOR"))

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ object JobSpecHelpers {
|"vendor": "com.snowplowanalytics.snowplow",
|"name": "currency_conversion_config",
|"parameters": {
|"accountType": "developer",
|"apiKey": "%s",
|"baseCurrency": "EUR",
|"rateAt": "EOD_PRIOR"
Expand Down

0 comments on commit e2c5d14

Please sign in to comment.