From ea2a325860f9d315e866007826faccce18a2a294 Mon Sep 17 00:00:00 2001 From: ksilex Date: Thu, 18 Feb 2021 16:54:47 +0300 Subject: [PATCH 1/6] init --- config.ru | 2 ++ time_app.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 config.ru create mode 100644 time_app.rb diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..41170ea --- /dev/null +++ b/config.ru @@ -0,0 +1,2 @@ +require_relative 'time_app' +run TimeApp.new diff --git a/time_app.rb b/time_app.rb new file mode 100644 index 0000000..93d88b5 --- /dev/null +++ b/time_app.rb @@ -0,0 +1,19 @@ +require 'rack' + +FORMATS = %w[year month day hour minute second].freeze +class TimeApp + def call(env) + @request = Rack::Request.new(env) + response.finish + end + + def response + if @request.path != "/time" + Rack::Response.new(['error'], 404, { 'Content-Type' => 'text/plain' }) + else + print FORMATS + Rack::Response.new(['time'], 200, { 'Content-Type' => 'text/plain' }) + end + end + +end From 106a9ab44d2e8ef6863952ee82f957b6104f2d1b Mon Sep 17 00:00:00 2001 From: ksilex Date: Thu, 18 Feb 2021 16:55:07 +0300 Subject: [PATCH 2/6] init --- time_app.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/time_app.rb b/time_app.rb index 93d88b5..703683a 100644 --- a/time_app.rb +++ b/time_app.rb @@ -11,7 +11,7 @@ def response if @request.path != "/time" Rack::Response.new(['error'], 404, { 'Content-Type' => 'text/plain' }) else - print FORMATS + @request.params Rack::Response.new(['time'], 200, { 'Content-Type' => 'text/plain' }) end end From 12fe7f9038e1a2281cd0624136a27e5c844b7934 Mon Sep 17 00:00:00 2001 From: ksilex Date: Fri, 19 Feb 2021 10:24:24 +0300 Subject: [PATCH 3/6] response created --- time_app.rb | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/time_app.rb b/time_app.rb index 703683a..b991fcd 100644 --- a/time_app.rb +++ b/time_app.rb @@ -1,19 +1,34 @@ -require 'rack' - -FORMATS = %w[year month day hour minute second].freeze +FORMATS = { 'year' => '%Y', 'month' => '%m', 'day' => '%d', 'hour' => '%H', + 'minute' => '%M', 'second' => '%S' }.freeze class TimeApp def call(env) @request = Rack::Request.new(env) - response.finish + response_conditions end - def response - if @request.path != "/time" - Rack::Response.new(['error'], 404, { 'Content-Type' => 'text/plain' }) + def response_conditions + return response(404, 'Path error or time format is nil') if (@request.path != '/time' || user_input.nil?) + + if (user_input - FORMATS.keys).empty? + response(200, time_output) else - @request.params - Rack::Response.new(['time'], 200, { 'Content-Type' => 'text/plain' }) + response(404, "Unknown time format(s): #{user_input - FORMATS.keys}") 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 + + def time_output + output = [] + user_input.each { |el| output.push(FORMATS[el]) if FORMATS[el] } + Time.now.strftime(output.join('-')) + end end From 2a285ee9a9330eb7992aebe38c06d275e5e7e507 Mon Sep 17 00:00:00 2001 From: ksilex Date: Fri, 19 Feb 2021 10:46:40 +0300 Subject: [PATCH 4/6] const replace --- time_app.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/time_app.rb b/time_app.rb index b991fcd..091e708 100644 --- a/time_app.rb +++ b/time_app.rb @@ -1,6 +1,7 @@ -FORMATS = { 'year' => '%Y', 'month' => '%m', 'day' => '%d', 'hour' => '%H', - 'minute' => '%M', 'second' => '%S' }.freeze class TimeApp + FORMATS = { 'year' => '%Y', 'month' => '%m', 'day' => '%d', 'hour' => '%H', + 'minute' => '%M', 'second' => '%S' }.freeze + def call(env) @request = Rack::Request.new(env) response_conditions From 7c6fa3887f51dc319d4097463e8aa692bb8c8ebc Mon Sep 17 00:00:00 2001 From: ksilex Date: Fri, 19 Feb 2021 23:06:13 +0300 Subject: [PATCH 5/6] refactoring --- app.rb | 28 ++++++++++++++++++++++++++++ config.ru | 4 ++-- time_app.rb | 35 ----------------------------------- time_formatter.rb | 24 ++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 37 deletions(-) create mode 100644 app.rb delete mode 100644 time_app.rb create mode 100644 time_formatter.rb diff --git a/app.rb b/app.rb new file mode 100644 index 0000000..afc3c27 --- /dev/null +++ b/app.rb @@ -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 diff --git a/config.ru b/config.ru index 41170ea..918a83d 100644 --- a/config.ru +++ b/config.ru @@ -1,2 +1,2 @@ -require_relative 'time_app' -run TimeApp.new +require_relative 'app' +run App.new diff --git a/time_app.rb b/time_app.rb deleted file mode 100644 index 091e708..0000000 --- a/time_app.rb +++ /dev/null @@ -1,35 +0,0 @@ -class TimeApp - FORMATS = { 'year' => '%Y', 'month' => '%m', 'day' => '%d', 'hour' => '%H', - 'minute' => '%M', 'second' => '%S' }.freeze - - def call(env) - @request = Rack::Request.new(env) - response_conditions - end - - def response_conditions - return response(404, 'Path error or time format is nil') if (@request.path != '/time' || user_input.nil?) - - if (user_input - FORMATS.keys).empty? - response(200, time_output) - else - response(404, "Unknown time format(s): #{user_input - FORMATS.keys}") - 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 - - def time_output - output = [] - user_input.each { |el| output.push(FORMATS[el]) if FORMATS[el] } - Time.now.strftime(output.join('-')) - end -end diff --git a/time_formatter.rb b/time_formatter.rb new file mode 100644 index 0000000..8cd5e9a --- /dev/null +++ b/time_formatter.rb @@ -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? + (user_input - FORMATS.keys).empty? + end + + def 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 From 7704178d6d2951e8fba42fbe98ddc3118b8fded6 Mon Sep 17 00:00:00 2001 From: ksilex Date: Sat, 20 Feb 2021 11:02:15 +0300 Subject: [PATCH 6/6] fixes --- time_formatter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/time_formatter.rb b/time_formatter.rb index 8cd5e9a..4f6ffb1 100644 --- a/time_formatter.rb +++ b/time_formatter.rb @@ -9,11 +9,11 @@ def initialize(user_input) end def input_valid? - (user_input - FORMATS.keys).empty? + unknown_formats.empty? end def unknown_formats - user_input - FORMATS.keys + @unknown_formats = user_input - FORMATS.keys end def time_output