From ca8a99c9d1200cc3f9c5992c08c2fdd579dc278d Mon Sep 17 00:00:00 2001 From: syeopite Date: Sun, 27 Jun 2021 11:48:28 -0700 Subject: [PATCH] Unnest country data from instance API --- src/fetch.cr | 10 +++++----- src/instances.cr | 7 +++---- src/instances/views/index.ecr | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/fetch.cr b/src/fetch.cr index b482166..63fba64 100644 --- a/src/fetch.cr +++ b/src/fetch.cr @@ -3,7 +3,7 @@ def fetch_country(md) flag = md["flag"]? country_name = md["country_name"]? - return {flag: flag, region: region, name: country_name} + return flag, region, country_name end def fetch_notes(md) @@ -19,7 +19,7 @@ def prepare_http_instance(md, instances, monitors) uri = URI.parse(md["uri"]) host = md["host"] - country = fetch_country(md) + flag, region, country_name = fetch_country(md) status_url = md["status_url"]? @@ -44,7 +44,7 @@ def prepare_http_instance(md, instances, monitors) end monitor = monitors.try &.select { |monitor| monitor["name"].try &.as_s == host }[0]? - return {country: country, stats: stats, type: "https", uri: uri.to_s, status_url: status_url, + return {flag: flag, region: region, country_name: country_name, stats: stats, type: "https", uri: uri.to_s, status_url: status_url, privacy_policy: privacy_policy, ddos_protection: ddos_protection, owner: owner, notes: notes, monitor: monitor || instances[host]?.try &.[:monitor]?} end @@ -54,7 +54,7 @@ def prepare_onion_instance(md, instances) host = md["host"] clearnet_url = md["clearnet_url"] - country = fetch_country(md) + flag, region, country_name = fetch_country(md) privacy_policy = md["privacy_policy"]? owner = {name: md["owner"].strip("@"), url: md["owner_url"]} notes = fetch_notes(md) @@ -76,7 +76,7 @@ def prepare_onion_instance(md, instances) stats = nil end - return {country: country, stats: stats, type: "onion", uri: uri.to_s, clearnet_url: clearnet_url, + return {flag: flag, region: region, country_name: country_name, stats: stats, type: "onion", uri: uri.to_s, clearnet_url: clearnet_url, privacy_policy: privacy_policy, owner: owner, notes: notes, monitor: nil} end diff --git a/src/instances.cr b/src/instances.cr index fc7dcfd..e461072 100644 --- a/src/instances.cr +++ b/src/instances.cr @@ -30,9 +30,8 @@ macro rendered(filename) end alias Owner = NamedTuple(name: String, url: String) -alias Country = NamedTuple(flag: String?, region: String?, name: String?) -alias ClearNetInstance = NamedTuple(country: Country, stats: JSON::Any?, type: String, uri: String, status_url: String?, privacy_policy: String?, ddos_protection: String?, owner: Owner, notes: String?, monitor: JSON::Any?) -alias OnionInstance = NamedTuple(country: Country, stats: JSON::Any?, type: String, uri: String, clearnet_url: String?, privacy_policy: String?, owner: Owner, notes: String?, monitor: JSON::Any?) +alias ClearNetInstance = NamedTuple(flag: String?, region: String?, country_name: String?, stats: JSON::Any?, type: String, uri: String, status_url: String?, privacy_policy: String?, ddos_protection: String?, owner: Owner, notes: String?, monitor: JSON::Any?) +alias OnionInstance = NamedTuple(flag: String?, region: String?, country_name: String?, stats: JSON::Any?, type: String, uri: String, clearnet_url: String?, privacy_policy: String?, owner: Owner, notes: String?, monitor: JSON::Any?) INSTANCES = {} of String => ClearNetInstance | OnionInstance @@ -119,7 +118,7 @@ end SORT_PROCS = { "health" => ->(name : String, instance : ClearNetInstance | OnionInstance) { -(instance[:monitor]?.try &.["30dRatio"]["ratio"].as_s.to_f || 0.0) }, - "location" => ->(name : String, instance : ClearNetInstance | OnionInstance) { instance[:country][:region]? || "ZZ" }, + "location" => ->(name : String, instance : ClearNetInstance | OnionInstance) { instance[:region]? || "ZZ" }, "name" => ->(name : String, instance : ClearNetInstance | OnionInstance) { name }, "signup" => ->(name : String, instance : ClearNetInstance | OnionInstance) { instance[:stats]?.try &.["openRegistrations"]?.try { |bool| bool.as_bool ? 0 : 1 } || 2 }, "type" => ->(name : String, instance : ClearNetInstance | OnionInstance) { instance[:type] }, diff --git a/src/instances/views/index.ecr b/src/instances/views/index.ecr index c087488..2ba3207 100644 --- a/src/instances/views/index.ecr +++ b/src/instances/views/index.ecr @@ -118,7 +118,7 @@ <%= instance[:type] %> <%= instance[:stats]?.try &.["usage"]?.try &.["users"]["total"] || "-" %> <%= instance[:stats]?.try &.["openRegistrations"]?.try { |bool| bool.as_bool ? "✔" : "❌" } || "-" %> - <%= instance[:country]? ? "#{instance[:country][:flag]} #{instance[:country][:name]}" : "-" %> + <%= instance[:flag]? ? "#{instance[:flag]} #{instance[:region]}" : "-" %> <%= instance[:monitor]?.try &.["30dRatio"]["ratio"] || "-" %> <% end %>