Last active
December 24, 2015 17:19
-
-
Save weirdpercent/6834904 to your computer and use it in GitHub Desktop.
This rake task is part of the kvr-tools project. It parses product entries in JSON format into Active Record.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc "Add product entries to database" | |
task :addjson => :environment do | |
require 'multi_json' | |
print 'Adding products to database' | |
fa=[] | |
Dir.glob('json/*.json').select {|f| fa.push f} | |
pa={} | |
x=0 | |
y=fa.length | |
y-=1 | |
fa.sort | |
while x <= y | |
file=File.readlines(fa[x]) | |
pa=MultiJson.load(file.join) | |
ptitle=pa['title'] | |
pcapabilities=pa['capabilities'] | |
if pcapabilities.class == Array then pcapabilities=pcapabilities.join(", "); end | |
pdeveloper=pa['developer'] | |
pformats=pa['formats'] | |
if pformats.class == Array then pformats=pformats.join(", "); end | |
plink=pa['link'] | |
pplatforms=pa['platforms'] | |
if pplatforms.class == Array then pplatforms=pplatforms.join(", "); end | |
prlink=pa['productlink'] | |
if prlink.class == Array then prlink=prlink.join(", "); end | |
psummary=pa['summary'] | |
ptaglist=pa['taglist'] | |
ptaglist=ptaglist.join(", ") | |
Product.create! ::title=>ptitle, :capabilities=>pcapabilities, :developer=>pdeveloper, :formats=>pformats, :link=>plink, :platform=>pplatform, :productlink=>prlink, :summary=>psummary, :taglist=>ptaglist | |
print '.' | |
x+=1 | |
end | |
puts 'done.' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment