< Summary

Information
Class: Chronicis.Api.Services.ExternalLinks.SrdJsonHelper
Assembly: Chronicis.Api
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Api/Services/ExternalLinks/SrdJsonHelper.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 21
Coverable lines: 21
Total lines: 65
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ExtractTitle(...)0%7280%
ExtractPk(...)0%2040%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Api/Services/ExternalLinks/SrdJsonHelper.cs

#LineLine coverage
 1using System.Text.Json;
 2
 3namespace Chronicis.Api.Services.ExternalLinks;
 4
 5/// <summary>
 6/// Minimal helper for extracting guaranteed fields from SRD JSON blobs.
 7/// Only extracts fields guaranteed by the blob schema: pk and fields.name.
 8/// </summary>
 9internal static class SrdJsonHelper
 10{
 11    /// <summary>
 12    /// Extracts the title from fields.name, with fallback to prettified slug.
 13    /// </summary>
 14    /// <param name="json">Parsed JSON document.</param>
 15    /// <param name="fallbackSlug">Slug to prettify if fields.name is missing.</param>
 16    /// <returns>Title string.</returns>
 17    public static string ExtractTitle(JsonDocument json, string fallbackSlug)
 18    {
 19        try
 20        {
 021            var root = json.RootElement;
 22
 23            // Try to get fields.name
 024            if (root.TryGetProperty("fields", out var fields) &&
 025                fields.TryGetProperty("name", out var nameElement) &&
 026                nameElement.ValueKind == JsonValueKind.String)
 27            {
 028                var name = nameElement.GetString();
 029                if (!string.IsNullOrWhiteSpace(name))
 30                {
 031                    return name;
 32                }
 33            }
 034        }
 035        catch
 36        {
 37            // Parsing failed, use fallback
 038        }
 39
 40        // Fallback: prettify slug
 041        return BlobFilenameParser.PrettifySlug(fallbackSlug);
 042    }
 43
 44    /// <summary>
 45    /// Extracts the optional pk field for debugging purposes.
 46    /// </summary>
 47    public static string? ExtractPk(JsonDocument json)
 48    {
 49        try
 50        {
 051            var root = json.RootElement;
 052            if (root.TryGetProperty("pk", out var pkElement) &&
 053                pkElement.ValueKind == JsonValueKind.String)
 54            {
 055                return pkElement.GetString();
 56            }
 057        }
 058        catch
 59        {
 60            // Ignore parsing errors
 061        }
 62
 063        return null;
 064    }
 65}