Skip to content

Instantly share code, notes, and snippets.

@kasima
Created May 22, 2010 03:54
Show Gist options
  • Save kasima/409748 to your computer and use it in GitHub Desktop.
Save kasima/409748 to your computer and use it in GitHub Desktop.
eventmachine suggester
require 'rubygems'
require 'eventmachine'
require 'evma_httpserver'
require 'json'
INPUT = 'suggestions.txt'
class Handler < EventMachine::Connection
include EventMachine::HttpServer
def process_http_request
resp = EventMachine::DelegatedHttpResponse.new( self )
matches = @@suggestions.find_all{ |s| s.match(@http_request_uri[1..-1]) }[0..9]
resp.status = 200
resp.content_type 'text/json'
resp.content = matches.to_json
resp.send_response
end
end
@@suggestions = []
File.open(INPUT, 'r').each { |line| @@suggestions << line.strip }
puts "#{@@suggestions.size} suggestions..."
EventMachine::run {
EventMachine.epoll
EventMachine::start_server("0.0.0.0", 8080, Handler)
puts "Listening..."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment