-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.rb
188 lines (152 loc) · 4.81 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
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
require 'sinatra'
require 'json'
require 'pry'
require 'active_model'
$home = {}
class Home
include ActiveModel::Validations
attr_accessor :town, :name, :description, :domain_name, :content_version
validates :town, presence: true
validates :name, presence: true
validates :description, presence: true
validates :domain_name,
format: { with: /\.cloudfront\.net\z/, message: "domain must be from .cloudfront.net" }
# uniqueness: true,
validates :content_version, numericality: { only_integer: true }
end
class TerraTownsMockServer < Sinatra::Base
def error code, message
halt code, {'Content-Type' => 'application/json'}, {err: message}.to_json
end
def error_json json
halt code, {'Content-Type' => 'application/json'}, json
end
def ensure_correct_headings
unless request.env["CONTENT_TYPE"] == "application/json"
error 415, "expected Content_type header to be application/json"
end
unless request.env["HTTP_ACCEPT"] == "application/json"
error 406, "expected Accept header to be application/json"
end
end
def x_access_code
'9b49b3fb-b8e9-483c-b703-97ba88eef8e0'
end
def x_user_uuid
'e328f4ab-b99f-421c-84c9-4ccea042c7d1'
end
def find_user_by_bearer_token
auth_header = request.env["HTTP_AUTHORIZATION"]
if auth_header.nil? || !auth_header.start_with?("Bearer ")
error 401, "a1000 Failed to authenicate, bearer token invalid and/or teacherseat_user_uuid invalid"
end
code = auth_header.split("Bearer ")[1]
if code != x_access_code
error 401, "a1001 Failed to authenicate, bearer token invalid and/or teacherseat_user_uuid invalid"
end
if params['user_uuid'].nil?
error 401, "a1002 Failed to authenicate, bearer token invalid and/or teacherseat_user_uuid invalid"
end
unless code == x_access_code && params['user_uuid'] == x_user_uuid
error 401, "a1003 Failed to authenicate, bearer token invalid and/or teacherseat_user_uuid invalid"
end
end
# CREATE
post '/api/u/:user_uuid/homes' do
ensure_correct_headings
find_user_by_bearer_token
puts "# create - POST /api/homes"
begin
payload = JSON.parse(request.body.read)
rescue JSON::ParserError
halt 422, "Malformed JSON"
end
# Validate payload data
name = payload["name"]
description = payload["description"]
domain_name = payload["domain_name"]
content_version = payload["content_version"]
town = payload["town"]
puts "name #{name}"
puts "description #{description}"
puts "domain_name #{domain_name}"
puts "content_version #{content_version}"
puts "town #{town}"
home = Home.new
home.town = town
home.name = name
home.description = description
home.domain_name = domain_name
home.content_version = content_version
unless home.valid?
error 422, home.errors.messages.to_json
end
uuid = SecureRandom.uuid
puts "uuid #{uuid}"
$home = {
uuid: uuid,
name: name,
town: town,
description: description,
domain_name: domain_name,
content_version: content_version
}
return { uuid: uuid }.to_json
end
# READ
get '/api/u/:user_uuid/homes/:uuid' do
ensure_correct_headings
find_user_by_bearer_token
puts "# read - GET /api/homes/:uuid"
# checks for house limit
content_type :json
if params[:uuid] == $home[:uuid]
return $home.to_json
else
error 404, "failed to find home with provided uuid and bearer token"
end
end
# UPDATE
put '/api/u/:user_uuid/homes/:uuid' do
ensure_correct_headings
find_user_by_bearer_token
puts "# update - PUT /api/homes/:uuid"
begin
# Parse JSON payload from the request body
payload = JSON.parse(request.body.read)
rescue JSON::ParserError
halt 422, "Malformed JSON"
end
# Validate payload data
name = payload["name"]
description = payload["description"]
domain_name = payload["domain_name"]
content_version = payload["content_version"]
unless params[:uuid] == $home[:uuid]
error 404, "failed to find home with provided uuid and bearer token"
end
home = Home.new
home.town = $home[:town]
home.name = name
home.description = description
home.domain_name = domain_name
home.content_version = content_version
unless home.valid?
error 422, home.errors.messages.to_json
end
return { uuid: params[:uuid] }.to_json
end
# DELETE
delete '/api/u/:user_uuid/homes/:uuid' do
ensure_correct_headings
find_user_by_bearer_token
puts "# delete - DELETE /api/homes/:uuid"
content_type :json
if params[:uuid] != $home[:uuid]
error 404, "failed to find home with provided uuid and bearer token"
end
$home = {}
{ message: "House deleted successfully" }.to_json
end
end
TerraTownsMockServer.run!