Skip to content

Commit 783c8b0

Browse files
author
Cédric Belin
committed
Rename the ToJson() method to ToDictionary()
1 parent cec545b commit 783c8b0

File tree

7 files changed

+23
-23
lines changed

7 files changed

+23
-23
lines changed

src/author.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public sealed class Author(string ipAddress) {
3939
public string UserAgent { get; set; } = string.Empty;
4040

4141
/// <summary>
42-
/// Returns a JSON representation of this object.
42+
/// Converts this object into a dictionary.
4343
/// </summary>
44-
/// <returns>The JSON representation of this object.</returns>
45-
internal IDictionary<string, string> ToJson() {
44+
/// <returns>The dictionary corresponding to this object.</returns>
45+
internal IDictionary<string, string> ToDictionary() {
4646
var map = new Dictionary<string, string> { ["user_ip"] = IPAddress.ToString() };
4747
if (!string.IsNullOrWhiteSpace(Email)) map["comment_author_email"] = Email;
4848
if (!string.IsNullOrWhiteSpace(Name)) map["comment_author"] = Name;

src/blog.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public sealed class Blog(string url) {
2424
public Uri Url { get; set; } = new Uri(url);
2525

2626
/// <summary>
27-
/// Returns a JSON representation of this object.
27+
/// Converts this object into a dictionary.
2828
/// </summary>
29-
/// <returns>The JSON representation of this object.</returns>
30-
internal IDictionary<string, string> ToJson() {
29+
/// <returns>The dictionary corresponding to this object.</returns>
30+
internal IDictionary<string, string> ToDictionary() {
3131
var map = new Dictionary<string, string> { ["blog"] = Url.ToString() };
3232
if (Charset is not null) map["blog_charset"] = Charset.WebName;
3333
if (Languages.Count != 0) map["blog_lang"] = string.Join(',', Languages);

src/client.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class Client(string apiKey, Blog blog, string baseUrl = "https://rest.aki
5151
/// <returns>A value indicating whether the specified comment is spam.</returns>
5252
/// <exception cref="HttpRequestException">The remote server returned an invalid response.</exception>
5353
public async Task<CheckResult> CheckComment(Comment comment, CancellationToken cancellationToken = default) {
54-
using var response = await Fetch("1.1/comment-check", comment.ToJson(), cancellationToken);
54+
using var response = await Fetch("1.1/comment-check", comment.ToDictionary(), cancellationToken);
5555
if (await response.Content.ReadAsStringAsync(cancellationToken) == "false") return CheckResult.Ham;
5656
if (!response.Headers.TryGetValues("X-akismet-pro-tip", out var proTips)) return CheckResult.Spam;
5757
return proTips.First() == "discard" ? CheckResult.PervasiveSpam : CheckResult.Spam;
@@ -65,7 +65,7 @@ public async Task<CheckResult> CheckComment(Comment comment, CancellationToken c
6565
/// <returns>Completes once the comment has been submitted.</returns>
6666
/// <exception cref="HttpRequestException">The remote server returned an invalid response.</exception>
6767
public async Task SubmitHam(Comment comment, CancellationToken cancellationToken = default) {
68-
using var response = await Fetch("1.1/submit-ham", comment.ToJson(), cancellationToken);
68+
using var response = await Fetch("1.1/submit-ham", comment.ToDictionary(), cancellationToken);
6969
var body = await response.Content.ReadAsStringAsync(cancellationToken);
7070
if (body != Success) throw new HttpRequestException("Invalid server response.");
7171
}
@@ -78,7 +78,7 @@ public async Task SubmitHam(Comment comment, CancellationToken cancellationToken
7878
/// <returns>Completes once the comment has been submitted.</returns>
7979
/// <exception cref="HttpRequestException">The remote server returned an invalid response.</exception>
8080
public async Task SubmitSpam(Comment comment, CancellationToken cancellationToken = default) {
81-
using var response = await Fetch("1.1/submit-spam", comment.ToJson(), cancellationToken);
81+
using var response = await Fetch("1.1/submit-spam", comment.ToDictionary(), cancellationToken);
8282
var body = await response.Content.ReadAsStringAsync(cancellationToken);
8383
if (body != Success) throw new HttpRequestException("Invalid server response.");
8484
}
@@ -107,7 +107,7 @@ public async Task<bool> VerifyKey(CancellationToken cancellationToken = default)
107107
/// <returns>The server response.</returns>
108108
/// <exception cref="HttpRequestException">An error occurred while querying the end point.</exception>
109109
private async Task<HttpResponseMessage> Fetch(string endpoint, IDictionary<string, string>? fields = null, CancellationToken cancellationToken = default) {
110-
var postFields = Blog.ToJson();
110+
var postFields = Blog.ToDictionary();
111111
postFields.Add("api_key", ApiKey);
112112
if (IsTest) postFields.Add("is_test", "1");
113113
if (fields is not null) foreach (var item in fields) postFields.Add(item.Key, item.Value);

src/comment.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ public sealed class Comment(Author author) {
5252
public string Type { get; set; } = string.Empty;
5353

5454
/// <summary>
55-
/// Returns a JSON representation of this object.
55+
/// Converts this object into a dictionary.
5656
/// </summary>
57-
/// <returns>The JSON representation of this object.</returns>
58-
internal IDictionary<string, string> ToJson() {
59-
var map = Author.ToJson();
57+
/// <returns>The dictionary corresponding to this object.</returns>
58+
internal IDictionary<string, string> ToDictionary() {
59+
var map = Author.ToDictionary();
6060
if (!string.IsNullOrWhiteSpace(Content)) map["comment_content"] = Content;
6161
// TODO if (Context.Count > 0) map["comment_context"] = string.Join(',', Context);
6262
if (Date is not null) map["comment_date_gmt"] = Date?.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")!;

test/author_test.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace Belin.Akismet;
77
public sealed class AuthorTest {
88

99
[TestMethod]
10-
public void ToJson() {
10+
public void ToDictionary() {
1111
// It should return only the IP address with a newly created instance.
12-
var map = new Author(ipAddress: "127.0.0.1").ToJson();
12+
var map = new Author(ipAddress: "127.0.0.1").ToDictionary();
1313
AreEqual(1, map.Count);
1414
AreEqual("127.0.0.1", map["user_ip"]);
1515

@@ -21,7 +21,7 @@ public void ToJson() {
2121
UserAgent = "Mozilla/5.0"
2222
};
2323

24-
map = author.ToJson();
24+
map = author.ToDictionary();
2525
AreEqual(5, map.Count);
2626
AreEqual("Cédric Belin", map["comment_author"]);
2727
AreEqual("cedric@belin.io", map["comment_author_email"]);

test/blog_test.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ namespace Belin.Akismet;
99
public sealed class BlogTest {
1010

1111
[TestMethod]
12-
public void ToJson() {
12+
public void ToDictionary() {
1313
// It should return only the blog URL with a newly created instance.
14-
var map = new Blog(url: "https://github.com/cedx/akismet.cs").ToJson();
14+
var map = new Blog(url: "https://github.com/cedx/akismet.cs").ToDictionary();
1515
AreEqual(1, map.Count);
1616
AreEqual("https://github.com/cedx/akismet.cs", map["blog"]);
1717

1818
// It should return a non-empty map with an initialized instance.
19-
map = new Blog(url: "https://github.com/cedx/akismet.cs") { Charset = Encoding.UTF8, Languages = ["en", "fr"] }.ToJson();
19+
map = new Blog(url: "https://github.com/cedx/akismet.cs") { Charset = Encoding.UTF8, Languages = ["en", "fr"] }.ToDictionary();
2020
AreEqual(3, map.Count);
2121
AreEqual("https://github.com/cedx/akismet.cs", map["blog"]);
2222
AreEqual("utf-8", map["blog_charset"]);

test/comment_test.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace Belin.Akismet;
77
public sealed class CommentTest {
88

99
[TestMethod]
10-
public void ToJson() {
10+
public void ToDictionary() {
1111
// It should return only the author info with a newly created instance.
12-
var map = new Comment(new Author(ipAddress: "127.0.0.1")).ToJson();
12+
var map = new Comment(new Author(ipAddress: "127.0.0.1")).ToDictionary();
1313
AreEqual(1, map.Count);
1414
AreEqual("127.0.0.1", map["user_ip"]);
1515

@@ -26,7 +26,7 @@ public void ToJson() {
2626
Type = "blog-post"
2727
};
2828

29-
map = comment.ToJson();
29+
map = comment.ToDictionary();
3030
AreEqual(7, map.Count);
3131
AreEqual("Cédric Belin", map["comment_author"]);
3232
AreEqual("A user comment.", map["comment_content"]);

0 commit comments

Comments
 (0)