Changed "symbols" keyword to "codes" in CurrencyConverter

This commit is contained in:
Erik C. Thauvin 2022-07-10 23:14:49 -07:00
parent 92634031a8
commit 2eb1c314cf
4 changed files with 38 additions and 33 deletions

View file

@ -43,8 +43,8 @@
<ID>NestedBlockDepth:Addons.kt$Addons$ fun add(command: AbstractCommand)</ID>
<ID>NestedBlockDepth:Addons.kt$Addons$ fun add(module: AbstractModule)</ID>
<ID>NestedBlockDepth:Comment.kt$Comment$override fun commandResponse(channel: String, args: String, event: GenericMessageEvent)</ID>
<ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter.Companion$ @JvmStatic @Throws(ModuleException::class) fun loadCodes()</ID>
<ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter.Companion$ @JvmStatic fun convertCurrency(query: String): Message</ID>
<ID>NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter.Companion$@JvmStatic @Throws(ModuleException::class) fun loadSymbols()</ID>
<ID>NestedBlockDepth:EntryLink.kt$EntryLink$ private fun setTags(tags: List&lt;String?&gt;)</ID>
<ID>NestedBlockDepth:FeedsMgr.kt$FeedsMgr.Companion$ @JvmStatic @Throws(IOException::class, FeedException::class) fun loadFeed(entries: Entries, currentFile: String = currentXml): String</ID>
<ID>NestedBlockDepth:FeedsMgr.kt$FeedsMgr.Companion$ @JvmStatic fun saveFeed(entries: Entries, currentFile: String = currentXml)</ID>

View file

