From 3517acf5442b4cbc6811c1a5ba4e566fe38adec2 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 8 May 2021 15:50:33 -0700 Subject: [PATCH] Added examples --- baseline.xml | 1 - build.gradle.kts | 18 -------- examples/.gitattributes | 6 +++ examples/.gitignore | 5 +++ examples/build.gradle.kts | 31 ++++++++++++++ examples/settings.gradle.kts | 10 +++++ .../java/com/example/CryptoPriceSample.java | 41 +++++++++++++++++++ .../kotlin/com/example/CryptoPriceExample.kt | 25 +++++++++++ .../net/thauvin/erik/crypto/CryptoPrice.kt | 34 ++++++++------- .../thauvin/erik/crypto/CryptoPriceTest.kt | 1 + 10 files changed, 139 insertions(+), 33 deletions(-) create mode 100644 examples/.gitattributes create mode 100644 examples/.gitignore create mode 100644 examples/build.gradle.kts create mode 100644 examples/settings.gradle.kts create mode 100644 examples/src/main/java/com/example/CryptoPriceSample.java create mode 100644 examples/src/main/kotlin/com/example/CryptoPriceExample.kt diff --git a/baseline.xml b/baseline.xml index d8a3442..7566107 100644 --- a/baseline.xml +++ b/baseline.xml @@ -3,6 +3,5 @@ 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/build.gradle.kts b/build.gradle.kts index 81b756b..b7b882f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -102,24 +102,6 @@ tasks { } } - val copyToDeploy by registering(Copy::class) { - from(configurations.runtimeClasspath) { - exclude("annotations-*.jar") - } - from(jar) - into(deployDir) - } - - - register("deploy") { - description = "Copies all needed files to the $deployDir directory." - group = PublishingPlugin.PUBLISH_TASK_GROUP - dependsOn("build", "jar") - outputs.dir(deployDir) - inputs.files(copyToDeploy) - mustRunAfter("clean") - } - "sonarqube" { dependsOn("jacocoTestReport") } diff --git a/examples/.gitattributes b/examples/.gitattributes new file mode 100644 index 0000000..00a51af --- /dev/null +++ b/examples/.gitattributes @@ -0,0 +1,6 @@ +# +# https://help.github.com/articles/dealing-with-line-endings/ +# +# These are explicitly windows files and should use crlf +*.bat text eol=crlf + diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..1b6985c --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,5 @@ +# Ignore Gradle project-specific cache directory +.gradle + +# Ignore Gradle build output directory +build diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts new file mode 100644 index 0000000..a2b3e3b --- /dev/null +++ b/examples/build.gradle.kts @@ -0,0 +1,31 @@ +plugins { + id("org.jetbrains.kotlin.jvm") version "1.5.0" + id("com.github.ben-manes.versions") version "0.38.0" + application +} + +// ./gradlew run +// ./gradlew runJava" + +defaultTasks(ApplicationPlugin.TASK_RUN_NAME) + +repositories { + mavenLocal() + mavenCentral() +} + +dependencies { + implementation("net.thauvin.erik:cryptoprice:0.9.0-SNAPSHOT") +} + +application { + mainClassName = "com.example.CryptoPriceExampleKt" +} + +tasks { + register("runJava") { + group = "application" + main = "com.example.CryptoPriceSample" + classpath = sourceSets["main"].runtimeClasspath + } +} diff --git a/examples/settings.gradle.kts b/examples/settings.gradle.kts new file mode 100644 index 0000000..ed9e13e --- /dev/null +++ b/examples/settings.gradle.kts @@ -0,0 +1,10 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * The settings file is used to specify which projects to include in your build. + * + * Detailed information about configuring a multi-project build in Gradle can be found + * in the user manual at https://docs.gradle.org/6.5.1/userguide/multi_project_builds.html + */ + +rootProject.name = "examples" diff --git a/examples/src/main/java/com/example/CryptoPriceSample.java b/examples/src/main/java/com/example/CryptoPriceSample.java new file mode 100644 index 0000000..8b24c7e --- /dev/null +++ b/examples/src/main/java/com/example/CryptoPriceSample.java @@ -0,0 +1,41 @@ +package com.example; + +import net.thauvin.erik.crypto.CryptoPrice; +import net.thauvin.erik.crypto.CryptoException; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +public class CryptoPriceSample { + public static void main(String[] args) { + try { + // Get current Bitcoin market price. + final CryptoPrice price = CryptoPrice.marketPrice("BTC"); + System.out.println("The current Bitcoin price is " + price.getAmount() + " in " + price.getCurrency()); + + // Get current Bitcoin market price in Euros. + final CryptoPrice euroPrice = CryptoPrice.marketPrice("BTC", "EUR"); + System.out.println("The current Bitcoin price is " + euroPrice.getAmount() + " in Euros"); + + // Get current Bitcoin buy price using API. + final CryptoPrice buyPrice = + CryptoPrice.toPrice( + CryptoPrice.apiCall( + List.of("prices", "BTC-USD", "buy"), + Collections.emptyMap() + ) + ); + System.out.println("The current BTC buy price is " + price.getAmount() + " in " + price.getCurrency()); + + System.out.println(); + + // Get current Ethereum market price in Pound sterling. + final CryptoPrice gbpPrice = CryptoPrice.marketPrice("ETH", "GBP"); + System.out.println("The current Ethereum price is " + gbpPrice.getAmount() + " in Pound sterling"); + + } catch (CryptoException e) { + System.err.println(e.getMessage()); + } + } +} diff --git a/examples/src/main/kotlin/com/example/CryptoPriceExample.kt b/examples/src/main/kotlin/com/example/CryptoPriceExample.kt new file mode 100644 index 0000000..72c5f6a --- /dev/null +++ b/examples/src/main/kotlin/com/example/CryptoPriceExample.kt @@ -0,0 +1,25 @@ +package com.example + +import net.thauvin.erik.crypto.CryptoPrice.Companion.marketPrice +import net.thauvin.erik.crypto.CryptoPrice.Companion.apiCall +import net.thauvin.erik.crypto.CryptoPrice.Companion.toPrice + +fun main(@Suppress("UNUSED_PARAMETER") args: Array) { + // Get current Bitcoin market price. + var price = marketPrice("BTC") + println("The current Bitcoin price is ${price.amount} in ${price.currency}") + + // Get current Bitcoin market price in Euro. + var euroPrice = marketPrice("BTC", "EUR") + println("The current Bitcoin price is ${euroPrice.amount} in Euros") + + // Get current Bitcoin buy price using API. + var buyPrice = apiCall(listOf("prices", "BTC-USD", "buy"), emptyMap()).toPrice() + println("The current BTC buy price is ${buyPrice.amount} in ${buyPrice.currency}") + + println() + + // Get current Ethereum market price in Pound sterling. + var gbpPrice = marketPrice("ETH", "GBP") + println("The current Ehtereum price is ${gbpPrice.amount} in Pound sterling") +} diff --git a/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt b/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt index 0e28eee..c11f76b 100644 --- a/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt +++ b/src/main/kotlin/net/thauvin/erik/crypto/CryptoPrice.kt @@ -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() if (date != null) { params.put("date", "$date") diff --git a/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt b/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt index 5cc8582..8e82149 100644 --- a/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt +++ b/src/test/kotlin/net/thauvin/erik/crypto/CryptoPriceTest.kt @@ -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