public
Last active — forked from igrigorik/webapp.rb

Object webapp

  • Download Gist
webapp.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
require 'rubygems'
require 'rack'
 
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
end
self
end
end
 
Rack::Handler::Mongrel.run [].webapp, :Port => 9292
# ^^^^^^^^^^^
# | (x)
# ROFLSCALE DB ---/
#
 
 
# 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
 
# Implementations in other languages (thanks guys!):
# Node.js: https://gist.github.com/700995
# Groovy: https://gist.github.com/702337
# Python: https://gist.github.com/702001
# Perl w/ plack: https://gist.github.com/703620
# Perl w/ continuity: https://gist.github.com/703651
# Io: https://gist.github.com/703431
 
# Great explanation of how this works in Ruby on Stackoverflow:
# http://stackoverflow.com/questions/4198883/exposing-any-ruby-object-over-the-web

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.