< 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
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 65
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ExtractTitle(...)100%88100%
ExtractPk(...)100%44100%

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        {
 521            var root = json.RootElement;
 22
 23            // Try to get fields.name
 524            if (root.TryGetProperty("fields", out var fields) &&
 525                fields.TryGetProperty("name", out var nameElement) &&
 526                nameElement.ValueKind == JsonValueKind.String)
 27            {
 228                var name = nameElement.GetString();
 229                if (!string.IsNullOrWhiteSpace(name))
 30                {
 131                    return name;
 32                }
 33            }
 334        }
 135        catch
 36        {
 37            // Parsing failed, use fallback
 138        }
 39
 40        // Fallback: prettify slug
 441        return BlobFilenameParser.PrettifySlug(fallbackSlug);
 142    }
 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        {
 451            var root = json.RootElement;
 452            if (root.TryGetProperty("pk", out var pkElement) &&
 453                pkElement.ValueKind == JsonValueKind.String)
 54            {
 155                return pkElement.GetString();
 56            }
 257        }
 158        catch
 59        {
 60            // Ignore parsing errors
 161        }
 62
 363        return null;
 164    }
 65}