From 78970f18e78818ec4d244145ad4302ce0689d4f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belin?= Date: Thu, 7 Nov 2024 20:25:07 +0100 Subject: [PATCH] Port the examples --- example/check_comment.coffee | 29 +++++++++++++++++++++++++++++ example/check_comment.js | 3 +-- example/submit_ham.coffee | 19 +++++++++++++++++++ example/submit_ham.js | 3 +-- example/submit_spam.coffee | 19 +++++++++++++++++++ example/submit_spam.js | 3 +-- example/verify_key.coffee | 13 +++++++++++++ example/verify_key.js | 3 +-- 8 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 example/check_comment.coffee create mode 100644 example/submit_ham.coffee create mode 100644 example/submit_spam.coffee create mode 100644 example/verify_key.coffee diff --git a/example/check_comment.coffee b/example/check_comment.coffee new file mode 100644 index 0000000..49c53d9 --- /dev/null +++ b/example/check_comment.coffee @@ -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: "john.doe@domain.com" + 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 diff --git a/example/check_comment.js b/example/check_comment.js index 6f69013..461dac9 100644 --- a/example/check_comment.js +++ b/example/check_comment.js @@ -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); } diff --git a/example/submit_ham.coffee b/example/submit_ham.coffee new file mode 100644 index 0000000..bcb2e8a --- /dev/null +++ b/example/submit_ham.coffee @@ -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 diff --git a/example/submit_ham.js b/example/submit_ham.js index b5ee6c1..991bbef 100644 --- a/example/submit_ham.js +++ b/example/submit_ham.js @@ -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); } diff --git a/example/submit_spam.coffee b/example/submit_spam.coffee new file mode 100644 index 0000000..252d9d1 --- /dev/null +++ b/example/submit_spam.coffee @@ -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 diff --git a/example/submit_spam.js b/example/submit_spam.js index ed1e8d0..1a82c0b 100644 --- a/example/submit_spam.js +++ b/example/submit_spam.js @@ -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); } diff --git a/example/verify_key.coffee b/example/verify_key.coffee new file mode 100644 index 0000000..a6e85b7 --- /dev/null +++ b/example/verify_key.coffee @@ -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 diff --git a/example/verify_key.js b/example/verify_key.js index 90cf004..2c0571c 100644 --- a/example/verify_key.js +++ b/example/verify_key.js @@ -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); }