Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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