Skip to content

Instantly share code, notes, and snippets.

@jamiehodge
Created January 15, 2012 22:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiehodge/1617775 to your computer and use it in GitHub Desktop.
Save jamiehodge/1617775 to your computer and use it in GitHub Desktop.
Spine.js contacts demo Sinatra backend
require 'bundler/setup'
Bundler.require
DB = Sequel.sqlite
DB.create_table :contacts do
primary_key :id
String :name
String :email
end
class Contact < Sequel::Model
self.strict_param_setting = false
end
set :port, 3000
set :protection, except: :json_csrf
before do
headers \
'Access-Control-Allow-Origin' => 'http://localhost:9294',
'Access-Control-Allow-Methods' => %w{GET POST PUT DELETE OPTIONS}.join(','),
'Access-Control-Allow-Headers' => %w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(',')
params['contact'] = Yajl::Parser.parse request.body || {}
end
options '/contacts*' do
200
end
get '/contacts/:id' do
@contact = Contact[params['id']]
yajl :show
end
put '/contacts/:id' do
@contact = Contact[params['id']].update(params['contact'])
yajl :show
end
delete '/contacts/:id' do
Contact[params['id']].destroy
200
end
get '/contacts' do
@contacts = Contact.all
yajl :index
end
post '/contacts' do
@contact = Contact.create(params['contact'])
yajl :show
end
__END__
@@index
json = @contacts.map {|c| c.values }
@@show
json = @contact.values
source 'https://rubygems.org'
gem 'sinatra', git: 'git://github.com/sinatra/sinatra.git'
gem 'sequel'
gem 'sqlite3'
gem 'yajl-ruby', require: 'yajl'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment