Minor cleanups

This commit is contained in:
Erik C. Thauvin 2025-05-28 20:21:17 -07:00
parent 80fac24b4d
commit c9cd086ba1
Signed by: erik
GPG key ID: 776702A6A2DA330E
6 changed files with 27 additions and 32 deletions

View file

@ -51,7 +51,7 @@ final AkismetComment comment = new AkismetComment(
.authorUrl("https://www.CheckOutMyCoolSite.com") .authorUrl("https://www.CheckOutMyCoolSite.com")
.dateGmt(Akismet.dateToGmt(new Date())) .dateGmt(Akismet.dateToGmt(new Date()))
.content("Thanks for reviewing our software.") .content("Thanks for reviewing our software.")
.build .build()
); );
//... //...

View file

@ -1,6 +1,6 @@
<?xml version="1.0" ?> <?xml version="1.0" ?>
<SmellBaseline> <SmellBaseline>
<ManuallySuppressedIssues></ManuallySuppressedIssues> <ManuallySuppressedIssues/>
<CurrentIssues> <CurrentIssues>
<ID>CyclomaticComplexMethod:Akismet.kt$Akismet$private fun buildFormBody(comment: AkismetComment): FormBody</ID> <ID>CyclomaticComplexMethod:Akismet.kt$Akismet$private fun buildFormBody(comment: AkismetComment): FormBody</ID>
<ID>CyclomaticComplexMethod:AkismetComment.kt$AkismetComment$@Suppress("DuplicatedCode") override fun equals(other: Any?): Boolean</ID> <ID>CyclomaticComplexMethod:AkismetComment.kt$AkismetComment$@Suppress("DuplicatedCode") override fun equals(other: Any?): Boolean</ID>
@ -9,9 +9,9 @@
<ID>MagicNumber:Akismet.kt$Akismet$8</ID> <ID>MagicNumber:Akismet.kt$Akismet$8</ID>
<ID>NestedBlockDepth:Akismet.kt$Akismet$@JvmOverloads fun executeMethod(apiUrl: HttpUrl, formBody: FormBody, trueOnError: Boolean = false): Boolean</ID> <ID>NestedBlockDepth:Akismet.kt$Akismet$@JvmOverloads fun executeMethod(apiUrl: HttpUrl, formBody: FormBody, trueOnError: Boolean = false): Boolean</ID>
<ID>NestedBlockDepth:AkismetExample.kt$fun main(args: Array&lt;String&gt;)</ID> <ID>NestedBlockDepth:AkismetExample.kt$fun main(args: Array&lt;String&gt;)</ID>
<ID>NestedBlockDepth:AkismetTest.kt$fun getKey(key: String): String</ID>
<ID>ReturnCount:Akismet.kt$Akismet$@JvmOverloads fun executeMethod(apiUrl: HttpUrl, formBody: FormBody, trueOnError: Boolean = false): Boolean</ID> <ID>ReturnCount:Akismet.kt$Akismet$@JvmOverloads fun executeMethod(apiUrl: HttpUrl, formBody: FormBody, trueOnError: Boolean = false): Boolean</ID>
<ID>ReturnCount:AkismetTest.kt$fun getKey(key: String): String</ID>
<ID>TooManyFunctions:CommentConfig.kt$CommentConfig$Builder</ID> <ID>TooManyFunctions:CommentConfig.kt$CommentConfig$Builder</ID>
<ID>WildcardImport:AkismetTest.kt$import assertk.assertions.*</ID> <ID>WildcardImport:AkismetTests.kt$import assertk.assertions.*</ID>
</CurrentIssues> </CurrentIssues>
</SmellBaseline> </SmellBaseline>

View file

@ -233,7 +233,7 @@ open class Akismet(apiKey: String) {
* *
* See the [Akismet API](https://akismet.com/development/api/#verify-key) for more details. * 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] * @see [Akismet.isVerifiedKey]
*/ */
fun verifyKey(): Boolean { 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. * See the [Akismet API](https://akismet.com/development/api/#comment-check) for more details.
* *
* @param trueOnError Set to return `true` on error. * @param trueOnError Set to return `true` on error
* @return `true` if the comment is spam, `false` if the comment is not. * @return `true` if the comment is spam, `false` if the comment is not
*/ */
@JvmOverloads @JvmOverloads
fun checkComment(comment: AkismetComment, trueOnError: Boolean = false): Boolean { 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. * 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 { fun submitSpam(comment: AkismetComment): Boolean {
return executeMethod("submit-spam".toApiUrl(), buildFormBody(comment)) 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. * 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 { fun submitHam(comment: AkismetComment): Boolean {
return executeMethod("submit-ham".toApiUrl(), buildFormBody(comment)) 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. * 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 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 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.) * @param trueOnError Set to return `true` on error (IO, empty response, etc.)
*/ */
@JvmOverloads @JvmOverloads

View file

@ -33,9 +33,7 @@ package net.thauvin.erik.akismet
import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletRequest
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlin.collections.set
private fun String?.ifNull() = this ?: "" 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. * See the [Akismet API](https://akismet.com/development/api/#comment-check) for more details.
* *
* @param userIp IP address of the comment submitter. * @param userIp IP address of the comment submitter
* @param userAgent User agent string of the web browser submitting the comment. * @param userAgent User agent string of the web browser submitting the comment
*/ */
@Serializable @Serializable
open class AkismetComment(val userIp: String, val userAgent: String) { open class AkismetComment(val userIp: String, val userAgent: String) {

View file

@ -56,8 +56,8 @@ class CommentConfig private constructor(builder: Builder) {
/** /**
* Provides a configuration builder. * Provides a configuration builder.
* *
* @param userIp IP address of the comment submitter. * @param userIp IP address of the comment submitter
* @param userAgent User agent string of the web browser submitting the comment. * @param userAgent User agent string of the web browser submitting the comment
*/ */
data class Builder(var userIp: String, var userAgent: String) { data class Builder(var userIp: String, var userAgent: String) {
var referrer = "" var referrer = ""

View file

@ -144,21 +144,8 @@ class AkismetTests {
} }
private fun getKey(key: String): String { private fun getKey(key: String): String {
var value = System.getenv(key) ?: "" return System.getenv(key)?.takeUnless { it.isBlank() }
if (value.isBlank()) { ?: loadPropertyValue(key)
val localProps = File("local.properties")
localProps.apply {
if (exists()) {
FileInputStream(this).use { fis ->
Properties().apply {
load(fis)
value = getProperty(key, "")
}
}
}
}
}
return value
} }
private fun getMockRequest(): HttpServletRequest { private fun getMockRequest(): HttpServletRequest {
@ -176,6 +163,16 @@ class AkismetTests {
} }
return request 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 @Nested