Changed "symbols" keyword to "codes" in CurrencyConverter
This commit is contained in:
parent
92634031a8
commit
2eb1c314cf
4 changed files with 38 additions and 33 deletions
|
@ -43,8 +43,8 @@
|
||||||
<ID>NestedBlockDepth:Addons.kt$Addons$ fun add(command: AbstractCommand)</ID>
|
<ID>NestedBlockDepth:Addons.kt$Addons$ fun add(command: AbstractCommand)</ID>
|
||||||
<ID>NestedBlockDepth:Addons.kt$Addons$ fun add(module: AbstractModule)</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: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 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<String?>)</ID>
|
<ID>NestedBlockDepth:EntryLink.kt$EntryLink$ private fun setTags(tags: List<String?>)</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 @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>
|
<ID>NestedBlockDepth:FeedsMgr.kt$FeedsMgr.Companion$ @JvmStatic fun saveFeed(entries: Entries, currentFile: String = currentXml)</ID>
|
||||||
|
|
|
@ -48,6 +48,7 @@ import org.slf4j.LoggerFactory
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The CurrencyConverter module.
|
* The CurrencyConverter module.
|
||||||
*/
|
*/
|
||||||
|
@ -59,7 +60,7 @@ class CurrencyConverter : ThreadedModule() {
|
||||||
override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) {
|
override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) {
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
if (pubDate != today()) {
|
if (pubDate != today()) {
|
||||||
SYMBOLS.clear()
|
CODES.clear()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super.commandResponse(channel, cmd, args, event)
|
super.commandResponse(channel, cmd, args, event)
|
||||||
|
@ -69,40 +70,40 @@ class CurrencyConverter : ThreadedModule() {
|
||||||
* Converts the specified currencies.
|
* Converts the specified currencies.
|
||||||
*/
|
*/
|
||||||
override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) {
|
override fun run(channel: String, cmd: String, args: String, event: GenericMessageEvent) {
|
||||||
if (SYMBOLS.isEmpty()) {
|
if (CODES.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
loadSymbols()
|
loadCodes()
|
||||||
} catch (e: ModuleException) {
|
} catch (e: ModuleException) {
|
||||||
if (logger.isWarnEnabled) logger.warn(e.debugMessage, e)
|
if (logger.isWarnEnabled) logger.warn(e.debugMessage, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SYMBOLS.isEmpty()) {
|
if (CODES.isEmpty()) {
|
||||||
event.respond(EMPTY_SYMBOLS_TABLE)
|
event.respond(EMPTY_CODES_TABLE)
|
||||||
} else if (args.matches("\\d+([,\\d]+)?(\\.\\d+)? [a-zA-Z]{3}+ to [a-zA-Z]{3}+".toRegex())) {
|
} else if (args.matches("\\d+([,\\d]+)?(\\.\\d+)? [a-zA-Z]{3}+ to [a-zA-Z]{3}+".toRegex())) {
|
||||||
val msg = convertCurrency(args)
|
val msg = convertCurrency(args)
|
||||||
event.respond(msg.msg)
|
event.respond(msg.msg)
|
||||||
if (msg.isError) {
|
if (msg.isError) {
|
||||||
helpResponse(event)
|
helpResponse(event)
|
||||||
}
|
}
|
||||||
} else if (args.contains(CURRENCY_SYMBOLS_KEYWORD)) {
|
} else if (args.contains(CURRENCY_CODES_KEYWORD)) {
|
||||||
event.sendMessage("The supported currency symbols are: ")
|
event.sendMessage("The supported currency codes are: ")
|
||||||
event.sendList(ArrayList(SYMBOLS.keys.sorted()), 11, isIndent = true)
|
event.sendList(ArrayList(CODES.keys.sorted()), 11, isIndent = true)
|
||||||
} else {
|
} else {
|
||||||
helpResponse(event)
|
helpResponse(event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun helpResponse(event: GenericMessageEvent): Boolean {
|
override fun helpResponse(event: GenericMessageEvent): Boolean {
|
||||||
if (SYMBOLS.isEmpty()) {
|
if (CODES.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
loadSymbols()
|
loadCodes()
|
||||||
} catch (e: ModuleException) {
|
} catch (e: ModuleException) {
|
||||||
if (logger.isWarnEnabled) logger.warn(e.debugMessage, e)
|
if (logger.isWarnEnabled) logger.warn(e.debugMessage, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (SYMBOLS.isEmpty()) {
|
if (CODES.isEmpty()) {
|
||||||
event.sendMessage(EMPTY_SYMBOLS_TABLE)
|
event.sendMessage(EMPTY_CODES_TABLE)
|
||||||
} else {
|
} else {
|
||||||
val nick = event.bot().nick
|
val nick = event.bot().nick
|
||||||
event.sendMessage("To convert from one currency to another:")
|
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)
|
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(
|
event.sendMessage(
|
||||||
helpFormat(
|
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
|
// Currency command
|
||||||
private const val CURRENCY_CMD = "currency"
|
private const val CURRENCY_CMD = "currency"
|
||||||
|
|
||||||
// Currency synbols keywords
|
// Currency codes keyword
|
||||||
private const val CURRENCY_SYMBOLS_KEYWORD = "symbols"
|
private const val CURRENCY_CODES_KEYWORD = "codes"
|
||||||
|
|
||||||
// Empty symbols table.
|
// Empty code table.
|
||||||
private const val EMPTY_SYMBOLS_TABLE = "Sorry, but the currency symbols table is empty."
|
private const val EMPTY_CODES_TABLE = "Sorry, but the currency table is empty."
|
||||||
|
|
||||||
// Currency Symbols
|
// Currency codes
|
||||||
private val SYMBOLS: MutableMap<String, String> = mutableMapOf()
|
private val CODES: MutableMap<String, String> = mutableMapOf()
|
||||||
|
|
||||||
// Last exchange rates table publication date
|
// Last exchange rates table publication date
|
||||||
private var pubDate = ""
|
private var pubDate = ""
|
||||||
|
@ -150,7 +151,7 @@ class CurrencyConverter : ThreadedModule() {
|
||||||
} else {
|
} else {
|
||||||
val to = cmds[1].uppercase()
|
val to = cmds[1].uppercase()
|
||||||
val from = cmds[3].uppercase()
|
val from = cmds[3].uppercase()
|
||||||
if (SYMBOLS.contains(to) && SYMBOLS.contains(from)) {
|
if (CODES.contains(to) && CODES.contains(from)) {
|
||||||
try {
|
try {
|
||||||
val amt = cmds[0].replace(",", "")
|
val amt = cmds[0].replace(",", "")
|
||||||
val url = URL("https://api.exchangerate.host/convert?from=$to&to=$from&amount=$amt")
|
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")) {
|
if (json.getBoolean("success")) {
|
||||||
PublicMessage(
|
PublicMessage(
|
||||||
"${cmds[0]} ${SYMBOLS[to]} = ${json.get("result")} ${SYMBOLS[from]}"
|
"${cmds[0]} ${CODES[to]} = ${json.getDouble("result")} ${CODES[from]}"
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
ErrorMessage("Sorry, an error occurred while converting the currencies.")
|
ErrorMessage("Sorry, an error occurred while converting the currencies.")
|
||||||
|
@ -173,23 +174,26 @@ class CurrencyConverter : ThreadedModule() {
|
||||||
} else ErrorMessage("Invalid query. Let's try again.")
|
} else ErrorMessage("Invalid query. Let's try again.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the country ISO codes.
|
||||||
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@Throws(ModuleException::class)
|
@Throws(ModuleException::class)
|
||||||
fun loadSymbols() {
|
fun loadCodes() {
|
||||||
if (SYMBOLS.isEmpty()) {
|
if (CODES.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
val url = URL("https://api.exchangerate.host/symbols")
|
val url = URL("https://api.exchangerate.host/symbols")
|
||||||
val json = JSONObject(url.reader())
|
val json = JSONObject(url.reader())
|
||||||
if (json.getBoolean("success")) {
|
if (json.getBoolean("success")) {
|
||||||
val symbols = json.getJSONObject("symbols")
|
val symbols = json.getJSONObject("symbols")
|
||||||
for (key in symbols.keys()) {
|
for (key in symbols.keys()) {
|
||||||
SYMBOLS[key] = symbols.getJSONObject(key).getString("description")
|
CODES[key] = symbols.getJSONObject(key).getString("description")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
throw ModuleException(
|
throw ModuleException(
|
||||||
"loadSymbols(): IOE",
|
"loadCodes(): IOE",
|
||||||
"An IO error has occurred while retrieving the currency symbols table.",
|
"An IO error has occurred while retrieving the currency table.",
|
||||||
e
|
e
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,13 +38,14 @@ import assertk.assertions.isInstanceOf
|
||||||
import assertk.assertions.matches
|
import assertk.assertions.matches
|
||||||
import assertk.assertions.prop
|
import assertk.assertions.prop
|
||||||
import net.thauvin.erik.mobibot.modules.CurrencyConverter.Companion.convertCurrency
|
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.ErrorMessage
|
||||||
import net.thauvin.erik.mobibot.msg.Message
|
import net.thauvin.erik.mobibot.msg.Message
|
||||||
import net.thauvin.erik.mobibot.msg.PublicMessage
|
import net.thauvin.erik.mobibot.msg.PublicMessage
|
||||||
import org.testng.annotations.BeforeClass
|
import org.testng.annotations.BeforeClass
|
||||||
import org.testng.annotations.Test
|
import org.testng.annotations.Test
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The `CurrencyConvertTest` class.
|
* The `CurrencyConvertTest` class.
|
||||||
*/
|
*/
|
||||||
|
@ -52,7 +53,7 @@ class CurrencyConverterTest {
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
@Throws(ModuleException::class)
|
@Throws(ModuleException::class)
|
||||||
fun before() {
|
fun before() {
|
||||||
loadSymbols()
|
loadCodes()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
#Generated by the Semver Plugin for Gradle
|
#Generated by the Semver Plugin for Gradle
|
||||||
#Sun Jul 10 14:28:56 PDT 2022
|
#Sun Jul 10 23:07:38 PDT 2022
|
||||||
version.buildmeta=258
|
version.buildmeta=315
|
||||||
version.major=0
|
version.major=0
|
||||||
version.minor=8
|
version.minor=8
|
||||||
version.patch=0
|
version.patch=0
|
||||||
version.prerelease=rc
|
version.prerelease=rc
|
||||||
version.project=mobibot
|
version.project=mobibot
|
||||||
version.semver=0.8.0-rc+258
|
version.semver=0.8.0-rc+315
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue