Skip to content

Instantly share code, notes, and snippets.

@mnyrop
Last active January 18, 2018 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnyrop/8b3e1b4668d3da984b988475869d458c to your computer and use it in GitHub Desktop.
Save mnyrop/8b3e1b4668d3da984b988475869d458c to your computer and use it in GitHub Desktop.
require 'faker'
require 'csv'
require 'yaml'
task :fake_data do
I18n.enforce_available_locales = false
Dir.mkdir('_data') unless File.exists?('_data')
collection_names = []
3.times do # 3 csv files
csv = []
headers = ['pid','title']
6.times { headers << slug(Faker::Witcher.unique.monster)} # with 5 custom headers
7.times do # with 7 rows
row = {}
row[headers[0]] = slug(Faker::Lovecraft.unique.word)
row[headers[1]] = Faker::Lorem.sentence
row[headers[2]] = Faker::TwinPeaks.quote
Faker::Config.locale = 'ru'
row[headers[3]] = Faker::Name.name
Faker::Config.locale = 'fa'
row[headers[4]] = Faker::Name.name
row[headers[5]] = Faker::Commerce.product_name
row[headers[6]] = Faker::File.file_name
row[headers[7]] = Faker::Lovecraft.sentence
csv << row
end
name = slug(Faker::Witcher.unique.monster)
path = '_data/' + name + '.csv'
write_csv(path, csv)
collection_names << name
Faker::Dune.unique.clear
Faker::Lovecraft.unique.clear
end
Rake::Task['wax:config'].invoke
$argv = collection_names
collection_hash = {}
collection_names.each do |name|
collection_hash[name] = {}
collection_hash[name]['source'] = name
collection_hash[name]['directory'] = name
collection_hash[name]['layout'] = 'default'
end
$config['collections'] = collection_hash
output = YAML.dump $config
File.write('_config.yml', output)
Rake::Task['wax:pagemaster'].invoke
end
def slug(str)
return str.downcase.gsub(' ', '_').gsub(/[^\w-]/, '')
end
def write_csv(path, data)
begin
CSV.open(path, 'wb:UTF-8') do |csv|
csv << data.first.keys
data.each do |hash|
csv << hash.values
end
end
puts ("Writing csv data to " + path + ".")
rescue
raise ("Cannot write csv data to "+ path + " for some reason.")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment