class Apiify::CsvImporter

Public Instance Methods

create_rake_file() click to toggle source
# File lib/apiify/csv_importer.rb, line 9
def create_rake_file
  File.open("./lib/tasks/importer.rake", 'w') do |f|
    f.write("require 'CSV'
    task :import_csv, [:file_path] => :environment do |t, args|
      file_name = args[:file_path].split('/').last.split('.').first
      model_name = file_name.capitalize.constantize
      table = CSV.table(args[:file_path])
      table.each do |row|
        model_name.create!(row.to_hash)
      end
    end")
  end
end
import(csv_path) click to toggle source
# File lib/apiify/csv_importer.rb, line 3
def import(csv_path)
  create_rake_file
  run_rake_task(csv_path)
  output_message(csv_path)
end
output_message(csv_path) click to toggle source
# File lib/apiify/csv_importer.rb, line 27
def output_message(csv_path)
  rows = CSV.table("#{csv_path}").length
  puts "#{rows} records imported successfully."
end
run_rake_task(csv_path) click to toggle source
# File lib/apiify/csv_importer.rb, line 23
def run_rake_task(csv_path)
  system("bin/rake import_csv\[#{csv_path}\]")
end