demarches-normaliennes/lib/tasks/api_geo_data.rake

44 lines
1.4 KiB
Ruby
Raw Normal View History

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]
if !departement_code.start_with?('98')
get_from_api_geo("epcis?codeDepartement=#{departement_code}", "epcis-#{departement_code}")
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|
item = {
name: result[:nom].tr("'", ''),
code: result[:code],
epci_code: result[:codeEpci],
departement_code: result[:codeDepartement]
}.compact
if result[:codesPostaux].present?
result[:codesPostaux].map { item.merge(postal_code: _1) }
else
[item]
end
end
data = json
f << JSON.dump(json)
end
data
end
end