Created
December 8, 2020 04:14
-
-
Save chinying/ad1df40535a04f9e1450627a889d397c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
hashes = Hash(String, FlattenedCountry).new | |
class CountryName | |
JSON.mapping( | |
common: String, | |
official: String | |
) | |
end | |
class CountryIdd | |
JSON.mapping( | |
root: String, | |
suffixes: Array(String) | |
) | |
end | |
class Country | |
JSON.mapping( | |
name: CountryName, | |
idd: CountryIdd, | |
cca2: String | |
) | |
end | |
class FlattenedCountry | |
def initialize(@name : String, @calling_code : String, @iso_8166 : String) | |
end | |
JSON.mapping( | |
name: String, | |
calling_code: String, | |
iso_8166: String | |
) | |
end | |
contents = File.read("./countries.json") | |
countries = Array(Country).from_json(contents) | |
# puts(countries.to_json) | |
(countries.map { |c| | |
if c.idd.suffixes.size == 1 | |
FlattenedCountry.new(c.name.official, c.idd.root + c.idd.suffixes[0], c.cca2) | |
else | |
FlattenedCountry.new(c.name.official, c.idd.root, c.cca2) | |
end | |
}).each{|j| hashes[j.iso_8166] = j } | |
# .map{ |j| | |
# "'#{j.iso_8166}': \"#{j.to_json}\"" | |
# } | |
puts hashes.to_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment