-
Notifications
You must be signed in to change notification settings - Fork 1
/
secretsanta.rb
96 lines (81 loc) · 2 KB
/
secretsanta.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
require 'sequel'
require 'sinatra'
require 'yaks'
require 'yaks-sinatra'
require 'yaks-html'
DB = Sequel.connect('sqlite://secretsanta.db')
configure_yaks do
# Make urls absolute, will be pulled into Yaks
after :map do |resource, env|
resource.controls(
resource.controls.map do |control|
base_url = URI("#{env['rack.url_scheme']}://#{env['HTTP_HOST']}")
uri = URI(control.href)
control.href(URI.join(base_url, uri))
end
)
end
end
def create_tables
DB.create_table(:groups) do
primary_key :id
end
DB.create_table(:elves) do
primary_key :id
foreign_key :group_id, :groups
String :name
String :email
String :url_key
foreign_key :recipient_id, :elves
String :wishlist
end
end
def create_some_elves
group = Group.create
%w( John Paul George Ringo ).each do |elf|
Elf.create(group_id: group.id)
end
end
class Group < Sequel::Model
one_to_many :elves
end
class Elf < Sequel::Model
many_to_one :group
end
class RootMapper < Yaks::Mapper
control :create do
method 'POST'
href '/groups'
media_type 'application/x-www-form-urlencoded'
field :name_1, label: 'Name 1', type: 'text'
field :email_1, label: 'Email 1', type: 'text'
field :name_2, label: 'Name 2', type: 'text'
field :email_2, label: 'Email 2', type: 'text'
field :name_3, label: 'Name 3', type: 'text'
field :email_3, label: 'Email 3', type: 'text'
end
end
class GroupMapper < Yaks::Mapper
attributes :id
has_many :elves
end
class ElfMapper < Yaks::Mapper
attributes :name, :wishlist
end
before do
headers 'Access-Control-Allow-Origin' => '*'
headers 'Access-Control-Allow-Headers' => 'Authorization,Accepts,Content-Type,X-CSRF-Token,X-Requested-With'
headers 'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,OPTIONS'
end
# TODO: Serve up the frontend
get '/' do
end
# Starting point for the API
get '/api' do
yaks :root, mapper: RootMapper
end
get '/groups/:id' do
yaks Group[params[:id]]
end
post '/groups' do
end