From 357c3a69418822e8a650f045149e31fd515f11f6 Mon Sep 17 00:00:00 2001 From: Kakhorov Aziz Date: Sat, 11 Feb 2023 15:58:27 +0500 Subject: [PATCH 1/9] feat: Controller#render plain: "text" --- app/controllers/tests_controller.rb | 3 +++ app/views/tests/index.html.erb | 3 +++ app/views/tests/list.html.erb | 1 + lib/simpler/controller.rb | 11 ++++++++--- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 1526a689..618b79ad 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -2,6 +2,9 @@ class TestsController < Simpler::Controller def index @time = Time.now + + #render 'tests/list' + #render plain: 'Just simple text' end def create diff --git a/app/views/tests/index.html.erb b/app/views/tests/index.html.erb index 39fce580..0fd200ad 100644 --- a/app/views/tests/index.html.erb +++ b/app/views/tests/index.html.erb @@ -2,11 +2,14 @@ + Index | Simpler application

Simpler framework at work!

<%= @time %>

+ + \ No newline at end of file diff --git a/app/views/tests/list.html.erb b/app/views/tests/list.html.erb index 0d430491..e9b79d6f 100644 --- a/app/views/tests/list.html.erb +++ b/app/views/tests/list.html.erb @@ -2,6 +2,7 @@ + List | Simpler application diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 9383b035..6083a01a 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -39,7 +39,11 @@ def write_response end def render_body - View.new(@request.env).render(binding) + if @request.env['simpler.plain_text'] + @request.env['simpler.plain_text'] + else + View.new(@request.env).render(binding) + end end def params @@ -47,8 +51,9 @@ def params end def render(template) - @request.env['simpler.template'] = template - end + @request.env['simpler.template'] = template if template.class == String + @request.env['simpler.plain_text'] = template[:plain] if template.has_key? :plain + end end end From 12aa37894180d493d3b34fde4f6f5492ff3aa587 Mon Sep 17 00:00:00 2001 From: Kakhorov Aziz Date: Sat, 11 Feb 2023 16:19:38 +0500 Subject: [PATCH 2/9] fix: Controller#render(template) --- .byebug_history | 8 ++++++++ app/controllers/tests_controller.rb | 4 ++-- lib/simpler/controller.rb | 9 ++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 .byebug_history diff --git a/.byebug_history b/.byebug_history new file mode 100644 index 00000000..5049286f --- /dev/null +++ b/.byebug_history @@ -0,0 +1,8 @@ +c +template[:plain] +template.class +c +template[:plain] +@request.env['simpler.plain_text'] +template.class +template diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 618b79ad..05564371 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -2,9 +2,9 @@ class TestsController < Simpler::Controller def index @time = Time.now + render plain: 'Just simple text' - #render 'tests/list' - #render plain: 'Just simple text' + render 'tests/list' end def create diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 6083a01a..140a8c42 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -1,4 +1,5 @@ require_relative 'view' +require 'byebug' module Simpler class Controller @@ -51,9 +52,11 @@ def params end def render(template) - @request.env['simpler.template'] = template if template.class == String - - @request.env['simpler.plain_text'] = template[:plain] if template.has_key? :plain + if template.class == String + @request.env['simpler.template'] = template + elsif template.class == Hash + @request.env['simpler.plain_text'] = template[:plain] if template[:plain] + end end end end From 4d920e3e7152c8c82111567ad0e75de1bba61184 Mon Sep 17 00:00:00 2001 From: Kakhorov Aziz Date: Mon, 13 Feb 2023 08:55:51 +0500 Subject: [PATCH 3/9] contin work --- app/controllers/tests_controller.rb | 3 ++- config.ru | 1 + lib/simpler/controller.rb | 12 +++++++++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 05564371..5168fa31 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -3,8 +3,9 @@ class TestsController < Simpler::Controller def index @time = Time.now render plain: 'Just simple text' - + status :bad_request render 'tests/list' + set_headers 'text/plain' end def create diff --git a/config.ru b/config.ru index 3060cc20..decfafaa 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,4 @@ require_relative 'config/environment' +use Rack::Reloader, 0 run Simpler.application diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 140a8c42..c6ebf99f 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -16,7 +16,9 @@ def make_response(action) @request.env['simpler.controller'] = self @request.env['simpler.action'] = action - set_default_headers + set_headers + status + send(action) write_response @@ -29,8 +31,12 @@ def extract_name self.class.name.match('(?.+)Controller')[:name].downcase end - def set_default_headers - @response['Content-Type'] = 'text/html' + def set_headers(type = 'text/html') + @response['Content-Type'] = type + end + + def status(sym = :ok) + @response.status = Rack::Utils::SYMBOL_TO_STATUS_CODE[sym] end def write_response From 7c2c8755c17f715a755045150b97a38e744d7c73 Mon Sep 17 00:00:00 2001 From: Kakhorov Aziz Date: Tue, 14 Feb 2023 23:00:00 +0500 Subject: [PATCH 4/9] feat: resque when request path hasn't match with routes --- .byebug_history | 5 +++++ app/controllers/tests_controller.rb | 2 +- lib/simpler/application.rb | 11 ++++++++++- lib/simpler/controller.rb | 8 ++++---- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/.byebug_history b/.byebug_history index 5049286f..e295c515 100644 --- a/.byebug_history +++ b/.byebug_history @@ -1,4 +1,9 @@ c +@routes.find { |route| route.match?('get', '/pest' } +@routes.find { |route| route.match?('get', '/pest') +@routes +path +c template[:plain] template.class c diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 5168fa31..aaf85e0c 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -3,7 +3,7 @@ class TestsController < Simpler::Controller def index @time = Time.now render plain: 'Just simple text' - status :bad_request + set_status :bad_request render 'tests/list' set_headers 'text/plain' end diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 711946a9..6f762455 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -28,7 +28,10 @@ def routes(&block) def call(env) route = @router.route_for(env) - controller = route.controller.new(env) + controller = route.controller.new(env) + rescue + invalid_request_path + else action = route.action make_response(controller, action) @@ -54,5 +57,11 @@ def make_response(controller, action) controller.make_response(action) end + def invalid_request_path + response = Rack::Response.new + response.status = 404 + response.write("Route for this URL not found") + response.finish + end end end diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index c6ebf99f..a719c4f1 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -17,7 +17,7 @@ def make_response(action) @request.env['simpler.action'] = action set_headers - status + set_status send(action) write_response @@ -35,7 +35,7 @@ def set_headers(type = 'text/html') @response['Content-Type'] = type end - def status(sym = :ok) + def set_status(sym = :ok) @response.status = Rack::Utils::SYMBOL_TO_STATUS_CODE[sym] end @@ -58,9 +58,9 @@ def params end def render(template) - if template.class == String + if template.is_a? String @request.env['simpler.template'] = template - elsif template.class == Hash + elsif template.is_a? Hash @request.env['simpler.plain_text'] = template[:plain] if template[:plain] end end From 3cfd3bd63ae380528f8de2a712632da657194ef4 Mon Sep 17 00:00:00 2001 From: Kakhorov Aziz Date: Wed, 15 Feb 2023 14:41:12 +0500 Subject: [PATCH 5/9] work with parsing path --- .byebug_history | 13 +++++++++++++ config/routes.rb | 2 ++ lib/simpler/router.rb | 3 +++ lib/simpler/router/route.rb | 3 ++- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.byebug_history b/.byebug_history index e295c515..a63fcd7e 100644 --- a/.byebug_history +++ b/.byebug_history @@ -1,4 +1,17 @@ c +@routes[1] +@routes[2] +path +c +@request.path +@request.params +c +path +method +c +path +clear +c @routes.find { |route| route.match?('get', '/pest' } @routes.find { |route| route.match?('get', '/pest') @routes diff --git a/config/routes.rb b/config/routes.rb index 4a751251..9cf254ce 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Simpler.application.routes do get '/tests', 'tests#index' post '/tests', 'tests#create' + get '/tests/:id', 'tests#show' end + \ No newline at end of file diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index 14b3415c..9bb4c6ca 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -1,4 +1,5 @@ require_relative 'router/route' +require 'byebug' module Simpler class Router @@ -18,6 +19,8 @@ def post(path, route_point) def route_for(env) method = env['REQUEST_METHOD'].downcase.to_sym path = env['PATH_INFO'] + +#byebug @routes.find { |route| route.match?(method, path) } end diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 4c66b4b7..39ba9cb4 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -12,7 +12,8 @@ def initialize(method, path, controller, action) end def match?(method, path) - @method == method && path.match(@path) + path_regexp = /^#{@path}$/ + @method == method && path.match(path_regexp) end end From 9d0ac3989846984d7b03a33462f48f7fcc97e2a9 Mon Sep 17 00:00:00 2001 From: Kakhorov Aziz Date: Thu, 16 Feb 2023 10:47:02 +0500 Subject: [PATCH 6/9] under work --- lib/simpler/application.rb | 2 +- lib/simpler/controller.rb | 2 ++ lib/simpler/router/route.rb | 21 +++++++++++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 6f762455..ff2dd9cc 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -60,7 +60,7 @@ def make_response(controller, action) def invalid_request_path response = Rack::Response.new response.status = 404 - response.write("Route for this URL not found") + response.write("Route for this URL not found\n") response.finish end end diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index a719c4f1..d3aa0003 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -16,6 +16,8 @@ def make_response(action) @request.env['simpler.controller'] = self @request.env['simpler.action'] = action + byebug + set_headers set_status diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 39ba9cb4..2827905a 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -9,13 +9,30 @@ def initialize(method, path, controller, action) @path = path @controller = controller @action = action + @params = {} end def match?(method, path) - path_regexp = /^#{@path}$/ - @method == method && path.match(path_regexp) + @method == method && match_path(path) end + private + + def path_to_array(any_path) + any_path.split('/').reject!{|i| i.empty?} + end + + def match_path(path) + path_request = path_to_array(path) + path_route = path_to_array(@path) + compare(path_request, path_route) + end + + def compare(req_p, route_p) + return false unless req_p.size == route_p.size + req_p.each_index do |i| + + end end end end From 41ae62187d9c8bb8143e1787380b4580ea0fd7aa Mon Sep 17 00:00:00 2001 From: Kakhorov Aziz Date: Thu, 16 Feb 2023 10:47:40 +0500 Subject: [PATCH 7/9] under work2 --- lib/simpler/router/route.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index 2827905a..b0891efb 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -31,7 +31,7 @@ def match_path(path) def compare(req_p, route_p) return false unless req_p.size == route_p.size req_p.each_index do |i| - + end end end end From 6ee922f9eb0b659d9caee3b837076e061f6ebe2c Mon Sep 17 00:00:00 2001 From: Kakhorov Aziz Date: Fri, 17 Feb 2023 23:38:42 +0500 Subject: [PATCH 8/9] feat: action show id --- .byebug_history | 39 +++++++++++++++++++++++++++++ app/controllers/tests_controller.rb | 6 +++++ app/views/tests/show.html.erb | 15 +++++++++++ lib/simpler/application.rb | 8 +++--- lib/simpler/controller.rb | 10 +++++--- lib/simpler/router.rb | 2 -- lib/simpler/router/route.rb | 27 ++++++++++++++++---- 7 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 app/views/tests/show.html.erb diff --git a/.byebug_history b/.byebug_history index a63fcd7e..6f0a7379 100644 --- a/.byebug_history +++ b/.byebug_history @@ -1,4 +1,43 @@ c +@id +c +params +@id +id +c +params +@id +c +n +params +c +@request.params +c +@request.params +@request.env['params'] +c +route +c +@params +c +request_path[1] +route_path[1] +route_path[0] +route_path +c +n +path_request +path_route +c +path_route +path_request +c +n +p +c +path +method +c @routes[1] @routes[2] path diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index aaf85e0c..068b1162 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -1,3 +1,4 @@ +require 'byebug' class TestsController < Simpler::Controller def index @@ -12,4 +13,9 @@ def create end + def show + @id = params[:id] + byebug + end + end diff --git a/app/views/tests/show.html.erb b/app/views/tests/show.html.erb new file mode 100644 index 00000000..07e043e1 --- /dev/null +++ b/app/views/tests/show.html.erb @@ -0,0 +1,15 @@ + + + + + + Index | Simpler application + + +

