Skip to content

Instantly share code, notes, and snippets.

@madx
Created July 16, 2009 17:58
Show Gist options
  • Save madx/148563 to your computer and use it in GitHub Desktop.
Save madx/148563 to your computer and use it in GitHub Desktop.
module RestSource
attr_accessor :env
def call(env)
@env = env
method = env['REQUEST_METHOD'].downcase.to_sym
params = env['REQUEST_URI'][1..-1].split('/').collect { |p|
Rack::Utils.unescape(p)
}
puts "Calling #{method} with: #{params.map {|p| p.inspect }.join(', ')}"
begin
status, body = 200, send(method, *params)
rescue NoMethodError
status, body = 500, "Internal error: no action #{method} defined"
rescue ArgumentError
status, body = 500, "Internal error: too much parameters"
end
[status, {"Content-Type" => 'text/html'}, body]
end
end
class App
extend RestSource
def self.get(name="world")
"Hello #{name}!\n"
end
end
run App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment