Skip to content
Browse files

Add script to generate autocomplete search-top-trumps.js file.

  • Loading branch information...
1 parent 29dbae8 commit 518382abaca34bbf606b340b588026912c744cfb @heathd heathd committed Jan 31, 2012
Showing with 40 additions and 0 deletions.
  1. +40 −0 lib/tasks/autocomplete_build_top_trumps.rake
View
40 lib/tasks/autocomplete_build_top_trumps.rake
@@ -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.
Something went wrong with that request. Please try again.