-
Notifications
You must be signed in to change notification settings - Fork 2
/
response.rb
49 lines (43 loc) · 991 Bytes
/
response.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
require 'json'
module Response
TIMEOUT_LIMIT = 10
STATUS_CODES = {
ok: 200,
error: 500
}
HEADERS = {
'Content-Type'=>'application/json',
'Access-Control-Allow-Origin' => '*'
}
BODY = {
classification: [
{ name: 'Madrid', points: 5 },
{ name: 'Valencia', points: 10 },
{ name: 'Barcelona', points: 7 },
{ name: 'Zaragoza', points: 8 },
{ name: 'Bilbao', points: 9 }
]
}.to_json
class Normal
def self.call(env)
[STATUS_CODES[:ok], HEADERS, [BODY]]
end
end
class Timeout
def self.call(env)
random_time = rand(TIMEOUT_LIMIT)
sleep(random_time)
[STATUS_CODES[:ok], HEADERS, [BODY]]
end
end
class Failure
ERROR_BODY = 'SERVER ERROR'
@@count = 0
def self.call(env)
@@count += 1
random_failure = @@count % 3 == 0
return [STATUS_CODES[:error], HEADERS, [ERROR_BODY]] if random_failure
[STATUS_CODES[:ok], HEADERS, [BODY]]
end
end
end