From 9ae7bc4fc53a3376d21dbed289a964867730aa35 Mon Sep 17 00:00:00 2001 From: Sid Shanker Date: Sat, 19 Nov 2016 18:16:34 -0800 Subject: [PATCH] Pass rubocop checks. --- app/models/thing.rb | 66 ++++++++++++++++++++++++--------------- test/models/thing_test.rb | 4 +-- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/app/models/thing.rb b/app/models/thing.rb index a446036c..c94488e4 100644 --- a/app/models/thing.rb +++ b/app/models/thing.rb @@ -4,6 +4,9 @@ class Thing < ActiveRecord::Base extend Forwardable include ActiveModel::ForbiddenAttributesProtection + + VALID_DRAIN_TYPES = ['Storm Water Inlet Drain', 'Catch Basin Drain'].freeze + belongs_to :user def_delegators :reverse_geocode, :city, :country, :country_code, :full_address, :state, :street_address, :street_name, @@ -42,37 +45,50 @@ def detail_link nil end - def self.load_drains(source_url) - puts 'Downloading Drains... ... ...' + def self._get_parsed_drains_csv(source_url) csv_string = open(source_url).read - drains = CSV.parse(csv_string, headers: true) - puts "Downloaded #{drains.size} Drains." + CSV.parse(csv_string, headers: true) + end - city_ids = drains.map { |drain| - drain["PUC_Maximo_Asset_ID"].gsub("N-", "") - } + def self._delete_non_existing_drains(drains_from_source) + city_ids = drains_from_source.map do |drain| + drain['PUC_Maximo_Asset_ID'].gsub('N-', '') + end Thing.where.not(city_id: city_ids).delete_all + end + + def self._drain_params(drain) + (lat, lng) = drain['Location'].delete('()').split(',').map(&:strip) + { + name: drain['Drain_Type'], + system_use_code: drain['System_Use_Code'], + lat: lat, + lng: lng, + } + end + + def self._create_or_update_drain(drain) + city_id = drain['PUC_Maximo_Asset_ID'].gsub('N-', '') + thing = Thing.where(city_id: city_id).first_or_initialize + if thing.new_record? + Rails.logger.info("Creating thing #{city_id}") + else + Rails.logger.info("Updating thing #{city_id}") + end + thing.update_attributes!(_drain_params(drain)) + end + + def self.load_drains(source_url) + Rails.logger.info('Downloading Drains... ... ...') + drains = _get_parsed_drains_csv(source_url) + Rails.logger.info("Downloaded #{drains.size} Drains.") + + _delete_non_existing_drains(drains) drains.each do |drain| - next unless ['Storm Water Inlet Drain', 'Catch Basin Drain'].include?(drain['Drain_Type']) - - (lat, lng) = drain['Location'].delete('()').split(',').map(&:strip) - - thing_hash = { - name: drain['Drain_Type'], - system_use_code: drain['System_Use_Code'], - lat: lat, - lng: lng, - } - - thing = Thing.where(city_id: drain['PUC_Maximo_Asset_ID'].gsub('N-', '')).first_or_initialize - if thing.new_record? - puts "Creating thing #{thing_hash[:city_id]}" - else - puts "Updating thing #{thing_hash[:city_id]}" - end - thing.update_attributes!(thing_hash) + next unless VALID_DRAIN_TYPES.include?(drain['Drain_Type']) + _create_or_update_drain(drain) end end end diff --git a/test/models/thing_test.rb b/test/models/thing_test.rb index 72a7a644..d06c1b69 100644 --- a/test/models/thing_test.rb +++ b/test/models/thing_test.rb @@ -22,8 +22,8 @@ class ThingTest < ActiveSupport::TestCase fake_url = 'http://sf-drain-data.org' fake_response = [ - "PUC_Maximo_Asset_ID,Drain_Type,System_Use_Code,Location", - "N-11,Catch Basin Drain,ABC,\"(37.75, -122.40)\"", + 'PUC_Maximo_Asset_ID,Drain_Type,System_Use_Code,Location', + 'N-11,Catch Basin Drain,ABC,"(37.75, -122.40)"', ].join("\n") stub_request(:get, fake_url).to_return(body: fake_response)