Created
November 13, 2010 21:28
-
-
Save igrigorik/675667 to your computer and use it in GitHub Desktop.
Inspired by @JEG2's talk at Rubyconf... Any ruby object, as a webapp! 'Cause we can. :-)
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 '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 |
So I updated it a bit in my fork; it removes url encoding and returns json serialization of results
@danny: nice!
How come you didn’t settle with def self.call(env)
instead?
require 'rack'
class Object
def to_webapp
def self.call(env)
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func || :inspect, *attrs)]
end
self
end
end
Rack::Handler::WEBrick.run [].to_webapp, :Port => 9292
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Right, you're seeing the problem I described below. You need to convert everything to a StringIO and then you're good to go.