-
Notifications
You must be signed in to change notification settings - Fork 0
Task #2
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
base: master
Are you sure you want to change the base?
Task #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,4 +8,8 @@ def create | |
|
|
||
| end | ||
|
|
||
| def show | ||
| render plain: params[:id] | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,4 +9,4 @@ | |
|
|
||
| <p><%= @time %></p> | ||
| </body> | ||
| </html> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <title>Show Page | Simpler application</title> | ||
| </head> | ||
| <body> | ||
| <h1>Simpler framework at work!</h1> | ||
|
|
||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| require_relative 'config/environment' | ||
|
|
||
| require_relative 'lib/simpler_logger' | ||
| use SimplerLogger | ||
| run Simpler.application |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| Simpler.application.routes do | ||
| get '/tests', 'tests#index' | ||
| post '/tests', 'tests#create' | ||
| get '/tests/:id', 'tests#show' | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,39 @@ | ||
| module Simpler | ||
| class Router | ||
| class Route | ||
|
|
||
| attr_reader :controller, :action | ||
| attr_reader :controller, :action, :params | ||
|
|
||
| def initialize(method, path, controller, action) | ||
| @method = method | ||
| @path = path | ||
| @controller = controller | ||
| @action = action | ||
| @params = {} | ||
| end | ||
|
|
||
| def match?(method, path) | ||
| @method == method && path.match(@path) | ||
| @method == method && path_handler(path) | ||
| end | ||
|
|
||
| def path_handler(path) | ||
| router_path = @path.split('/') | ||
| request = path.split('/') | ||
| return if request.size != router_path.size | ||
|
|
||
| router_path.each_with_index do |part, id| | ||
| if part.include?(':') && request[id].to_i.positive? | ||
| params[sym!(part)] = request[id].to_i | ||
| elsif part != request[id] | ||
| return | ||
| end | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def sym!(part) | ||
| part.delete(':').to_sym | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,20 @@ | ||
| require 'erb' | ||
| require 'json' | ||
|
|
||
| module Simpler | ||
| class View | ||
|
|
||
| VIEW_BASE_PATH = 'app/views'.freeze | ||
|
|
||
| def initialize(env) | ||
| def initialize(env, response) | ||
| @env = env | ||
| @response = response | ||
| end | ||
|
|
||
| def render(binding) | ||
| template = File.read(template_path) | ||
| return send template.keys.first, template.values.first if template&.keys | ||
|
|
||
| template = File.read(template_path) | ||
| ERB.new(template).result(binding) | ||
| end | ||
|
|
||
|
|
@@ -30,10 +33,20 @@ def template | |
| end | ||
|
|
||
| def template_path | ||
| path = template || [controller.name, action].join('/') | ||
|
|
||
| path = [controller.name, action].join('/') | ||
| @env['simpler.template_path'] = "#{path}.html.erb" | ||
| Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") | ||
| end | ||
|
|
||
| def plain(content) | ||
|
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. Можно сделать отдельный класс для рендеринга каждого типа ответа: HTMLRenderer, PlainTextRenderrer. Каждый из таких классов может определять свой метод render. Тогда в обязанность View будет входить выбор нужного класса на основе информации из env и делегирование ему вызова метода render. |
||
| @response.headers['Content-Type'] = 'text/plain' | ||
| content | ||
| end | ||
|
|
||
| def json(content) | ||
| @response.headers['Content-Type'] = 'json' | ||
| content.to_json | ||
| end | ||
|
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| require 'logger' | ||
| class SimplerLogger | ||
| def initialize(app) | ||
| @app = app | ||
| end | ||
|
|
||
| def call(env) | ||
| @request = Rack::Request.new(env) | ||
| status, headers, body = @app.call(env) | ||
| logger = Logger.new('log/app.log') | ||
| if status == 404 | ||
| logger.error("\n#{log_error(@request, status, headers)}") | ||
| else | ||
| logger.info("\n#{log_info(@request, status, headers)}") | ||
| end | ||
| [status, headers, body] | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def log_info(request, status, headers) | ||
| "Request: #{request.request_method} #{request.fullpath}\n" \ | ||
| "Handler: #{request.env['simpler.controller'].class}##{request.env['simpler.action']}\n" \ | ||
| "Parameters: #{request.env['simpler.params'].merge!(request.params)}\n" \ | ||
| "Response: #{status} #{headers['Content-Type']} #{request.env['simpler.template_path']}\n" \ | ||
| end | ||
|
|
||
| def log_error(request, status, headers) | ||
| "Request: #{request.request_method} #{request.fullpath}\n" \ | ||
| "Response: #{status} #{headers['Content-Type']}\n" \ | ||
| end | ||
| end |
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.
Rack::Response?