-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rb
48 lines (38 loc) · 1013 Bytes
/
main.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
# SPDX-License-Identifier: AGPL-3.0-only
# SPDX-FileCopyrightText: 2022 Hugo Peixoto <[email protected]>
require 'sinatra'
require './database.rb'
require './models.rb'
get '/' do
challenges = Challenge.all
users = User.all.sort_by { |u| -u.score(challenges) }
ERB
.new(File.read("index.html.erb"), trim_mode: "<>-")
.result_with_hash(users: users, challenges: challenges)
end
get '/index.json' do
content_type 'application/json'
challenges = Challenge.all
users = User.all.sort_by { |u| -u.score(challenges) }
{
challenges: challenges.map do |c|
{
kata: "https://codewars.com/kata/#{c.kata}",
year: c.year,
week: c.week,
}
end,
users: users.map do |u|
{
username: u.username,
score: u.score(challenges),
points: u.points(challenges),
}
end,
}.to_json
end
post '/refresh' do
halt 403 if request.env["HTTP_AUTHORIZATION"] != ENV["TOKEN"]
User.all.each(&:refresh!)
"ok".to_json
end