Skip to content

Commit

Permalink
Port the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Nov 7, 2024
1 parent cd68f55 commit 78970f1
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 8 deletions.
29 changes: 29 additions & 0 deletions example/check_comment.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Author, Blog, CheckResult, Client, Comment, CommentType} from "@cedx/akismet"
import console from "node:console"

# Checks a comment against the Akismet service.
try
author = new Author
email: "[email protected]"
ipAddress: "192.168.123.456"
name: "John Doe"
role: "guest"
userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"

comment = new Comment
author: author
date: new Date
content: "A user comment."
referrer: "https://github.com/cedx/akismet.js"
type: CommentType.contactForm

blog = new Blog
charset: "UTF-8"
languages: ["fr"]
url: "https://www.yourblog.com"

result = await new Client("123YourAPIKey", blog).checkComment comment
console.log(if result is CheckResult.ham then "The comment is ham." else "The comment is spam.")

catch error
console.error if error instanceof Error then error.message else error
3 changes: 1 addition & 2 deletions example/check_comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ try {
console.log(result == CheckResult.ham ? "The comment is ham." : "The comment is spam.");
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.log(`An error occurred: ${message}`);
console.log(error instanceof Error ? error.message : error);
}
19 changes: 19 additions & 0 deletions example/submit_ham.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Author, Blog, Client, Comment} from "@cedx/akismet"
import console from "node:console"

# Submits ham to the Akismet service.
try
blog = new Blog url: "https://www.yourblog.com"
client = new Client "123YourAPIKey", blog

comment = new Comment
content: "I'm testing out the Service API."
author: new Author
ipAddress: "192.168.123.456"
userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"

await client.submitHam comment
console.log "The comment was successfully submitted as ham."

catch error
console.error if error instanceof Error then error.message else error
3 changes: 1 addition & 2 deletions example/submit_ham.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ try {
console.log("The comment was successfully submitted as ham.");
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.log(`An error occurred: ${message}`);
console.log(error instanceof Error ? error.message : error);
}
19 changes: 19 additions & 0 deletions example/submit_spam.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Author, Blog, Client, Comment} from "@cedx/akismet"
import console from "node:console"

# Submits spam to the Akismet service.
try
blog = new Blog url: "https://www.yourblog.com"
client = new Client "123YourAPIKey", blog

comment = new Comment
content: "Spam!"
author: new Author
ipAddress: "192.168.123.456"
userAgent: "Spam Bot/6.6.6"

await client.submitSpam comment
console.log "The comment was successfully submitted as spam."

catch error
console.error if error instanceof Error then error.message else error
3 changes: 1 addition & 2 deletions example/submit_spam.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ try {
console.log("The comment was successfully submitted as spam.");
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.log(`An error occurred: ${message}`);
console.log(error instanceof Error ? error.message : error);
}
13 changes: 13 additions & 0 deletions example/verify_key.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Blog, Client} from "@cedx/akismet"
import console from "node:console"

# Verifies an Akismet API key.
try
blog = new Blog url: "https://www.yourblog.com"
client = new Client "123YourAPIKey", blog

isValid = await client.verifyKey()
console.log(if isValid then "The API key is valid." else "The API key is invalid.")

catch error
console.error if error instanceof Error then error.message else error
3 changes: 1 addition & 2 deletions example/verify_key.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ try {
console.log(isValid ? "The API key is valid." : "The API key is invalid.");
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.log(`An error occurred: ${message}`);
console.log(error instanceof Error ? error.message : error);
}

0 comments on commit 78970f1

Please sign in to comment.