-
Notifications
You must be signed in to change notification settings - Fork 0
feat: rack time format #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
supronva-github
wants to merge
1
commit into
main
Choose a base branch
from
lesson-19
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| 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"=>[]} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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('-') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. у тебя двойной цикл получился. сначала ты выбираешь все правильные параметры.а поотм начинаешь их перебирать чтобы собрать строку. |
||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
папку с логами помещай всегда в гитигнор. этот файл растет очень быстро, ты им просто вес репозитория увеличишь, да и другим разработчикам он не нужен