Created
March 15, 2012 03:28
-
-
Save ox/2041696 to your computer and use it in GitHub Desktop.
Sinatra AutoAPI generator in the making
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 'sinatra' | |
require 'sinatra/namespace' | |
require 'mongoid' | |
ENV["RACK_ENV"] ||= "development" | |
Mongoid.load!("mongoid.yml") | |
class Note | |
include Mongoid::Document | |
field :body, type: String | |
end | |
module AutoAPI | |
def generate(model) | |
namespace "/#{model.to_s.downcase}" do | |
get '/' do | |
@models = model.all | |
erb "#{model.to_s.downcase}_index".intern | |
end | |
post '/' do | |
m = model.create(params) | |
m.save! | |
redirect to "/#{model.to_s.downcase}" | |
end | |
get '/new' do | |
erb "#{model.to_s.downcase}_new".intern | |
end | |
end | |
end | |
end | |
include AutoAPI | |
generate(Note) | |
get '/' do | |
"base" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment