Added toPrice() extension.

This commit is contained in:
Erik C. Thauvin 2021-05-08 10:21:12 -07:00
parent cd76f2d9cb
commit 8cb6ac4c0b
3 changed files with 31 additions and 18 deletions

View file

@ -1,8 +1,8 @@
<?xml version="1.0" ?> <?xml version='1.0' encoding='UTF-8'?>
<SmellBaseline> <SmellBaseline>
<ManuallySuppressedIssues></ManuallySuppressedIssues> <ManuallySuppressedIssues/>
<CurrentIssues> <CurrentIssues>
<ID>ThrowsCount:CryptoPrice.kt$CryptoPrice.Companion$ @JvmStatic @JvmOverloads @Throws(CryptoException::class) fun apiCall(paths: List&lt;String&gt;, params: Map&lt;String, String&gt; = emptyMap()): String</ID> <ID>ThrowsCount:CryptoPrice.kt$CryptoPrice.Companion$ @JvmStatic @JvmOverloads @Throws(CryptoException::class) fun apiCall(paths: List&lt;String>, params: Map&lt;String, String> = emptyMap()): String</ID>
<ID>UtilityClassWithPublicConstructor:CryptoPrice.kt$CryptoPrice</ID> <ID>UtilityClassWithPublicConstructor:CryptoPrice.kt$CryptoPrice</ID>
</CurrentIssues> </CurrentIssues>
</SmellBaseline> </SmellBaseline>

View file

@ -42,6 +42,16 @@ import java.time.LocalDate
data class Price(val base: String, val currency: String, val amount: Double) 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() { open class CryptoPrice private constructor() {
companion object { companion object {
// Coinbase API URL // Coinbase API URL
@ -101,13 +111,7 @@ open class CryptoPrice private constructor() {
params.put("date", "$date") params.put("date", "$date")
} }
val body = apiCall(listOf("prices", "$base-$currency", "spot"), params) val body = apiCall(listOf("prices", "$base-$currency", "spot"), params)
val json = JSONObject(body) return body.toPrice()
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.")
}
} }
} }
} }

View file

@ -42,7 +42,7 @@ class CryptoPriceTest {
@Throws(CryptoException::class) @Throws(CryptoException::class)
fun testBCHPrice() { fun testBCHPrice() {
val price = marketPrice("BCH", "GBP", LocalDate.now().minusDays(1)) 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") assertEquals(price.currency, "GBP", "is GBP")
assertTrue(price.amount > 0.00, "BCH > 0") assertTrue(price.amount > 0.00, "BCH > 0")
} }
@ -62,4 +62,13 @@ class CryptoPriceTest {
block = { marketPrice("BTC", "BAR") } 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")
}
} }