Skip to content

Instantly share code, notes, and snippets.

@chinying
Created December 8, 2020 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chinying/ad1df40535a04f9e1450627a889d397c to your computer and use it in GitHub Desktop.
Save chinying/ad1df40535a04f9e1450627a889d397c to your computer and use it in GitHub Desktop.
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