-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
120 lines (96 loc) · 2.79 KB
/
app.rb
File metadata and controls
120 lines (96 loc) · 2.79 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Require the bundler gem and then call Bundler.require to load in all gems
# listed in Gemfile.
require 'bundler'
Bundler.require
use Rack::Cors do
allow do
origins '*'
resource '*',
:headers => :any,
:methods => [:get, :post, :put, :delete, :options]
end
end
# Setup DataMapper with a database URL. On Heroku, ENV['DATABASE_URL'] will be
# set, when working locally this line will fall back to using SQLite in the
# current directory.
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite://#{Dir.pwd}/development.sqlite")
# Define a simple DataMapper model.
class Thing
include DataMapper::Resource
property :id, Serial, :key => true
property :created_at, DateTime
property :title, String, :length => 255
property :description, Text
end
# Finalize the DataMapper models.
DataMapper.finalize
# Tell DataMapper to update the database according to the definitions above.
DataMapper.auto_upgrade!
get '/' do
send_file './public/index.html'
end
# Route to show all Things, ordered like a blog
get '/things' do
content_type :json
@things = Thing.all(:order => :created_at.desc)
@things.to_json
end
# CREATE: Route to create a new Thing
post '/things' do
content_type :json
headers "Access-Control-Allow-Headers" => "Content-Type"
# These next commented lines are for if you are using Backbone.js
# JSON is sent in the body of the http request. We need to parse the body
# from a string into JSON
params_json = JSON.parse(request.body.read)
# If you are using jQuery's ajax functions, the data goes through in the
# params.
@thing = Thing.new(params_json)
if @thing.save
@thing.to_json
else
halt 500
end
end
# READ: Route to show a specific Thing based on its `id`
get '/things/:id' do
content_type :json
@thing = Thing.get(params[:id])
if @thing
@thing.to_json
else
halt 404
end
end
# UPDATE: Route to update a Thing
put '/things/:id' do
content_type :json
# These next commented lines are for if you are using Backbone.js
# JSON is sent in the body of the http request. We need to parse the body
# from a string into JSON
# params_json = JSON.parse(request.body.read)
# If you are using jQuery's ajax functions, the data goes through in the
# params.
@thing = Thing.get(params[:id])
@thing.update(params)
if @thing.save
@thing.to_json
else
halt 500
end
end
# DELETE: Route to delete a Thing
delete '/things/:id/delete' do
content_type :json
@thing = Thing.get(params[:id])
if @thing.destroy
{:success => "ok"}.to_json
else
halt 500
end
end
# If there are no Things in the database, add a few.
if Thing.count == 0
Thing.create(:title => "Test Thing One", :description => "Sometimes I eat pizza.")
Thing.create(:title => "Test Thing Two", :description => "Other times I eat cookies.")
end