Skip to content
This repository has been archived by the owner on Oct 12, 2018. It is now read-only.

Commit

Permalink
Add script to generate autocomplete search-top-trumps.js file.
Browse files Browse the repository at this point in the history
  • Loading branch information
heathd committed Jan 31, 2012
1 parent 29dbae8 commit 518382a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/tasks/autocomplete_build_top_trumps.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace :autocomplete do
desc 'Find all the titles and formats for the slugs in autocomplete-top-trumps.csv, writes out search-top-trumps.json'
task :build_top_trumps => :environment do
require 'csv'
require 'json'

input_file = "autocomplete-top-trumps.csv"
output_file = "search-top-trumps.json"
raise "Can't read #{input_file}" unless File.exist?(input_file)

docs = []
i = 0
CSV.foreach(input_file) do |row|
i +=1
next if i == 1
phrase, slug, weight = row
slug = slug.gsub('/', '')
a = Artefact.where(slug: slug).first
if a
docs << {
"title" => a.name,
"link" => "/" + a.slug,
"format" => a.kind,
"keywords" => phrase,
"weight" => weight
}
else
raise "Couldn't find slug '#{slug}'"
end
end

File.open(output_file, "w") do |f|
f << %q{if (typeof(GDS) == 'undefined') { GDS = {}; };} + "\n"
f << %q{GDS.search_top_trumps = }
f << JSON.dump(docs).gsub(/},{/, "},\n{")
f << ";\n"
end
end
end

0 comments on commit 518382a

Please sign in to comment.