Sha256: 0bf07f43c11d2e85ad6dc85f9dd6e121452fcaee99ea06c7d4c8f9de4d59f23e

Contents?: true

Size: 850 Bytes

Versions: 2

Compression:

Stored size: 850 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

2 entries across 2 versions & 2 rubygems

Version Path
sinatra-backbone-2-0.1.1 examples/restapi/app.rb
sinatra-backbone-0.1.1 examples/restapi/app.rb