Simpler framework at work!

+ +

Test ID: <%= @id %>

+ + + + \ No newline at end of file diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index ff2dd9cc..70862aad 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -3,6 +3,7 @@ require 'sequel' require_relative 'router' require_relative 'controller' +require 'byebug' module Simpler class Application @@ -33,8 +34,9 @@ def call(env) invalid_request_path else action = route.action + params = route.params - make_response(controller, action) + make_response(controller, action, params) end private @@ -53,8 +55,8 @@ def setup_database @db = Sequel.connect(database_config) end - def make_response(controller, action) - controller.make_response(action) + def make_response(controller, action, params) + controller.make_response(action,params) end def invalid_request_path diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index d3aa0003..2bff314d 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -12,12 +12,12 @@ def initialize(env) @response = Rack::Response.new end - def make_response(action) + def make_response(action,params) @request.env['simpler.controller'] = self @request.env['simpler.action'] = action - - byebug + set_params(params) + set_headers set_status @@ -41,6 +41,10 @@ def set_status(sym = :ok) @response.status = Rack::Utils::SYMBOL_TO_STATUS_CODE[sym] end + def set_params(hash) + hash.each {|key,value| @request.update_param(key, value) } + end + def write_response body = render_body diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index 9bb4c6ca..a913a099 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -19,8 +19,6 @@ def post(path, route_point) def route_for(env) method = env['REQUEST_METHOD'].downcase.to_sym path = env['PATH_INFO'] - -#byebug @routes.find { |route| route.match?(method, path) } end diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index b0891efb..ad67cf09 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -1,8 +1,10 @@ +require 'byebug' + module Simpler class Router class Route - attr_reader :controller, :action + attr_reader :controller, :action, :params def initialize(method, path, controller, action) @method = method @@ -14,25 +16,40 @@ def initialize(method, path, controller, action) def match?(method, path) @method == method && match_path(path) + end private def path_to_array(any_path) - any_path.split('/').reject!{|i| i.empty?} + any_path.split('/').reject!{|i| i.empty?} end def match_path(path) path_request = path_to_array(path) path_route = path_to_array(@path) + compare(path_request, path_route) end - def compare(req_p, route_p) - return false unless req_p.size == route_p.size - req_p.each_index do |i| + def compare(request_path, route_path) + return false unless request_path.size == route_path.size + + request_path.each_index do |i| + if route_path[i] == request_path[i] + next + end + + return false unless route_path[i] =~ /^:id$/ + get_params_from_request(route_path[i],request_path[i]) end end + + def get_params_from_request(key, value) + key = key[1..-1] if key.chr == ":" + key_sym = key.intern + @params[key_sym] = value + end end end end From 21d28a93dc7985a439f94dc19e0ad984eb240ab5 Mon Sep 17 00:00:00 2001 From: Kakhorov Aziz Date: Sat, 18 Feb 2023 14:48:25 +0500 Subject: [PATCH 9/9] deat: middleware simpler_logger --- .byebug_history | 65 ----------------------------- .gitgnore | 2 + app/controllers/tests_controller.rb | 12 ------ app/models/test.rb | 1 - app/views/tests/index.html.erb | 6 +-- app/views/tests/list.html.erb | 1 - app/views/tests/show.html.erb | 6 +-- config.ru | 1 + config/environment.rb | 1 + config/routes.rb | 1 - lib/middleware/simpler_logger.rb | 35 ++++++++++++++++ lib/simpler.rb | 2 - lib/simpler/application.rb | 8 ++-- lib/simpler/controller.rb | 19 ++++----- lib/simpler/router.rb | 3 -- lib/simpler/router/route.rb | 19 ++++----- lib/simpler/view.rb | 4 +- log/app.log | 0 18 files changed, 59 insertions(+), 127 deletions(-) delete mode 100644 .byebug_history create mode 100644 .gitgnore create mode 100644 lib/middleware/simpler_logger.rb create mode 100644 log/app.log diff --git a/.byebug_history b/.byebug_history deleted file mode 100644 index 6f0a7379..00000000 --- a/.byebug_history +++ /dev/null @@ -1,65 +0,0 @@ -c -@id -c -params -@id -id -c -params -@id -c -n -params -c -@request.params -c -@request.params -@request.env['params'] -c -route -c -@params -c -request_path[1] -route_path[1] -route_path[0] -route_path -c -n -path_request -path_route -c -path_route -path_request -c -n -p -c -path -method -c -@routes[1] -@routes[2] -path -c -@request.path -@request.params -c -path -method -c -path -clear -c -@routes.find { |route| route.match?('get', '/pest' } -@routes.find { |route| route.match?('get', '/pest') -@routes -path -c -template[:plain] -template.class -c -template[:plain] -@request.env['simpler.plain_text'] -template.class -template diff --git a/.gitgnore b/.gitgnore new file mode 100644 index 00000000..22be9915 --- /dev/null +++ b/.gitgnore @@ -0,0 +1,2 @@ +.byebug +log/app.log diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb index 068b1162..9293be85 100644 --- a/app/controllers/tests_controller.rb +++ b/app/controllers/tests_controller.rb @@ -1,21 +1,9 @@ -require 'byebug' class TestsController < Simpler::Controller - def index @time = Time.now - render plain: 'Just simple text' - set_status :bad_request - render 'tests/list' - set_headers 'text/plain' - end - - def create - end def show @id = params[:id] - byebug end - end diff --git a/app/models/test.rb b/app/models/test.rb index 86376668..98cea0cb 100644 --- a/app/models/test.rb +++ b/app/models/test.rb @@ -4,5 +4,4 @@ # Integer :level, default: 0 # end class Test < Sequel::Model - end diff --git a/app/views/tests/index.html.erb b/app/views/tests/index.html.erb index 0fd200ad..c6fbad57 100644 --- a/app/views/tests/index.html.erb +++ b/app/views/tests/index.html.erb @@ -2,14 +2,10 @@ - Index | Simpler application

