Skip to content

Instantly share code, notes, and snippets.

@aerith
Forked from igrigorik/webapp.rb
Created November 19, 2010 07:42
Show Gist options
  • Save aerith/706231 to your computer and use it in GitHub Desktop.
Save aerith/706231 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
if !func.nil? &&
!func.empty? &&
!Kernel.methods.include?(func.to_sym) &&
self.class.instance_methods.include?(func.to_sym) then
[200, {}, send(func, *attrs)]
else
[404, {}, "Not Found"]
end
end
end
self
end
end
Rack::Handler::Mongrel.run [].webapp, :Port => 9292
# http://localhost:9292/push/1 -> 1
# http://localhost:9292/push/2 -> 12
# http://localhost:9292/push/3 -> 123
# http://localhost:9292/to_a -> 123
# http://localhost:9292/pop -> 3
# http://localhost:9292/shift -> 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment