Browse Source

BikeCsvImporter: refactor importer to output log messages to stdoud

master
Ilya Konanykhin 8 years ago
parent
commit
072f303c65
  1. 23
      app/models/bike_csv_importer.rb

23
app/models/bike_csv_importer.rb

@ -22,22 +22,28 @@ class BikeCsvImporter
end end
def run(dry_run) def run(dry_run)
result = {imported: {}, skipped: {}} imported_count, skipped_count = 0, 0
puts "Performing a #{dry_run ? 'DRY RUN' : 'LIVE RUN'} of import"
fetch do |bike_hash| fetch do |bike_hash|
bike = new_bike bike_hash bike = new_bike bike_hash
check_method = dry_run ? :valid? : :save check_method = dry_run ? :valid? : :save
if bike.try check_method if bike.try check_method
result[:imported][bike.shop_id] = bike.inspect puts "Imported #{bike.shop_id}: #{bike}"
imported_count += 1
else else
result[:skipped][bike.try(:shop_id) || bike_hash.values.first] = bike.try(:errors).try(:messages) puts "Skipped #{bike.try(:shop_id) || bike_hash.values.first}: #{bike.try(:errors).try(:full_messages).try :join, '; '}"
skipped_count += 1
end end
end end
result puts "#{imported_count} bikes imported, #{skipped_count} bikes skipped, total of #{imported_count + skipped_count} rows in the CSV"
end end
def analyze(fields = []) def analyze(fields = [])
puts "Analyzing CSV values frequency for #{fields.any? ? fields.join(', ') + ' field' : 'all fields'}"
fields = fields.map &:downcase fields = fields.map &:downcase
grouped = {} grouped = {}
fetch do |bike_hash| fetch do |bike_hash|
@ -48,7 +54,14 @@ class BikeCsvImporter
grouped[key][value] += 1 grouped[key][value] += 1
end end
end end
grouped
grouped.each do |field, values|
puts "#{field}:"
values.each do |value, count|
puts "\t#{value.inspect}: #{count}"
end
puts "\tTotal of #{values.count} distinct values"
end
end end

Loading…
Cancel
Save