-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.rb
307 lines (256 loc) · 6.58 KB
/
app.rb
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# encoding: UTF-8
require 'rubygems'
require 'sinatra'
require 'json'
require 'data_mapper'
require 'puma'
require "sinatra/namespace"
require "sinatra/base"
require 'debugger'
require 'haml'
configure :development, :test, :production do
register ::Sinatra::Namespace
set :protection, true
# Allows local requests such as Postman (Chrome extension):
set :protection, origin_whitelist: ["chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm", "http://127.0.0.1"]
set :protect_from_csrf, true
set :server, :puma
# Local Sqlite (Development):
# set :datamapper_url, "sqlite3://#{File.dirname(__FILE__)}/test.sqlite3"
end
# Live Postgres for Heroku (Production):
DataMapper.setup(:default, ENV['HEROKU_POSTGRESQL_AMBER_URL'] || 'postgres://localhost/mydb')
# Local SQlite Locally (Development):
# DataMapper.setup(:default, "sqlite::memory:")
# Main classes for the order of the pixel API.
class Hero
include DataMapper::Resource
belongs_to :weapon
belongs_to :job
belongs_to :race
property :id, Serial
property :name, String, required: true
end
class Weapon
include DataMapper::Resource
has n, :heroes
property :id, Serial
property :name, String, required: true
property :desc, Text, required: true, length: 255
end
class Race
include DataMapper::Resource
has n, :heroes
property :id, Serial
property :name, String, required: true
end
class Job
include DataMapper::Resource
has n, :heroes
property :id, Serial
property :name, String, required: true
end
DataMapper.finalize
DataMapper.auto_migrate!
# Example data.
Job.create(name: 'Paladin')
Race.create(name: 'Human')
Weapon.create(name: 'Mithril Hammer', desc: "The almighty Thor Hammer, gives +10 to all stats")
Hero.create(name: 'Thor', weapon_id: 1, job_id: 1, race_id: 1)
get '/' do
haml :index
end
get '/readme' do
redirect "https://github.com/PixelPerfectTree/order-of-the-pixel"
end
# Namespacing the API for version one.
namespace '/api/v1' do
# Index
get '/heroes' do
heroes = Hero.all
heroes.to_json
end
# Show
get '/heroes/:id' do
hero = Hero.get(params[:id])
if hero.nil?
halt 404
end
hero.to_json
end
# Create
post '/heroes' do
json = request.body.read.to_json
data = JSON.parse(json, :quirks_mode => true)
if data.nil? || data['name'].nil?
halt 400
end
hero = Hero.new(name: params[:name], weapon_id: params[:weapon_id], job_id: params[:job_id], race_id: params[:race_id])
halt 500 unless hero.save
status 201
hero.to_json
end
# Update
put '/heroes/:id' do
json = request.body.read.to_json
data = JSON.parse(json, :quirks_mode => true)
hero ||= Hero.get(params[:id]) || halt(404)
halt 500 unless hero.update(
name: params[:name],
)
hero.to_json
end
# Delete
delete '/heroes/:id' do
hero ||= Hero.get(params[:id]) || halt(404)
halt 404 if hero.nil?
if hero.destroy
"Your hero with an id of #{hero.id} has died with honour."
else
halt 500
end
end
# Weapon routes
# Index
get '/weapons' do
weapons = Weapon.all
weapons.to_json
end
# Show
get '/weapons/:id' do
weapon = Weapon.get(params[:id])
if weapon.nil?
halt 404
end
weapon.to_json
end
# Create
post '/weapons' do
json = request.body.read.to_json
data = JSON.parse(json, :quirks_mode => true)
if data.nil? || data['name'].nil?
halt 400
end
weapon = Weapon.new(name: params[:name], desc: params[:desc])
halt 500 unless weapon.save
[201, {'Location' => "/weapon/#{weapon.id}"}, weapon.to_json]
end
# Update
put '/weapons/:id' do
json = request.body.read.to_json
data = JSON.parse(json, :quirks_mode => true)
weapon ||= Weapon.get(params[:id]) || halt(404)
halt 500 unless weapon.update(
name: params[:name],
)
weapon.to_json
end
# Delete
delete '/weapons/:id' do
weapon ||= Weapon.get(params[:id]) || halt(404)
halt 404 if weapon.nil?
if weapon.destroy
"Your weapon with an id of #{weapon.id} has been reduced to ashes!."
else
status 500
body "The weapon with an id of #{weapon.id} doesn't exist or is related to a hero and can't be deleted."
end
end
# RACE routes
# Index
get '/races' do
races = Race.all
races.to_json
end
# Show
get '/races/:id' do
race = Race.get(params[:id])
if race.nil?
halt 404
end
race.to_json
end
# Create
post '/races' do
json = request.body.read.to_json
data = JSON.parse(json, :quirks_mode => true)
if data.nil? || data['name'].nil?
halt 400
end
race = Race.new(name: params[:name])
halt 500 unless race.save
[201, {'Location' => "/race/#{race.id}"}, race.to_json]
end
# Update
put '/races/:id' do
json = request.body.read.to_json
data = JSON.parse(json, :quirks_mode => true)
race ||= Race.get(params[:id]) || halt(404)
halt 500 unless race.update(
name: params[:name],
)
race.to_json
end
# Delete
delete '/races/:id' do
race ||= Race.get(params[:id]) || halt(404)
halt 404 if race.nil?
if race.destroy
"The race with an id of #{race.id} is now extinct."
else
status 500
body "The race with an id of #{race.id} doesn't exist or is related to a hero and can't be deleted."
end
end
# JOB routes
# Index
get '/jobs' do
jobs = Job.all
jobs.to_json
end
# Show
get '/jobs/:id' do
job = Job.get(params[:id])
if job.nil?
halt 404
end
job.to_json
end
# Create
post '/jobs' do
json = request.body.read.to_json
data = JSON.parse(json, :quirks_mode => true)
if data.nil? || data['name'].nil?
halt 400
end
job = Job.new(name: params[:name])
halt 500 unless job.save
[201, {'Location' => "/job/#{job.id}"}, job.to_json]
end
# Show
put '/jobs/:id' do
json = request.body.read.to_json
data = JSON.parse(json, :quirks_mode => true)
job ||= Job.get(params[:id]) || halt(404)
halt 500 unless job.update(
name: params[:name],
)
job.to_json
end
# Delete
delete '/jobs/:id' do
job ||= Job.get(params[:id]) || halt(404)
if job.destroy
"Your job with an id of #{job.id} has been eliminated."
else
status 500
body "The job with an id of #{job.id} doesn't exist or is related to a hero and can't be deleted."
end
end
before do
content_type 'application/json'
headers["X-CSRF-Token"] = session[:csrf] ||= SecureRandom.hex(32)
# To allow Cross Domain XHR
headers["Access-Control-Allow-Origin"] ||= request.env["HTTP_ORIGIN"]
end
end