demarches-normaliennes/lib/tasks/api_geo_data.rake

50 lines
1.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2023-04-13 13:08:47 +02:00
namespace :api_geo_data do
PATH = Rails.root.join('lib', 'data', 'api_geo')
desc 'Refresh data from API Geo'
task refresh: :environment do
PATH.rmtree if PATH.exist?
PATH.mkpath
get_from_api_geo('regions', 'regions')
departements = get_from_api_geo('departements?zone=metro,drom,com', 'departements')
departements.each do |departement|
departement_code = departement[:code]
epci_filename = "epcis-#{departement_code}"
if departement_code.start_with?('98')
PATH.join("#{epci_filename}.json").write(JSON.dump([]))
else
get_from_api_geo("epcis?codeDepartement=#{departement_code}", epci_filename)
2023-04-13 13:08:47 +02:00
end
get_from_api_geo("communes?codeDepartement=#{departement_code}&type=commune-actuelle,arrondissement-municipal", "communes-#{departement_code}")
end
end
def get_from_api_geo(query, filename)
data = []
PATH.join("#{filename}.json").open('w') do |f|
response = Typhoeus.get("#{API_GEO_URL}/#{query}")
json = JSON.parse(response.body).map(&:symbolize_keys).flat_map do |result|
2023-04-13 13:08:47 +02:00
item = {
name: result[:nom].tr("'", ''),
code: result[:code],
epci_code: result[:codeEpci],
departement_code: result[:codeDepartement],
region_code: result[:codeRegion]
2023-04-13 13:08:47 +02:00
}.compact
if result[:codesPostaux].present?
result[:codesPostaux].map { item.merge(postal_code: _1) }
else
[item]
end
end
data = json
f << JSON.pretty_generate(json.sort_by { _1[:code] })
2023-04-13 13:08:47 +02:00
end
data
end
end