require 'rubygems'
require 'sinatra'
require 'active_record'
require 'json'

ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'test.db')

class Note < ActiveRecord::Base; end

# Since I named the proxy in Sproutcore "sinatra", requests from Sproutcore need
# to go through '/sinatra'
get '/sinatra' do
Note.find(:all).map {|note| note.text}.to_json
end

get '/sinatra/clear' do
Note.destroy_all
'Bye-bye everybody!'
end

post '/sinatra/:notes' do
notes = JSON.load(params['notes'])
notes.each do |note|
Note.find_or_create_by_text(note)
end
end