namespace :import do desc "Check doc/import for resources to import against existing database" task check: :environment do begin features = JSON.parse(IO.read(File.join(Rails.root, 'doc/import/Locations.geojson')), { symbolize_names: false })['features'] rescue Exception => e p e $stderr.puts "Missing Locations.geojson. Did you run the parser.rb first?" exit(1) end # Make a first pass to see if we have existing records existing_records = {} i = 0 # temp known_records = new_records = 0 features.each do |feature| i+= 1 name = feature['name'] phone_number = feature['phone_number'] email = feature['email'] website = feature['website'] unless name.blank? r0 = Resource.where("feature #>> '{properties,name}' = :name", name: name) $stderr.puts "No existing resource with name '%s'" % name if r0.blank? end unless phone_number.blank? r1 = Resource.where("feature #>> '{properties,phone_number}' = :num", num: PhonyRails.normalize_number(phone_number, default_country_code: 'BE')) $stderr.puts "No existing resource with phone '%s'" % phone_number if r1.blank? end unless email.blank? r2 = Resource.where("feature #>> '{properties,email}' = :email", email: email) $stderr.puts "No existing resource with email = '%s'" % email if r2.blank? end unless website.blank? r3 = Resource.where("feature #>> '{properties,website}' = :url", url: website) $stderr.puts "No existing resource with website = '%s" % website if r3.blank? end unless r0.blank? && r1.blank? && r2.blank? && r3.blank? $stderr.print '!' known_records += 1 =begin $stderr.puts "Some potential resources found..." p r0 p r1 p r2 p r3 =end else new_records += 1 $stderr.print "." end # Normalize fields # stop at 3 #i >= 3 && exit(0) end $stderr.puts "Task complete (known: #{known_records}, new: #{new_records}, total: #{features.count})" end desc "Import new resources" task import: :environment do dewey = Agent.find_by(name: 'Dewey') concertes = Agent.find_by(name: 'ConcertES') $stderr.puts "Resources: #{Resource.count}" begin features = JSON.parse(IO.read(File.join(Rails.root, 'doc/import/Locations.geojson')), { symbolize_names: false }) rescue Exception => e p e $stderr.puts "Missing Locations.geojson. Did you run the parser.rb first?" exit(1) end $stderr.puts "Features: #{features['features'].count}" features['features'].each do |feat| ag = feat['properties']['source'] == 'ConcertES' ? concertes : dewey $stderr.puts "Creating Resource ( #{feat['properties']['name']} ) for #{ag.name}" r = Resource.new(feature: feat, agent_id: ag.id) p r p r.errors unless r.valid? exit unless r.save $stderr.puts "Created Resource #{r.uuid}" end end end