Sha256: d5e9f092264dc98fa20f26b53f4cae7c9a7a202c95847495638fb64a69a6e473

Contents?: true

Size: 847 Bytes

Versions: 1

Compression:

Stored size: 847 Bytes

Contents

$:.unshift File.expand_path('../../lib', __FILE__)

require 'sinatra/base'
require 'sequel'
require 'sinatra/backbone'

DB = Sequel.connect("sqlite::memory:")
DB.create_table :books do
  primary_key :id
  String :title
  String :author
end

class Book < Sequel::Model
  def to_hash
    { :id => id, :title => title, :author => author }
  end

  def validate
    errors.add :author, "can't be empty"  if author.to_s.size == 0
  end
end

class App < Sinatra::Base
  enable   :raise_errors, :logging
  enable   :show_exceptions  if development?

  register Sinatra::RestAPI

  rest_create("/book") { Book.new }
  rest_resource("/book/:id") { |id| Book[id] }

  set :root,   File.expand_path('../', __FILE__)
  set :views,  File.expand_path('../', __FILE__)
  set :public, File.expand_path('../public', __FILE__)

  get '/' do
    erb :home
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-backbone-0.1.0.rc2 examples/restapi/app.rb