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 task :import_school_users => :environment do file = File.join(Rails.root, "public", "hps", "school_user.json") data = File.read(file) users = JSON.parse(data) users.each do |user| hpsm = HpsMember.where(:old_id => user["old_id"]).first rescue nil if hpsm.nil? hpsm = HpsMember.new end puts "Createing acccount #{user["account"]}" hpsm.old_id = user["old_id"] hpsm.account = user["account"] hpsm.password = user["password"] hpsm.name = user["name"] hpsm.telephone = user["telephone"] hpsm.mobile = user["mobile"] hpsm.email = user["email"] hpsm.address = user["address"] hpsm.title = user["title"] school = HpsSchool.where(:old_id => user["SchoolID"]).first hpsm.hps_school_id = school.id.to_s if !school.nil? city = HpsCity.where(:old_id => user["my_county"]).first hpsm.hps_city_id = city.id.to_s if !city.nil? county = HpsCounty.where(:old_id => user["cityzip"]).first hpsm.hps_county_id = county.id.to_s if !county.nil? hpsm.save end end end