Skip to content

Commit

Permalink
fix: sta warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
skwasjer committed Oct 9, 2024
1 parent ab0970b commit 413cc4a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
17 changes: 12 additions & 5 deletions src/IbanNet.CodeGen/Wikipedia/Loader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,32 @@ namespace IbanNet.CodeGen.Wikipedia;

public static class Loader
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };

public static WikiResult GetWikiData()
{
#pragma warning disable S1075
var uri = new Uri("https://en.wikipedia.org/w/api.php?format=json&action=parse&page=International_Bank_Account_Number&section=16", UriKind.Absolute);
#pragma warning restore S1075
HttpWebRequest req = WebRequest.CreateHttp(uri);
using WebResponse response = req.GetResponse();
using var ms = new MemoryStream();
response.GetResponseStream().CopyTo(ms);
response.GetResponseStream()!.CopyTo(ms);
byte[] buffer = ms.ToArray();

var jsonOpts = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
WikiResponse? wikiResponse = JsonSerializer.Deserialize<WikiResponse>(buffer, jsonOpts);
WikiResponse? wikiResponse = JsonSerializer.Deserialize<WikiResponse>(buffer, JsonSerializerOptions);
if (wikiResponse is null)
{
throw new InvalidOperationException("Unexpected response.");
}

var tableRegex = new Regex("(<table[^\\>]*?>.*<\\/table>)", RegexOptions.Singleline);
Match tableMatch = tableRegex.Match(wikiResponse.Parse.Text["*"]);

var doc = new HtmlDocument();
doc.LoadHtml($"<div>${tableMatch.Value}</div>");

IEnumerable<WikiRecord>? records = doc.DocumentNode.SelectNodes("//tr")
IEnumerable<WikiRecord> records = doc.DocumentNode.SelectNodes("//tr")
.GroupBy(e => e.ParentNode)
.Select(g => g.Where(e => e.Element("td") != null))
.Select(rows => rows.Select(r =>
Expand All @@ -38,7 +45,7 @@ public static WikiResult GetWikiData()
Pattern = cells[2].InnerText.Trim().Replace(" ", "")
};
}))
.SelectMany(_ => _);
.SelectMany(x => x);

return new WikiResult(records, wikiResponse.Parse);
}
Expand Down
8 changes: 4 additions & 4 deletions src/IbanNet.CodeGen/Wikipedia/ParseResult.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace IbanNet.CodeGen.Wikipedia;

public class ParseResult
public sealed record ParseResult
{
public int PageId { get; set; }
public int RevId { get; set; }
public Dictionary<string, string> Text { get; set; }
public int PageId { get; init; }
public int RevId { get; init; }
public Dictionary<string, string> Text { get; init; } = [];
}
4 changes: 2 additions & 2 deletions src/IbanNet.CodeGen/Wikipedia/WikiResponse.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace IbanNet.CodeGen.Wikipedia;

public class WikiResponse
public sealed record WikiResponse
{
public ParseResult Parse { get; set; }
public ParseResult Parse { get; init; } = default!;
}

0 comments on commit 413cc4a

Please sign in to comment.