From 8cb6ac4c0ba1b4a1ecb021e0136927f2e2307c1f Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 8 May 2021 10:21:12 -0700 Subject: [PATCH] Added toPrice() extension. --- baseline.xml | 6 ++--- .../net/thauvin/erik/crypto/CryptoPrice.kt | 18 +++++++------ .../CryptoPriceTest.kt | 25 +++++++++++++------ 3 files changed, 31 insertions(+), 18 deletions(-) rename src/test/kotlin/net/thauvin/erik/{cryptoprice => crypto}/CryptoPriceTest.kt (67%) diff --git a/baseline.xml b/baseline.xml index 8c77e12..d8a3442 100644 --- a/baseline.xml +++ b/baseline.xml @@ -1,8 +1,8 @@ - + - + - ThrowsCount:CryptoPrice.kt$CryptoPrice.Companion$ @JvmStatic @JvmOverloads @Throws(CryptoException::class) fun apiCall(paths: List<String>, params: Map<String, String> = emptyMap()): String + ThrowsCount:CryptoPrice.kt$CryptoPrice.Companion$ @JvmStatic @JvmOverloads @Throws(CryptoException::class) fun apiCall(paths: List<String>, params: Map<String, String> = emptyMap()): String UtilityClassWithPublicConstructor:CryptoPrice.kt$CryptoPrice diff --git a/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt b/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt index ac7cb54..0e28eee 100644 --- a/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt +++ b/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt @@ -42,6 +42,16 @@ import java.time.LocalDate data class Price(val base: String, val currency: String, val amount: Double) +fun String.toPrice(): Price { + val json = JSONObject(this) + if (json.has("data")) { + val data = json.getJSONObject("data") + return Price(data.getString("base"), data.getString("currency"), data.getString("amount").toDouble()) + } else { + throw CryptoException("Missing JSON data.") + } +} + open class CryptoPrice private constructor() { companion object { // Coinbase API URL @@ -101,13 +111,7 @@ open class CryptoPrice private constructor() { params.put("date", "$date") } val body = apiCall(listOf("prices", "$base-$currency", "spot"), params) - val json = JSONObject(body) - if (json.has("data")) { - val data = json.getJSONObject("data") - return Price(data.getString("base"), data.getString("currency"), data.getString("amount").toDouble()) - } else { - throw CryptoException("Missing JSON data.") - } + return body.toPrice() } } } diff --git a/src/test/kotlin/net/thauvin/erik/cryptoprice/CryptoPriceTest.kt b/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt similarity index 67% rename from src/test/kotlin/net/thauvin/erik/cryptoprice/CryptoPriceTest.kt rename to src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt index dfd5da6..5cc8582 100644 --- a/src/test/kotlin/net/thauvin/erik/cryptoprice/CryptoPriceTest.kt +++ b/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt @@ -42,24 +42,33 @@ class CryptoPriceTest { @Throws(CryptoException::class) fun testBCHPrice() { val price = marketPrice("BCH", "GBP", LocalDate.now().minusDays(1)) - assertEquals(price.base, "BCH", "ETH2") + assertEquals(price.base, "BCH", "BCH") assertEquals(price.currency, "GBP", "is GBP") assertTrue(price.amount > 0.00, "BCH > 0") } - + @Test @Throws(CryptoException::class) fun testMarketPriceExceptions() { assertFailsWith( - message = "FOO did not fail", - exceptionClass = CryptoException::class, - block = { marketPrice("FOO") } + message = "FOO did not fail", + exceptionClass = CryptoException::class, + block = { marketPrice("FOO") } ) assertFailsWith( - message = "BAR did not fail", - exceptionClass = CryptoException::class, - block = { marketPrice("BTC", "BAR") } + message = "BAR did not fail", + exceptionClass = CryptoException::class, + block = { marketPrice("BTC", "BAR") } ) } + + @Test + @Throws(CryptoException::class) + fun testToPrice() { + val price = "{\"data\":{\"base\":\"BTC\",\"currency\":\"USD\",\"amount\":\"57515.69\"}}".toPrice() + assertEquals(price.base, "BTC", "base is BTC") + assertEquals(price.currency, "USD", "currency is USD") + assertEquals(price.amount, "57515.69".toDouble(), "amount is 57515.69") + } }