Skip to content

Commit

Permalink
Pass rubocop checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
squidarth committed Nov 21, 2016
1 parent 431115f commit 9ae7bc4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
66 changes: 41 additions & 25 deletions app/models/thing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/models/thing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 9ae7bc4

Please sign in to comment.