Skip to content

Instantly share code, notes, and snippets.

@jamiehodge
Created November 3, 2011 22:58
Show Gist options
  • Save jamiehodge/1338190 to your computer and use it in GitHub Desktop.
Save jamiehodge/1338190 to your computer and use it in GitHub Desktop.
Sinatra human and api routes
module App
class Users < App::Base
use Rack::Parser
helpers do
def cycle
@cycle ||= %w{odd even}.cycle
end
end
before provides: :html do
env['warden'].authenticate!
end
before provides: :json do
env['warden'].authenticate! scope: :api
end
before %r{^/(?<id>\d+)} do
not_found unless @user = User[params[:id]]
end
# index
get '/', provides: :html do
slim :'users/index', locals: { users: User.all }
end
get '/', provides: :json do
MultiJson.encode(User.all.map {|user| user.values })
end
# new
get '/new' do
slim :'users/new'
end
# create
post '/' do
@user = User.create(params[:user])
pass
end
post '/', provides: :html do
redirect to "/#{@user.id}"
end
post '/', provides: :json do
headers \
'Location' => url("/#{@user.id}"),
'Content-Location' => url("/#{@user.id}")
status 201
MutiJson.encode(@user.values)
end
# read
get '/:id', provides: :html do
slim :'users/show', locals: { user: @user }
end
get '/:id', provides: :json do
MultiJson.encode(@user.values)
end
# edit
get '/:id/edit' do
slim :'users/edit', locals: { user: @user }
end
# update
put '/:id' do
@user.update(params[:user])
pass
end
put '/:id', provides: :html do
redirect to "/#{@user.id}"
end
put '/:id', provides: :json do
204
end
# destroy
delete '/:id' do
halt 403 unless @user.destroy
pass
end
delete '/:id', provides: :html do
redirect to '/'
end
delete '/:id', provides: :json do
204
end
# errors
error Sequel::ValidationFailed, Sequel::HookFailed do
422
end
end
end
@rkh
Copy link

rkh commented Nov 4, 2011

It would be possible to create something like InheritedResource from this. You could just inherit from this and make the model pluggable. Also, you could use respond_with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment