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
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

gem 'pry'
gem 'rack', '~> 1.6', '>= 1.6.4'
19 changes: 19 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.3)
method_source (1.0.0)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
rack (1.6.13)

PLATFORMS
x86_64-darwin-22

DEPENDENCIES
pry
rack (~> 1.6, >= 1.6.4)

BUNDLED WITH
2.3.22
33 changes: 33 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require_relative 'time_format'

class App
def call(env)
request = Rack::Request.new(env)
return not_found unless request.get? && request.path == '/time'

time_format = TimeFormat.new(request.params['format']&.split(','))

if time_format.unknown_formats.empty?
response(body: time_format.formatted_time)
else
bad_request("Unknown time format [#{time_format.unknown_formats.join(',')}]")
end
end

private

def not_found
response(status: 404)
end

def bad_request(body)
response(status: 400, body: body)
end

def response(status: 200, headers: {}, body: [])
response = Rack::Response.new(body, status, headers)
response.finish
end
end
7 changes: 7 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require_relative 'app'
require_relative 'middleware/logger'

use AppLogger, logdev: File.expand_path('log/app.log', __dir__)
run App.new
2 changes: 2 additions & 0 deletions log/app.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Logfile created on 2023-07-13 05:54:24 +0300 by logger.rb/v1.4.2

Choose a reason for hiding this comment

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

папку с логами помещай всегда в гитигнор. этот файл растет очень быстро, ты им просто вес репозитория увеличишь, да и другим разработчикам он не нужен

I, [2023-07-13T05:54:33.501996 #62736] INFO -- : {"GATEWAY_INTERFACE"=>"CGI/1.1", "PATH_INFO"=>"/time", "QUERY_STRING"=>"format=year%2Cmonth%2Cdays%2Cdd", "REMOTE_ADDR"=>"127.0.0.1", "REMOTE_HOST"=>"127.0.0.1", "REQUEST_METHOD"=>"GET", "REQUEST_URI"=>"http://localhost:9292/time?format=year%2Cmonth%2Cdays%2Cdd", "SCRIPT_NAME"=>"", "SERVER_NAME"=>"localhost", "SERVER_PORT"=>"9292", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"WEBrick/1.6.1 (Ruby/2.7.6/2022-04-12)", "HTTP_HOST"=>"localhost:9292", "HTTP_USER_AGENT"=>"curl/7.85.0", "HTTP_ACCEPT"=>"*/*", "rack.version"=>[1, 3], "rack.input"=>#<Rack::Lint::InputWrapper:0x00007f82ef8f3300 @input=#<StringIO:0x00007f82ef8f9a70>>, "rack.errors"=>#<Rack::Lint::ErrorWrapper:0x00007f82ef8f32d8 @error=#<IO:<STDERR>>>, "rack.multithread"=>true, "rack.multiprocess"=>false, "rack.run_once"=>false, "rack.url_scheme"=>"http", "rack.hijack?"=>true, "rack.hijack"=>#<Proc:0x00007f82ef8f35f8 /Users/vs/.rvm/gems/ruby-2.7.6/gems/rack-1.6.13/lib/rack/lint.rb:525>, "rack.hijack_io"=>nil, "HTTP_VERSION"=>"HTTP/1.1", "REQUEST_PATH"=>"/time", "rack.tempfiles"=>[]}
15 changes: 15 additions & 0 deletions middleware/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'logger'

class AppLogger
def initialize(app, **options)
@logger = Logger.new(options[:logdev] || $stdout)
@app = app
end

def call(env)
@logger.info(env)
@app.call(env)
end
end
28 changes: 28 additions & 0 deletions time_format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

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

def initialize(formats)
@formats = formats || []
end

def valid_formats

Choose a reason for hiding this comment

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

это публичный метод?

@formats.select { |format| FORMATS.include?(format) }
end

def unknown_formats
@formats - FORMATS.keys
end

def formatted_time
valid_formats.map { |format| Time.now.strftime(FORMATS[format]) }.join('-')

Choose a reason for hiding this comment

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

у тебя двойной цикл получился. сначала ты выбираешь все правильные параметры.а поотм начинаешь их перебирать чтобы собрать строку.

end
end