Create a gist now

Instantly share code, notes, and snippets.

require 'rake'
namespace :data do
require 'open-uri'
require 'csv'
task load_drains: :environment do
puts 'Downloading Drains... ... ...'
url = 'durham.csv'
csv_string = open(url).read
drains = CSV.parse(csv_string, headers: true)
puts "Downloaded #{drains.size} Drains."
drains.each do |drain|
# lat,lon,owner,status,type
# next unless ['Storm Water Inlet Drain', 'Catch Basin Drain'].include?(drain['type'])
lat = drain['lat']
lng = drain['lon']
thing_hash = {
name: drain['type'],
system_use_code: drain['type'],
lat: lat,
lng: lng,
}
thing = Thing.where(city_id: 'Durham').first_or_initialize
if thing.new_record?
puts "Updating thing #{thing_hash[:city_id]}"
else
puts "Creating thing #{thing_hash[:city_id]}"
end
thing.update_attributes!(thing_hash)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment