Skip to content

Instantly share code, notes, and snippets.

@weirdpercent
Last active December 24, 2015 17:19
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 weirdpercent/6834904 to your computer and use it in GitHub Desktop.
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.
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