Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove currency flag #50

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ data class ConversionRate(
val currency: String,
val symbol: String,
val name: String,
val flag: String,
val rate: Float
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.sujanpoudel.playdeals.jobs

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import io.vertx.core.json.Json
import io.vertx.ext.web.client.WebClient
import io.vertx.ext.web.client.WebClientOptions
Expand All @@ -19,10 +21,21 @@ import org.kodein.di.instance
import java.time.Duration
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.util.Currency
import java.util.Locale
import java.util.UUID

data class Currency(
val name: String,
val symbol: String
)

fun loadCurrencies(): HashMap<String, me.sujanpoudel.playdeals.jobs.Currency> {
return Gson().fromJson(
Thread.currentThread().contextClassLoader.getResource("currencies.json")?.readText() ?: "{}",
object : TypeToken<HashMap<String, me.sujanpoudel.playdeals.jobs.Currency>>() {}
)
}

class ForexFetcher(
override val di: DI,
private val conf: Conf
Expand All @@ -48,6 +61,8 @@ class ForexFetcher(
}

private suspend fun getForexRates(): ForexRate {
val currencies = loadCurrencies()

val response = webClient.get("/v1/latest?access_key=${conf.forexApiKey}&format=1&base=EUR")
.send()
.await()
Expand All @@ -59,21 +74,15 @@ class ForexFetcher(
val epochSeconds = response.getLong("timestamp")
val usdRate = response.getJsonObject("rates").getNumber("USD").toFloat()

val currencyLocale = Locale.getAvailableLocales().associateBy {
kotlin.runCatching { Currency.getInstance(it) }.getOrNull() ?: Locale.getDefault()
}

return ForexRate(
timestamp = OffsetDateTime.ofInstant(java.time.Instant.ofEpochSecond(epochSeconds), ZoneOffset.UTC),
rates = response.getJsonObject("rates").map {
val currency = kotlin.runCatching { Currency.getInstance(it.key) }.getOrNull()
val locale = currencyLocale.getOrDefault(currency, Locale.US)
val currency = currencies[it.key]

ConversionRate(
currency = it.key,
symbol = currency?.getSymbol(locale) ?: "$",
name = currency?.displayName ?: it.key,
flag = locale.flagEmoji,
symbol = currency?.symbol ?: "$",
name = currency?.name ?: it.key,
rate = (it.value as Number).toFloat() / usdRate
)
}
Expand Down
Loading