-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
98 lines (90 loc) · 2.76 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
require 'cuba'
require 'ohm'
require_relative 'lib/challenge'
require_relative 'lib/submission'
require_relative 'lib/registration'
BASE_URL = "http://scvsoft.com/challenge-accepted/"
Challenge.all = [
Challenge.new(
1,
'Sheldon Cooper',
"ch-1"),
Challenge.new(
2,
'Walter White',
"ea660fcbaf91db3d"),
Challenge.new(
3,
'Nikola Tesla',
"107ebb8c601db219"),
Challenge.new(
4,
'Stephen Hawking',
"eb86accd09f2b31d"),
Challenge.new(
5,
'Spock',
"e0f4b7103bb86b9e")
]
Cuba.define do
def self.handle_submission_for(challenge)
on param('token'), param('code'), param('type') do |token, code, type|
unless Registration.find(token: token).empty?
if challenge.passed?(code, type)
Submission.create(
challenge_number: challenge.number,
token: token,
code: code,
type: type,
created_at: Time.now.to_f) if Submission.find(challenge_number: challenge.number, token: token).empty?
res.status = 200
res['X-NERD-LEVEL'] = challenge.nerd_level
if next_challenge = Challenge.next(challenge)
res.write "Next exercise: #{BASE_URL}#{next_challenge.path}.html"
else
res.write "You completed all exercises! Congratulations! We're checking if all your answers are valid and will publish the final result at http://scvsoft.com/challenge-accepted/winners on December 6, 2013 (also we will send you an email)."
end
else
res.status = 422
res.write "WRONG! The output of the test was: #{challenge.output}"
end
else
res.status = 401
res.write "Your token is not registered!"
end
end
on default do
res.status = 400
end
end
on 'challenge-accepted' do
on post do
on 'register' do
on param('email') do |email|
registration = Registration.new(email: email.downcase)
registration.generate_token
res['Access-Control-Allow-Origin'] = "*"
begin
if registration.save
res.status = 200
res.write "{ \"registration_token\": \"#{registration.token}\" }"
else
res.status = 422
res.write "Hubo un problema con tu registración: Email incorrecto."
end
rescue Ohm::UniqueIndexViolation
registration = Registration.find(email: email.downcase).first
res.status = 200
res.write "{ \"registration_token\": \"#{registration.token}\"}"
end
end
end
Challenge.all.each do |challenge|
on(challenge.path) { handle_submission_for(challenge) }
end
on default do
res.status = 404
end
end
end
end