Created
July 24, 2012 09:06
-
-
Save ybiquitous/3168990 to your computer and use it in GitHub Desktop.
Sinatra+Sprockets+DataMapper+CoffeeScript
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 foo | |
document.write "<p>#{foo.f(10)}</p>" |
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 '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 |
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 './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 |
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 './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 |
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
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