hps_member/lib/tasks/hps_task.rake

52 lines
1.8 KiB
Ruby
Raw Normal View History

2017-05-18 12:12:35 +00:00
namespace :hps_task do
task :import => :environment do
file = File.join(Rails.root, "public", "hps", "cities.json")
data = File.read(file)
cities = JSON.parse(data)
cities.each do |city|
hpscity = HpsCity.where(:old_id => city["old_id"]).first rescue nil
if hpscity.nil?
hpscity = HpsCity.new
end
hpscity.old_id = city["old_id"]
hpscity.name = city["name"]
hpscity.save
end
file = File.join(Rails.root, "public", "hps", "counties.json")
data = File.read(file)
counties = JSON.parse(data)
counties.each do |county|
hpscounty = HpsCounty.where(:old_id => county["old_id"]).first rescue nil
if hpscounty.nil?
hpscounty = HpsCounty.new
end
hpscounty.old_id = county["old_id"]
hpscounty.name = county["name"]
hpscounty.zip_code = county["zip_code"]
city = HpsCity.where(:old_id => county["cityID"]).first
hpscounty.hps_city = city
hpscounty.save
end
file = File.join(Rails.root, "public", "hps", "schools.json")
data = File.read(file)
schools = JSON.parse(data)
schools.each do |school|
hpsschool = HpsSchool.where(:old_id => school["old_id"]).first rescue nil
if hpsschool.nil?
hpsschool = HpsSchool.new
end
hpsschool.old_id = school["old_id"]
hpsschool.name = school["name"]
hpsschool.address = school["address"]
hpsschool.telephone = school["telephone"]
hpsschool.land = school["land"]
hpsschool.url = school["url"]
hpsschool.type = school["type"]
city = HpsCity.where(:old_id => school["City"]).first
county = HpsCounty.where(:old_id => school["CityZip"]).first
hpsschool.hps_city = city
hpsschool.hps_county = county
hpsschool.save
end
end
end