diff --git a/README.md b/README.md index 81404ec..dbafcad 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ final AkismetComment comment = new AkismetComment( .authorUrl("https://www.CheckOutMyCoolSite.com") .dateGmt(Akismet.dateToGmt(new Date())) .content("Thanks for reviewing our software.") - .build + .build() ); //... diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index a418973..078daff 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -1,6 +1,6 @@ - + CyclomaticComplexMethod:Akismet.kt$Akismet$private fun buildFormBody(comment: AkismetComment): FormBody CyclomaticComplexMethod:AkismetComment.kt$AkismetComment$@Suppress("DuplicatedCode") override fun equals(other: Any?): Boolean @@ -9,9 +9,9 @@ MagicNumber:Akismet.kt$Akismet$8 NestedBlockDepth:Akismet.kt$Akismet$@JvmOverloads fun executeMethod(apiUrl: HttpUrl, formBody: FormBody, trueOnError: Boolean = false): Boolean NestedBlockDepth:AkismetExample.kt$fun main(args: Array<String>) - NestedBlockDepth:AkismetTest.kt$fun getKey(key: String): String ReturnCount:Akismet.kt$Akismet$@JvmOverloads fun executeMethod(apiUrl: HttpUrl, formBody: FormBody, trueOnError: Boolean = false): Boolean + ReturnCount:AkismetTest.kt$fun getKey(key: String): String TooManyFunctions:CommentConfig.kt$CommentConfig$Builder - WildcardImport:AkismetTest.kt$import assertk.assertions.* + WildcardImport:AkismetTests.kt$import assertk.assertions.* diff --git a/src/main/kotlin/net/thauvin/erik/akismet/Akismet.kt b/src/main/kotlin/net/thauvin/erik/akismet/Akismet.kt index 1824f45..055d380 100644 --- a/src/main/kotlin/net/thauvin/erik/akismet/Akismet.kt +++ b/src/main/kotlin/net/thauvin/erik/akismet/Akismet.kt @@ -233,7 +233,7 @@ open class Akismet(apiKey: String) { * * See the [Akismet API](https://akismet.com/development/api/#verify-key) for more details. * - * @return `true` if the key is valid, `false` otherwise. + * @return `true` if the key is valid, `false` otherwise * @see [Akismet.isVerifiedKey] */ fun verifyKey(): Boolean { @@ -258,8 +258,8 @@ open class Akismet(apiKey: String) { * * See the [Akismet API](https://akismet.com/development/api/#comment-check) for more details. * - * @param trueOnError Set to return `true` on error. - * @return `true` if the comment is spam, `false` if the comment is not. + * @param trueOnError Set to return `true` on error + * @return `true` if the comment is spam, `false` if the comment is not */ @JvmOverloads fun checkComment(comment: AkismetComment, trueOnError: Boolean = false): Boolean { @@ -280,7 +280,7 @@ open class Akismet(apiKey: String) { * * See the [Akismet API](https://akismet.com/development/api/#submit-spam) for more details. * - * @return `true` if the comment was submitted, `false` otherwise. + * @return `true` if the comment was submitted, `false` otherwise */ fun submitSpam(comment: AkismetComment): Boolean { return executeMethod("submit-spam".toApiUrl(), buildFormBody(comment)) @@ -302,7 +302,7 @@ open class Akismet(apiKey: String) { * * See the [Akismet API](https://akismet.com/development/api/#submit-ham) for more details. * - * @return `true` if the comment was submitted, `false` otherwise. + * @return `true` if the comment was submitted, `false` otherwise */ fun submitHam(comment: AkismetComment): Boolean { return executeMethod("submit-ham".toApiUrl(), buildFormBody(comment)) @@ -311,8 +311,8 @@ open class Akismet(apiKey: String) { /** * Executes a call to an Akismet REST API method. * - * @param apiUrl The Akismet API URL endpoint. (e.g. https://rest.akismet.com/1.1/verify-key) - * @param formBody The HTTP POST form body containing the request parameters to be submitted. + * @param apiUrl The Akismet API URL endpoint. (e.g., https://rest.akismet.com/1.1/verify-key) + * @param formBody The HTTP POST form body containing the request parameters to be submitted * @param trueOnError Set to return `true` on error (IO, empty response, etc.) */ @JvmOverloads diff --git a/src/main/kotlin/net/thauvin/erik/akismet/AkismetComment.kt b/src/main/kotlin/net/thauvin/erik/akismet/AkismetComment.kt index 400fc7e..80f0193 100644 --- a/src/main/kotlin/net/thauvin/erik/akismet/AkismetComment.kt +++ b/src/main/kotlin/net/thauvin/erik/akismet/AkismetComment.kt @@ -33,9 +33,7 @@ package net.thauvin.erik.akismet import jakarta.servlet.http.HttpServletRequest import kotlinx.serialization.Serializable -import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json -import kotlin.collections.set private fun String?.ifNull() = this ?: "" @@ -52,8 +50,8 @@ private fun String?.ifNull() = this ?: "" * * See the [Akismet API](https://akismet.com/development/api/#comment-check) for more details. * - * @param userIp IP address of the comment submitter. - * @param userAgent User agent string of the web browser submitting the comment. + * @param userIp IP address of the comment submitter + * @param userAgent User agent string of the web browser submitting the comment */ @Serializable open class AkismetComment(val userIp: String, val userAgent: String) { diff --git a/src/main/kotlin/net/thauvin/erik/akismet/CommentConfig.kt b/src/main/kotlin/net/thauvin/erik/akismet/CommentConfig.kt index 53b9295..6777487 100644 --- a/src/main/kotlin/net/thauvin/erik/akismet/CommentConfig.kt +++ b/src/main/kotlin/net/thauvin/erik/akismet/CommentConfig.kt @@ -56,8 +56,8 @@ class CommentConfig private constructor(builder: Builder) { /** * Provides a configuration builder. * - * @param userIp IP address of the comment submitter. - * @param userAgent User agent string of the web browser submitting the comment. + * @param userIp IP address of the comment submitter + * @param userAgent User agent string of the web browser submitting the comment */ data class Builder(var userIp: String, var userAgent: String) { var referrer = "" diff --git a/src/test/kotlin/net/thauvin/erik/akismet/AkismetTests.kt b/src/test/kotlin/net/thauvin/erik/akismet/AkismetTests.kt index 676158c..f4f4363 100644 --- a/src/test/kotlin/net/thauvin/erik/akismet/AkismetTests.kt +++ b/src/test/kotlin/net/thauvin/erik/akismet/AkismetTests.kt @@ -144,21 +144,8 @@ class AkismetTests { } private fun getKey(key: String): String { - var value = System.getenv(key) ?: "" - if (value.isBlank()) { - val localProps = File("local.properties") - localProps.apply { - if (exists()) { - FileInputStream(this).use { fis -> - Properties().apply { - load(fis) - value = getProperty(key, "") - } - } - } - } - } - return value + return System.getenv(key)?.takeUnless { it.isBlank() } + ?: loadPropertyValue(key) } private fun getMockRequest(): HttpServletRequest { @@ -176,6 +163,16 @@ class AkismetTests { } return request } + + private fun loadPropertyValue(key: String): String { + return File("local.properties") + .takeIf { it.exists() } + ?.let { file -> + FileInputStream(file).use { fis -> + Properties().apply { load(fis) }.getProperty(key, "") + } + }.orEmpty() + } } @Nested