Skip to content

Instantly share code, notes, and snippets.

@ybiquitous
Created July 24, 2012 09:06
Show Gist options
  • Save ybiquitous/3168990 to your computer and use it in GitHub Desktop.
Save ybiquitous/3168990 to your computer and use it in GitHub Desktop.
Sinatra+Sprockets+DataMapper+CoffeeScript
//= require foo
document.write "<p>#{foo.f(10)}</p>"
require 'sinatra'
require 'json'
require 'data_mapper'
DataMapper.setup(:default, 'sqlite::memory:')
DataMapper::Logger.new($stdout, :debug)
class Book
include DataMapper::Resource
property :id, Serial
property :title, String, :required => true
property :author, String, :required => true
end
DataMapper.auto_upgrade!
before do
content_type :json
end
get '/books' do
Book.all.to_json
end
post '/book' do
newbook = Book.create(
:title => params[:title],
:author => params[:author]
)
{ :id => newbook.id }.to_json
end
require './app'
require 'rspec'
require 'rack/test'
set :environment, :test
describe 'Book RESTful API' do
include Rack::Test::Methods
def app
Sinatra::Application
end
it 'return book list' do
post '/book', :title => 'hoge', :author => 'aaa'
last_response.should be_ok
p last_response.body
get '/books'
last_response.should be_ok
p last_response.body
end
end
require './app'
require 'sprockets'
require 'coffee_script'
map '/assets' do
env = Sprockets::Environment.new
env.append_path 'assets/js'
env.append_path 'assets/css'
run env
end
map '/' do
run Sinatra::Application
end
foo = {}
foo.f = (x) -> num * 2 for num in [0..x]
window.foo = foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment