Added examples

This commit is contained in:
Erik C. Thauvin 2021-05-08 15:50:33 -07:00
parent 8cb6ac4c0b
commit 3517acf544
10 changed files with 139 additions and 33 deletions

View file

@ -40,23 +40,29 @@ import java.io.IOException
import java.net.URL
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() {
/**
* The `CryptoPrice` class
*/
open class CryptoPrice(val base: String, val currency: String, val amount: Double) {
companion object {
// Coinbase API URL
private const val COINBASE_API_URL = "https://api.coinbase.com/v2/"
@JvmStatic
fun String.toPrice(): CryptoPrice {
val json = JSONObject(this)
if (json.has("data")) {
val data = json.getJSONObject("data")
return CryptoPrice(
data.getString("base"),
data.getString("currency"),
data.getString("amount").toDouble()
)
} else {
throw CryptoException("Missing JSON data.")
}
}
/**
* Make an API call.
*/
@ -105,7 +111,7 @@ open class CryptoPrice private constructor() {
@JvmStatic
@JvmOverloads
@Throws(CryptoException::class)
fun marketPrice(base: String, currency: String = "USD", date: LocalDate? = null): Price {
fun marketPrice(base: String, currency: String = "USD", date: LocalDate? = null): CryptoPrice {
val params = mutableMapOf<String, String>()
if (date != null) {
params.put("date", "$date")

View file

@ -1,6 +1,7 @@
package net.thauvin.erik.crypto
import net.thauvin.erik.crypto.CryptoPrice.Companion.marketPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.toPrice
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith