-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.rb
48 lines (40 loc) · 1.12 KB
/
application.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
require_relative 'lib/exceptions'
require_relative 'lib/path_validator'
require_relative 'lib/query_validator'
require_relative 'lib/time_formatter'
class TimeFormatterApplication
INTERNAL_ERROR = 500
SUCCESS = 200
def initialize
@time_formatter = TimeFormatter.new
@path_validator = PathValidator.new
@format_validator = QueryValidator.new
end
def call(env)
begin
req = Rack::Request.new(env)
validate!(req)
status = SUCCESS
request_time_format_str = req.params['format']
content = time_formatter.format(Time.now, request_time_format_str)
rescue ValidationError => error
status = error.error_code
content = error.message
rescue StandardError => error
status = INTERNAL_ERROR
content = error.message
end
[status, headers, [content]]
end
private
attr_reader :time_formatter
attr_reader :path_validator
attr_reader :format_validator
def validate!(request)
path_validator.validate!(request.path_info)
format_validator.validate!(request.params['format'])
end
def headers
{ 'content-type' => 'text/plain' }
end
end