-
Notifications
You must be signed in to change notification settings - Fork 1
Add new version of countries data. #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
83a02fa
641ab49
8384341
c511090
5c019d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Versioning | ||
|
|
||
| ## Context and problem statement | ||
|
|
||
| Teams using the reference data datasets need to be able to rely on a specific version of the datasets. Up to now, | ||
| there were situations where the datasets were updated without notice and it was leading to the applications displaying | ||
| data that might not be expected. | ||
|
|
||
| ## Solution | ||
|
|
||
| Solution is to add a version namespace (V2, V3, etc.). Version `V1` is omitted. | ||
| For example, updates to the countries list were introduced in version `V2`: `DfE::ReferenceData::V2::CountriesAndTerritories`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| require 'csv' | ||
|
mysteryGarlicPress marked this conversation as resolved.
|
||
|
|
||
| module DfE | ||
| module ReferenceData | ||
| module Helpers | ||
| module V2 | ||
| class CountryListCompiler | ||
| attr_reader :all_countries_data, :legacy_codes, :fcdo_codes, :fcdo_codes_index, :legacy_codes_index | ||
|
|
||
| LegacyCountry = Struct.new(:code, :name) | ||
| FCDOCountry = Struct.new(:code, :name, :official_name, :citizen_names) | ||
| # current FCDO list | ||
| FCDO_COUNTRIES = CSV.read('raw_data/FCDO_Geographical_Names_Index_September_2025.csv', headers: false).map do |country| | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason we're loading the CSVs when the class is required rather than when the helper is actually invoked? For production code this is probably fine because it's a utility class, but loading files during require can make testing and reuse a little harder. |
||
| FCDOCountry.new(*country) | ||
| end | ||
| LEGACY_COUNTRIES_AND_TERRITORIES = CSV.read('raw_data/legacy_countries.csv').map do |country| | ||
| LegacyCountry.new(*country) | ||
| end | ||
|
|
||
| def initialize | ||
| @legacy_codes = LEGACY_COUNTRIES_AND_TERRITORIES.map(&:code) | ||
| @fcdo_codes = FCDO_COUNTRIES.map(&:code) | ||
| @domicile_codes = @legacy_codes - @fcdo_codes | ||
| build_all_countries_data | ||
|
|
||
| missing_codes = @fcdo_codes - @legacy_codes | ||
|
|
||
| raise "Missing legacy codes: #{missing_codes.join(', ')}" unless missing_codes.empty? | ||
| end | ||
|
|
||
| # columns: | ||
| # code, name, full_name, citizen_name, FCDO_list, Legacy list | ||
| def create_full_csv | ||
| build_all_countries_data | ||
| CSV.open('raw_data/full_countries_and_territories.csv', 'wb') do |csv| | ||
| csv << ['code', 'name', 'full_name', 'citizen_name', 'FCDO_list', 'Legacy_list', 'is_domicile'] | ||
| @all_countries_data.each do |country| | ||
| csv << [country[:code], country[:name], country[:full_name], country[:citizen_name], country[:fcdo_list_name], country[:legacy_list_name], @domicile_codes.include?(country[:code])] | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def generate_countries_hash | ||
| @fcdo_codes.each do |code| | ||
| country = @fcdo_codes_index[code] | ||
| puts "\"#{code}\" => { name: \"#{country.name}\", official_name: \"#{country.official_name}\", citizen_names: \"#{country.citizen_names}\" }," | ||
| end | ||
| end | ||
|
|
||
| def generate_territories_hash | ||
| @domicile_codes.each do |code| | ||
| country = @legacy_codes_index[code] | ||
| puts "\"#{code}\" => { name: \"#{country.name}\" }," | ||
| end | ||
| end | ||
|
|
||
| def build_all_countries_data | ||
| compile_all_countries_symbols | ||
| build_indexes | ||
| @all_countries_data = [] | ||
| @all_codes.each do |code| | ||
| # code, name, full_name, citizen_name, FCDO_list, Legacy list | ||
| @all_countries_data << { | ||
| code: code, | ||
| name: @legacy_codes_index[code]&.name, | ||
| full_name: @fcdo_codes_index[code]&.official_name, | ||
| citizen_name: @fcdo_codes_index[code]&.citizen_names, | ||
| fcdo_list_name: @fcdo_codes_index[code]&.name, | ||
| legacy_list_name: @legacy_codes_index[code]&.name | ||
| } | ||
| end | ||
| end | ||
|
|
||
| def compile_all_countries_symbols | ||
| @all_codes = (@legacy_codes + @fcdo_codes).uniq | ||
| end | ||
|
|
||
| def build_indexes | ||
| @fcdo_codes_index = {} | ||
| @fcdo_codes.each { |code| @fcdo_codes_index[code] = FCDO_COUNTRIES.find { |country| country.code == code } } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small readability/performance suggestion: could these indexes be built directly from the collections rather than repeatedly calling find? For example: @fcdo_codes_index = FCDO_COUNTRIES.index_by(&:code) |
||
|
|
||
| @legacy_codes_index = {} | ||
| @legacy_codes.each { |code| @legacy_codes_index[code] = LEGACY_COUNTRIES_AND_TERRITORIES.find { |country| country.code == code } } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above: @legacy_codes_index = LEGACY_COUNTRIES_AND_TERRITORIES.index_by(&:code) |
||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.