-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
47 lines (36 loc) · 954 Bytes
/
app.rb
File metadata and controls
47 lines (36 loc) · 954 Bytes
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
require_relative 'time_formatter'
class App
def call(env)
@request = Rack::Request.new(env)
return not_found_response if unknown_route?
return no_formats_given_response unless format_given?
formatted_time_response
end
private
def not_found_response
[404, headers, []]
end
def no_formats_given_response
[400, headers, ['Need time format instructions']]
end
def unknown_route?
!@request.get? || @request.path != '/time'
end
def formatted_time_response
formatter = TimeFormatter.new(formats_from_request)
if formatter.valid_formats?
[200, headers, [formatter.datetime]]
else
[400, headers, ["Unknown time format [#{formatter.unknown_formats.join(', ')}]"]]
end
end
def headers
{ 'Content-Type' => 'text/plain' }
end
def formats_from_request
@request.GET['format'].split(',').map(&:strip)
end
def format_given?
formats_from_request.any?
end
end