diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index 565e84b..9c3a1f7 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -43,8 +43,8 @@ NestedBlockDepth:Addons.kt$Addons$ fun add(command: AbstractCommand) NestedBlockDepth:Addons.kt$Addons$ fun add(module: AbstractModule) NestedBlockDepth:Comment.kt$Comment$override fun commandResponse(channel: String, args: String, event: GenericMessageEvent) + NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter.Companion$ @JvmStatic @Throws(ModuleException::class) fun loadCodes() NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter.Companion$ @JvmStatic fun convertCurrency(query: String): Message - NestedBlockDepth:CurrencyConverter.kt$CurrencyConverter.Companion$@JvmStatic @Throws(ModuleException::class) fun loadSymbols() NestedBlockDepth:EntryLink.kt$EntryLink$ private fun setTags(tags: List<String?>) NestedBlockDepth:FeedsMgr.kt$FeedsMgr.Companion$ @JvmStatic @Throws(IOException::class, FeedException::class) fun loadFeed(entries: Entries, currentFile: String = currentXml): String NestedBlockDepth:FeedsMgr.kt$FeedsMgr.Companion$ @JvmStatic fun saveFeed(entries: Entries, currentFile: String = currentXml) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt index 0afd499..e448e27 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverter.kt @@ -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 = mutableMapOf() + // Currency codes + private val CODES: MutableMap = 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 ) } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverterTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverterTest.kt index a0f4ce4..1e9a346 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverterTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/CurrencyConverterTest.kt @@ -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 diff --git a/version.properties b/version.properties index 0ca78d0..3d7c19e 100644 --- a/version.properties +++ b/version.properties @@ -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