mirror of
https://github.com/fspc/BikeShed-1.git
synced 2025-02-28 08:43:23 -05:00
Create rake task (import:bikes:csv, import:bikes:analyze_csv) & importing class
This commit is contained in:
parent
21fba7251f
commit
904b824c8b
67
app/models/bikes_csv_importer.rb
Normal file
67
app/models/bikes_csv_importer.rb
Normal file
@ -0,0 +1,67 @@
|
||||
require 'csv'
|
||||
|
||||
class BikesCsvImporter
|
||||
attr_reader :file
|
||||
|
||||
def initialize(file)
|
||||
@file = file
|
||||
end
|
||||
|
||||
def run
|
||||
fetch do |bike_hash|
|
||||
import_bike bike_hash
|
||||
end
|
||||
end
|
||||
|
||||
def analyze(fields = [])
|
||||
fields = fields.map &:downcase
|
||||
grouped = {}
|
||||
fetch do |bike_hash|
|
||||
bike_hash.each do |key, value|
|
||||
next if fields.any? && !fields.include?(key.downcase)
|
||||
grouped[key] ||= {}
|
||||
grouped[key][value] ||= 0
|
||||
grouped[key][value] += 1
|
||||
end
|
||||
end
|
||||
grouped
|
||||
end
|
||||
|
||||
|
||||
|
||||
private
|
||||
|
||||
def fetch
|
||||
CSV.foreach(file).each_with_index do |row, i|
|
||||
if i.zero?
|
||||
parse_header row
|
||||
else
|
||||
yield parse_bike(row)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_header(row)
|
||||
@header = row
|
||||
end
|
||||
|
||||
def parse_bike(row)
|
||||
@header.zip(row).to_h
|
||||
end
|
||||
|
||||
def import_bike(bike_hash)
|
||||
# TODO
|
||||
end
|
||||
|
||||
def clean_value(value)
|
||||
value_or_nil strip_value(value)
|
||||
end
|
||||
|
||||
def strip_value(value)
|
||||
value.try(:strip).try(:gsub, /\n|\r/, '')
|
||||
end
|
||||
|
||||
def value_or_nil(value)
|
||||
return value unless ['?', 'n/a', 'missing', ''].include? value.try(:downcase)
|
||||
end
|
||||
end
|
19
lib/tasks/import.rake
Normal file
19
lib/tasks/import.rake
Normal file
@ -0,0 +1,19 @@
|
||||
namespace :import do
|
||||
namespace :bikes do
|
||||
# Imports bikes info from CSV file
|
||||
task :csv, [:file] => :environment do |t, args|
|
||||
file = args[:file]
|
||||
next puts "Usage: rake #{t.name}[$csv_file_path]" unless file
|
||||
next puts "File #{file} does not exist or is unreachable" unless File.readable? file
|
||||
pp BikesCsvImporter.new(file).run
|
||||
end
|
||||
|
||||
# Analyze a single field from CSV file
|
||||
task :analyze_csv, [:file, :field] => :environment do |t, args|
|
||||
file, field = args.values_at :file, :field
|
||||
next puts "Usage: rake #{t.name}[$csv_file_path[,\"$field_name\"]]" unless file
|
||||
next puts "File #{file} does not exist or is unreachable" unless File.readable? file
|
||||
pp BikesCsvImporter.new(file).analyze field ? [field] : []
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user