Added toPrice() extension.
This commit is contained in:
parent
cd76f2d9cb
commit
8cb6ac4c0b
3 changed files with 31 additions and 18 deletions
|
@ -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<String>, params: Map<String, String> = emptyMap()): String</ID>
|
<ID>ThrowsCount:CryptoPrice.kt$CryptoPrice.Companion$ @JvmStatic @JvmOverloads @Throws(CryptoException::class) fun apiCall(paths: List<String>, params: Map<String, String> = emptyMap()): String</ID>
|
||||||
<ID>UtilityClassWithPublicConstructor:CryptoPrice.kt$CryptoPrice</ID>
|
<ID>UtilityClassWithPublicConstructor:CryptoPrice.kt$CryptoPrice</ID>
|
||||||
</CurrentIssues>
|
</CurrentIssues>
|
||||||
</SmellBaseline>
|
</SmellBaseline>
|
||||||
|
|
|
@ -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.")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,24 +42,33 @@ 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")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Throws(CryptoException::class)
|
@Throws(CryptoException::class)
|
||||||
fun testMarketPriceExceptions() {
|
fun testMarketPriceExceptions() {
|
||||||
assertFailsWith(
|
assertFailsWith(
|
||||||
message = "FOO did not fail",
|
message = "FOO did not fail",
|
||||||
exceptionClass = CryptoException::class,
|
exceptionClass = CryptoException::class,
|
||||||
block = { marketPrice("FOO") }
|
block = { marketPrice("FOO") }
|
||||||
)
|
)
|
||||||
|
|
||||||
assertFailsWith(
|
assertFailsWith(
|
||||||
message = "BAR did not fail",
|
message = "BAR did not fail",
|
||||||
exceptionClass = CryptoException::class,
|
exceptionClass = CryptoException::class,
|
||||||
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")
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue