Created
June 9, 2010 20:25
-
-
Save kasima/432117 to your computer and use it in GitHub Desktop.
sinatra v8 suggester
This file contains 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
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