Simpler framework at work!

- -

<%= @time %>

- - +

<%= @time %>

\ No newline at end of file diff --git a/app/views/tests/list.html.erb b/app/views/tests/list.html.erb index e9b79d6f..8ff5af24 100644 --- a/app/views/tests/list.html.erb +++ b/app/views/tests/list.html.erb @@ -7,7 +7,6 @@

Simpler framework at work!

-

<%= @time %>

\ No newline at end of file diff --git a/app/views/tests/show.html.erb b/app/views/tests/show.html.erb index 07e043e1..e79e720a 100644 --- a/app/views/tests/show.html.erb +++ b/app/views/tests/show.html.erb @@ -6,10 +6,6 @@ Index | Simpler application -

Simpler framework at work!

- -

Test ID: <%= @id %>

- - +

Test ID: <%= @id %>

\ No newline at end of file diff --git a/config.ru b/config.ru index decfafaa..059e2cd1 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,5 @@ require_relative 'config/environment' use Rack::Reloader, 0 +use SimplerLogger, logdev: 'log/app.log' run Simpler.application diff --git a/config/environment.rb b/config/environment.rb index 7a0d38c3..06955542 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,3 +1,4 @@ require_relative '../lib/simpler' +require_relative '../lib/middleware/simpler_logger' Simpler.application.bootstrap! diff --git a/config/routes.rb b/config/routes.rb index 9cf254ce..1700ff3a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,4 +3,3 @@ post '/tests', 'tests#create' get '/tests/:id', 'tests#show' end - \ No newline at end of file diff --git a/lib/middleware/simpler_logger.rb b/lib/middleware/simpler_logger.rb new file mode 100644 index 00000000..c6943636 --- /dev/null +++ b/lib/middleware/simpler_logger.rb @@ -0,0 +1,35 @@ +require 'logger' + +class SimplerLogger + def initialize(app, **options) + @logger = Logger.new(options[:logdev] || STDOUT) + @app = app + end + + def call(env) + request = Rack::Request.new(env) + response = @app.call(env) + + write_log(request, response) + + response + end + + def write_log(request, response) + time = Time.now + method = request.request_method + url = request.fullpath + controller = request.env['simpler.controller'].class + action = request.env['simpler.action'] + params = request.env['simpler.params'] || '-' + status = response[0] + type_response = response[1]['Content-Type'] + template = request.env['simpler.view_template'] + + @logger << "#{time} + Requiest: #{method} #{url} + Handler: #{controller}##{action} + Parameters: #{params} + Respponse: #{status} [#{type_response}] #{template}\n" + end +end diff --git a/lib/simpler.rb b/lib/simpler.rb index f9dfe3c4..d4d365c9 100644 --- a/lib/simpler.rb +++ b/lib/simpler.rb @@ -2,7 +2,6 @@ require_relative 'simpler/application' module Simpler - class << self def application Application.instance @@ -12,5 +11,4 @@ def root Pathname.new(File.expand_path('..', __dir__)) end end - end diff --git a/lib/simpler/application.rb b/lib/simpler/application.rb index 70862aad..7cf30789 100644 --- a/lib/simpler/application.rb +++ b/lib/simpler/application.rb @@ -3,11 +3,9 @@ require 'sequel' require_relative 'router' require_relative 'controller' -require 'byebug' module Simpler class Application - include Singleton attr_reader :db @@ -29,8 +27,8 @@ def routes(&block) def call(env) route = @router.route_for(env) - controller = route.controller.new(env) - rescue + controller = route.controller.new(env) + rescue StandardError invalid_request_path else action = route.action @@ -56,7 +54,7 @@ def setup_database end def make_response(controller, action, params) - controller.make_response(action,params) + controller.make_response(action, params) end def invalid_request_path diff --git a/lib/simpler/controller.rb b/lib/simpler/controller.rb index 2bff314d..29c23659 100644 --- a/lib/simpler/controller.rb +++ b/lib/simpler/controller.rb @@ -1,9 +1,7 @@ require_relative 'view' -require 'byebug' module Simpler class Controller - attr_reader :name, :request, :response def initialize(env) @@ -12,11 +10,12 @@ def initialize(env) @response = Rack::Response.new end - def make_response(action,params) + def make_response(action, params) @request.env['simpler.controller'] = self @request.env['simpler.action'] = action - + set_params(params) + @request.env['simpler.params'] = self.params set_headers set_status @@ -38,11 +37,11 @@ def set_headers(type = 'text/html') end def set_status(sym = :ok) - @response.status = Rack::Utils::SYMBOL_TO_STATUS_CODE[sym] + @response.status = "#{Rack::Utils::SYMBOL_TO_STATUS_CODE[sym]} #{sym}" end def set_params(hash) - hash.each {|key,value| @request.update_param(key, value) } + hash.each { |key, value| @request.update_param(key, value) } end def write_response @@ -52,11 +51,7 @@ def write_response end def render_body - if @request.env['simpler.plain_text'] - @request.env['simpler.plain_text'] - else - View.new(@request.env).render(binding) - end + @request.env['simpler.plain_text'] || View.new(@request.env).render(binding) end def params @@ -65,7 +60,7 @@ def params def render(template) if template.is_a? String - @request.env['simpler.template'] = template + @request.env['simpler.template'] = template elsif template.is_a? Hash @request.env['simpler.plain_text'] = template[:plain] if template[:plain] end diff --git a/lib/simpler/router.rb b/lib/simpler/router.rb index a913a099..b05449b3 100644 --- a/lib/simpler/router.rb +++ b/lib/simpler/router.rb @@ -1,9 +1,7 @@ require_relative 'router/route' -require 'byebug' module Simpler class Router - def initialize @routes = [] end @@ -37,6 +35,5 @@ def add_route(method, path, route_point) def controller_from_string(controller_name) Object.const_get("#{controller_name.capitalize}Controller") end - end end diff --git a/lib/simpler/router/route.rb b/lib/simpler/router/route.rb index ad67cf09..87191e24 100644 --- a/lib/simpler/router/route.rb +++ b/lib/simpler/router/route.rb @@ -1,9 +1,6 @@ -require 'byebug' - module Simpler class Router class Route - attr_reader :controller, :action, :params def initialize(method, path, controller, action) @@ -16,19 +13,18 @@ def initialize(method, path, controller, action) def match?(method, path) @method == method && match_path(path) - end private def path_to_array(any_path) - any_path.split('/').reject!{|i| i.empty?} + any_path.split('/').reject! { |i| i.empty? } end def match_path(path) path_request = path_to_array(path) path_route = path_to_array(@path) - + compare(path_request, path_route) end @@ -36,17 +32,16 @@ def compare(request_path, route_path) return false unless request_path.size == route_path.size request_path.each_index do |i| - if route_path[i] == request_path[i] - next - end - + next if route_path[i] == request_path[i] + return false unless route_path[i] =~ /^:id$/ - get_params_from_request(route_path[i],request_path[i]) + + get_params_from_request(route_path[i], request_path[i]) end end def get_params_from_request(key, value) - key = key[1..-1] if key.chr == ":" + key = key[1..-1] if key.chr == ':' key_sym = key.intern @params[key_sym] = value end diff --git a/lib/simpler/view.rb b/lib/simpler/view.rb index 19a73b34..6aa2b06e 100644 --- a/lib/simpler/view.rb +++ b/lib/simpler/view.rb @@ -2,7 +2,6 @@ module Simpler class View - VIEW_BASE_PATH = 'app/views'.freeze def initialize(env) @@ -31,9 +30,8 @@ def template def template_path path = template || [controller.name, action].join('/') - + @env['simpler.view_template'] = "#{path}.html.erb" Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb") end - end end diff --git a/log/app.log b/log/app.log new file mode 100644 index 00000000..e69de29b