Moved static functions to companion object.
This commit is contained in:
parent
1815093f5f
commit
8702c6511d
4 changed files with 48 additions and 38 deletions
|
@ -3,6 +3,8 @@ package com.example;
|
||||||
import net.thauvin.erik.akismet.Akismet;
|
import net.thauvin.erik.akismet.Akismet;
|
||||||
import net.thauvin.erik.akismet.AkismetComment;
|
import net.thauvin.erik.akismet.AkismetComment;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
public class AkismetSample {
|
public class AkismetSample {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
final Akismet akismet = new Akismet("YOUR_API_KEY", "YOUR_BLOG_URL");
|
final Akismet akismet = new Akismet("YOUR_API_KEY", "YOUR_BLOG_URL");
|
||||||
|
@ -17,6 +19,7 @@ public class AkismetSample {
|
||||||
comment.setAuthor("admin");
|
comment.setAuthor("admin");
|
||||||
comment.setAuthorEmail("test@test.com");
|
comment.setAuthorEmail("test@test.com");
|
||||||
comment.setAuthorUrl("http://www.CheckOutMyCoolSite.com");
|
comment.setAuthorUrl("http://www.CheckOutMyCoolSite.com");
|
||||||
|
comment.setDateGmt(Akismet.dateToGmt(new Date()));
|
||||||
// comment.setUserRole(AkismetComment.ADMIN_ROLE);
|
// comment.setUserRole(AkismetComment.ADMIN_ROLE);
|
||||||
comment.setContent("It means a lot that you would take the time to review our software. Thanks again.");
|
comment.setContent("It means a lot that you would take the time to review our software. Thanks again.");
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.example
|
||||||
|
|
||||||
import net.thauvin.erik.akismet.Akismet
|
import net.thauvin.erik.akismet.Akismet
|
||||||
import net.thauvin.erik.akismet.AkismetComment
|
import net.thauvin.erik.akismet.AkismetComment
|
||||||
|
import java.util.Date
|
||||||
import kotlin.system.exitProcess
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
|
@ -19,6 +20,7 @@ fun main() {
|
||||||
comment.author = "admin"
|
comment.author = "admin"
|
||||||
comment.authorEmail = "test@test.com"
|
comment.authorEmail = "test@test.com"
|
||||||
comment.authorUrl = "http://www.CheckOutMyCoolSite.com"
|
comment.authorUrl = "http://www.CheckOutMyCoolSite.com"
|
||||||
|
comment.dateGmt = Akismet.dateToGmt(Date())
|
||||||
// comment.userRole = AkismetComment.ADMIN_ROLE
|
// comment.userRole = AkismetComment.ADMIN_ROLE
|
||||||
comment.content = "It means a lot that you would take the time to review our software. Thanks again."
|
comment.content = "It means a lot that you would take the time to review our software. Thanks again."
|
||||||
|
|
||||||
|
@ -40,7 +42,7 @@ fun main() {
|
||||||
System.err.println(akismet.errorMessage)
|
System.err.println(akismet.errorMessage)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println("The comment is not SPAM (HAM) according to Akismet.")
|
println("The comment is not SPAM according to Akismet.")
|
||||||
|
|
||||||
val hasBeenSubmitted = akismet.submitHam(comment)
|
val hasBeenSubmitted = akismet.submitHam(comment)
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,44 @@ import java.util.logging.Logger
|
||||||
*/
|
*/
|
||||||
@Version(properties = "version.properties", type = "kt")
|
@Version(properties = "version.properties", type = "kt")
|
||||||
open class Akismet(apiKey: String) {
|
open class Akismet(apiKey: String) {
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* (Re)Create a [comment][AkismetComment] from a JSON string.
|
||||||
|
*
|
||||||
|
* @see [AkismetComment.toString]
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun jsonComment(json: String): AkismetComment {
|
||||||
|
return Json(JsonConfiguration.Stable).parse(AkismetComment.serializer(), json)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a date to a UTC timestamp. (ISO 8601)
|
||||||
|
*
|
||||||
|
* @see [AkismetComment.dateGmt]
|
||||||
|
* @see [AkismetComment.postModifiedGmt]
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun dateToGmt(date: Date): String {
|
||||||
|
return DateTimeFormatter.ISO_DATE_TIME.format(
|
||||||
|
OffsetDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).truncatedTo(ChronoUnit.SECONDS)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a locale date/time to a UTC timestamp. (ISO 8601)
|
||||||
|
*
|
||||||
|
* @see [AkismetComment.dateGmt]
|
||||||
|
* @see [AkismetComment.postModifiedGmt]
|
||||||
|
*/
|
||||||
|
@JvmStatic
|
||||||
|
fun dateToGmt(date: LocalDateTime): String {
|
||||||
|
return DateTimeFormatter.ISO_DATE_TIME.format(
|
||||||
|
date.atOffset(OffsetDateTime.now().offset).truncatedTo(ChronoUnit.SECONDS)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private val apiEndPoint = "https://%srest.akismet.com/1.1/%s"
|
private val apiEndPoint = "https://%srest.akismet.com/1.1/%s"
|
||||||
private val libUserAgent = "${GeneratedVersion.PROJECT}/${GeneratedVersion.VERSION}"
|
private val libUserAgent = "${GeneratedVersion.PROJECT}/${GeneratedVersion.VERSION}"
|
||||||
private val verifyMethod = "verify-key"
|
private val verifyMethod = "verify-key"
|
||||||
|
@ -280,39 +318,6 @@ open class Akismet(apiKey: String) {
|
||||||
return executeMethod(buildApiUrl("submit-ham"), buildFormBody(comment))
|
return executeMethod(buildApiUrl("submit-ham"), buildFormBody(comment))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* (Re)Create a [comment][AkismetComment] from a JSON string.
|
|
||||||
*
|
|
||||||
* @see [AkismetComment.toString]
|
|
||||||
*/
|
|
||||||
fun jsonComment(json: String): AkismetComment {
|
|
||||||
return Json(JsonConfiguration.Stable).parse(AkismetComment.serializer(), json)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a date to a UTC timestamp. (ISO 8601)
|
|
||||||
*
|
|
||||||
* @see [AkismetComment.dateGmt]
|
|
||||||
* @see [AkismetComment.postModifiedGmt]
|
|
||||||
*/
|
|
||||||
fun dateToGmt(date: Date): String {
|
|
||||||
return DateTimeFormatter.ISO_DATE_TIME.format(
|
|
||||||
OffsetDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).truncatedTo(ChronoUnit.SECONDS)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a locale date/time to a UTC timestamp. (ISO 8601)
|
|
||||||
*
|
|
||||||
* @see [AkismetComment.dateGmt]
|
|
||||||
* @see [AkismetComment.postModifiedGmt]
|
|
||||||
*/
|
|
||||||
fun dateToGmt(date: LocalDateTime): String {
|
|
||||||
return DateTimeFormatter.ISO_DATE_TIME.format(
|
|
||||||
date.atOffset(OffsetDateTime.now().offset).truncatedTo(ChronoUnit.SECONDS)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute a call to an Akismet REST API method.
|
* Execute a call to an Akismet REST API method.
|
||||||
*
|
*
|
||||||
|
|
|
@ -102,7 +102,7 @@ class AkismetTest {
|
||||||
comment.authorEmail = "test@test.com"
|
comment.authorEmail = "test@test.com"
|
||||||
comment.authorUrl = "http://www.CheckOutMyCoolSite.com"
|
comment.authorUrl = "http://www.CheckOutMyCoolSite.com"
|
||||||
comment.content = "It means a lot that you would take the time to review our software. Thanks again."
|
comment.content = "It means a lot that you would take the time to review our software. Thanks again."
|
||||||
comment.dateGmt = akismet.dateToGmt(date)
|
comment.dateGmt = Akismet.dateToGmt(date)
|
||||||
comment.postModifiedGmt = comment.dateGmt
|
comment.postModifiedGmt = comment.dateGmt
|
||||||
comment.blogLang = "en"
|
comment.blogLang = "en"
|
||||||
comment.blogCharset = "UTF-8"
|
comment.blogCharset = "UTF-8"
|
||||||
|
@ -279,7 +279,7 @@ class AkismetTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testJsonComment() {
|
fun testJsonComment() {
|
||||||
val jsonComment = akismet.jsonComment(mockComment.toString())
|
val jsonComment = Akismet.jsonComment(mockComment.toString())
|
||||||
|
|
||||||
assertEquals(jsonComment, mockComment, "equals")
|
assertEquals(jsonComment, mockComment, "equals")
|
||||||
assertEquals(jsonComment.hashCode(), mockComment.hashCode(), "hashcode")
|
assertEquals(jsonComment.hashCode(), mockComment.hashCode(), "hashcode")
|
||||||
|
@ -305,8 +305,8 @@ class AkismetTest {
|
||||||
@Test
|
@Test
|
||||||
fun dateToGmtTest() {
|
fun dateToGmtTest() {
|
||||||
val localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault())
|
val localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault())
|
||||||
val utcDate = akismet.dateToGmt(date)
|
val utcDate = Akismet.dateToGmt(date)
|
||||||
assertEquals(akismet.dateToGmt(localDateTime), utcDate, "dateGmt(localDateTime) = utcDate")
|
assertEquals(Akismet.dateToGmt(localDateTime), utcDate, "dateGmt(localDateTime) = utcDate")
|
||||||
assertEquals(comment.dateGmt, utcDate, "dateGmt == utcDate")
|
assertEquals(comment.dateGmt, utcDate, "dateGmt == utcDate")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue