Skip to content

Instantly share code, notes, and snippets.

@kasima
Created June 9, 2010 20:25
Show Gist options
  • Save kasima/432117 to your computer and use it in GitHub Desktop.
Save kasima/432117 to your computer and use it in GitHub Desktop.
sinatra v8 suggester
require 'rubygems'
require 'sinatra'
require 'json'
require 'v8'
input = 'suggestions.txt'
suggestions = []
File.open(input, 'r').each { |line| suggestions << line.strip }
cxt = V8::Context.new
cxt['suggestions'] = suggestions
get '/:q' do
# matches = suggestions.find_all{ |s| s.match(params[:q]) }[0..9]
cxt['q'] = params[:q]
matches = Array(cxt.eval(<<-eojs
suggestions.filter(function(suggestion) {
return suggestion.search(q) > -1;
}).slice(0,10);
eojs
))
halt 200, {'Content-Type' => 'text/json'}, matches.to_json
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment