Skip to content

Instantly share code, notes, and snippets.

@ox
Created March 15, 2012 03:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ox/2041696 to your computer and use it in GitHub Desktop.
Save ox/2041696 to your computer and use it in GitHub Desktop.
Sinatra AutoAPI generator in the making
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