Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require_relative 'time_formatter'
class App
def call(env)
@request = Rack::Request.new(env)
time_service_call
end

def time_service_call
return response(404, 'Path error or time format is nil') if (@request.path != '/time' || user_input.nil?)

time_service = TimeFormatter.new(user_input)
if time_service.input_valid?
response(200, time_service.time_output)
else
response(404, "Unknown time format(s): #{time_service.unknown_formats}")
end
end

private

def response(status, content)
Rack::Response.new([content], status, { 'Content-Type' => 'text/plain' }).finish
end

def user_input
@request.params['format']&.split(',')
end
end
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require_relative 'app'
run App.new
24 changes: 24 additions & 0 deletions time_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class TimeFormatter
attr_reader :user_input

FORMATS = { 'year' => '%Y', 'month' => '%m', 'day' => '%d', 'hour' => '%H',
'minute' => '%M', 'second' => '%S' }.freeze

def initialize(user_input)
@user_input = user_input
end

def input_valid?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

при наличии неверного формата, у тебя user_input - FORMATS.keys два раза будет выполнятся. куда-то бы сохранить результат операции чтобы не гонять два раза одну операцию

unknown_formats.empty?
end

def unknown_formats
@unknown_formats = user_input - FORMATS.keys
end

def time_output
output = []
user_input.each { |el| output.push(FORMATS[el]) if FORMATS[el] }
Time.now.strftime(output.join('-'))
end
end