@ -48,6 +48,7 @@ import org.slf4j.LoggerFactory
import java.io.IOException
import java.net.URL
/**
* The CurrencyConverter module.
*/
@ -59,7 +60,7 @@ class CurrencyConverter : ThreadedModule() {
override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) {
synchronized(this) {
if (pubDate != today()) {
SYMBOLS.clear()
CODES.clear()
}
}
super.commandResponse(channel, cmd, args, event)
@ -69,40 +70,40 @@ class CurrencyConverter : ThreadedModule() {
* Converts the specified currencies.
*/
override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) {
if (SYMBOLS.isEmpty()) {
if (CODES.isEmpty()) {
try {
loadSymbols()
loadCodes()
} catch (e: ModuleException) {
if (logger.isWarnEnabled) logger.warn(e.debugMessage, e)
}
}
if (SYMBOLS.isEmpty()) {
event.respond(EMPTY_SYMBOLS_TABLE)
if (CODES.isEmpty()) {
event.respond(EMPTY_CODES_TABLE)
} else if (args.matches("\\d+([,\\d]+)?(\\.\\d+)? [a-zA-Z]{3}+ to [a-zA-Z]{3}+".toRegex())) {
val msg = convertCurrency(args)
event.respond(msg.msg)
if (msg.isError) {
helpResponse(event)
}
} else if (args.contains(CURRENCY_SYMBOLS_KEYWORD)) {
event.sendMessage("The supported currency symbols are: ")
event.sendList(ArrayList(SYMBOLS.keys.sorted()), 11, isIndent = true)
} else if (args.contains(CURRENCY_CODES_KEYWORD)) {
event.sendMessage("The supported currency codes are: ")
event.sendList(ArrayList(CODES.keys.sorted()), 11, isIndent = true)
} else {
helpResponse(event)
}
}
override fun helpResponse(event: GenericMessageEvent): Boolean {
if (SYMBOLS.isEmpty()) {
if (CODES.isEmpty()) {
try {
loadSymbols()
loadCodes()
} catch (e: ModuleException) {
if (logger.isWarnEnabled) logger.warn(e.debugMessage, e)
}
}
if (SYMBOLS.isEmpty()) {
event.sendMessage(EMPTY_SYMBOLS_TABLE)
if (CODES.isEmpty()) {
event.sendMessage(EMPTY_CODES_TABLE)
} else {
val nick = event.bot().nick
event.sendMessage("To convert from one currency to another:")
@ -112,10 +113,10 @@ class CurrencyConverter : ThreadedModule() {
helpCmdSyntax("%c $CURRENCY_CMD 50,000 GBP to BTC", nick, isPrivateMsgEnabled)
)
)
event.sendMessage("To list the supported currency symbols: ")
event.sendMessage("To list the supported currency codes: ")
event.sendMessage(
helpFormat(
helpCmdSyntax("%c $CURRENCY_CMD $CURRENCY_SYMBOLS_KEYWORD", nick, isPrivateMsgEnabled)
helpCmdSyntax("%c $CURRENCY_CMD $CURRENCY_CODES_KEYWORD", nick, isPrivateMsgEnabled)
)
)
}
@ -126,14 +127,14 @@ class CurrencyConverter : ThreadedModule() {
// Currency command
private const val CURRENCY_CMD = "currency"
// Currency synbols keywords
private const val CURRENCY_SYMBOLS_KEYWORD = "symbols"
// Currency codes keyword
private const val CURRENCY_CODES_KEYWORD = "codes"
// Empty symbols table.
private const val EMPTY_SYMBOLS_TABLE = "Sorry, but the currency symbols table is empty."
// Empty code table.
private const val EMPTY_CODES_TABLE = "Sorry, but the currency table is empty."
// Currency Symbols
private val SYMBOLS: MutableMap<String, String> = mutableMapOf()
// Currency codes
private val CODES: MutableMap<String, String> = mutableMapOf()
// Last exchange rates table publication date
private var pubDate = ""
@ -150,7 +151,7 @@ class CurrencyConverter : ThreadedModule() {
} else {
val to = cmds[1].uppercase()
val from = cmds[3].uppercase()
if (SYMBOLS.contains(to) && SYMBOLS.contains(from)) {
if (CODES.contains(to) && CODES.contains(from)) {
try {
val amt = cmds[0].replace(",", "")
val url = URL("https://api.exchangerate.host/convert?from=$to&to=$from&amount=$amt")
@ -158,7 +159,7 @@ class CurrencyConverter : ThreadedModule() {
if (json.getBoolean("success")) {
PublicMessage(
"${cmds[0]} ${SYMBOLS[to]} = ${json.get("result")} ${SYMBOLS[from]}"
"${cmds[0]} ${CODES[to]} = ${json.getDouble("result")} ${CODES[from]}"
)
} else {
ErrorMessage("Sorry, an error occurred while converting the currencies.")
@ -173,23 +174,26 @@ class CurrencyConverter : ThreadedModule() {
} else ErrorMessage("Invalid query. Let's try again.")
}
/**
* Loads the country ISO codes.
*/
@JvmStatic
@Throws(ModuleException::class)
fun loadSymbols() {
if (SYMBOLS.isEmpty()) {
fun loadCodes() {
if (CODES.isEmpty()) {
try {
val url = URL("https://api.exchangerate.host/symbols")
val json = JSONObject(url.reader())
if (json.getBoolean("success")) {
val symbols = json.getJSONObject("symbols")
for (key in symbols.keys()) {
SYMBOLS[key] = symbols.getJSONObject(key).getString("description")
CODES[key] = symbols.getJSONObject(key).getString("description")
}
}
} catch (e: IOException) {
throw ModuleException(
"loadSymbols(): IOE",
"An IO error has occurred while retrieving the currency symbols table.",
"loadCodes(): IOE",
"An IO error has occurred while retrieving the currency table.",
e
)
}

View file

@ -38,13 +38,14 @@ import assertk.assertions.isInstanceOf
import assertk.assertions.matches
import assertk.assertions.prop
import net.thauvin.erik.mobibot.modules.CurrencyConverter.Companion.convertCurrency
import net.thauvin.erik.mobibot.modules.CurrencyConverter.Companion.loadSymbols
import net.thauvin.erik.mobibot.modules.CurrencyConverter.Companion.loadCodes
import net.thauvin.erik.mobibot.msg.ErrorMessage
import net.thauvin.erik.mobibot.msg.Message
import net.thauvin.erik.mobibot.msg.PublicMessage
import org.testng.annotations.BeforeClass
import org.testng.annotations.Test
/**
* The `CurrencyConvertTest` class.
*/
@ -52,7 +53,7 @@ class CurrencyConverterTest {
@BeforeClass
@Throws(ModuleException::class)
fun before() {
loadSymbols()
loadCodes()
}
@Test

View file

@ -1,9 +1,9 @@
#Generated by the Semver Plugin for Gradle
#Sun Jul 10 14:28:56 PDT 2022
version.buildmeta=258
#Sun Jul 10 23:07:38 PDT 2022
version.buildmeta=315
version.major=0
version.minor=8
version.patch=0
version.prerelease=rc
version.project=mobibot
version.semver=0.8.0-rc+258
version.semver=0.8.0-rc+315