-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
55 lines (48 loc) · 1.17 KB
/
server.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
require 'sinatra'
require 'sinatra/cross_origin'
require 'json'
require_relative './db'
# PizzaPlanetServer is awesome
class PizzaPlanetServer < Sinatra::Base
set :bind, '0.0.0.0'
configure do
enable :cross_origin
end
before do
response.headers['Access-Control-Allow-Origin'] = '*'
end
get '/' do
status 200
File.read(File.join('public', 'index.html'))
end
get '/pizzas' do
status 200
DB.read('pizzas').to_json
end
post '/order' do
params = JSON.parse(request.body.read) rescue nil
if params.nil? || params.empty? || params["order"].nil? || params["order"]["items"].nil? || params["order"]["items"].nil?
status 400
'Order malformed'
else
status 201
DB.create('orders', params)
orders = DB.read('orders')
{ orderNumber: (orders.length - 1).to_s,
order: orders.last }.to_json
end
end
get '/order/:ordernumber' do
orders = DB.read('orders')
ordernumber = params[:ordernumber].to_i
order = orders[ordernumber]
if order.nil?
status 404
'Order Not Found'
else
status 200
{ orderNumber: ordernumber,
order: order }.to_json
end
end
end