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
2 changes: 2 additions & 0 deletions .gitgnore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.byebug
log/app.log
6 changes: 2 additions & 4 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
class TestsController < Simpler::Controller

def index
@time = Time.now
end

def create

def show
@id = params[:id]
end

end
1 change: 0 additions & 1 deletion app/models/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
# Integer :level, default: 0
# end
class Test < Sequel::Model

end
3 changes: 1 addition & 2 deletions app/views/tests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
</head>
<body>
<h1>Simpler framework at work!</h1>

<p><%= @time %></p>
<p><%= @time %></p>
</body>
</html>
2 changes: 1 addition & 1 deletion app/views/tests/list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<html>
<head>
<meta charset="utf-8">
<link rel="shortcut icon" href="#" />
<title>List | Simpler application</title>
</head>
<body>
<h1>Simpler framework at work!</h1>

<p><%= @time %></p>
</body>
</html>
11 changes: 11 additions & 0 deletions app/views/tests/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="shortcut icon" href="#" />
<title>Index | Simpler application</title>
</head>
<body>
<h3>Test ID: <%= @id %></h3>
</body>
</html>
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative 'config/environment'

use Rack::Reloader, 0
use SimplerLogger, logdev: 'log/app.log'
run Simpler.application
1 change: 1 addition & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative '../lib/simpler'
require_relative '../lib/middleware/simpler_logger'

Simpler.application.bootstrap!
1 change: 1 addition & 0 deletions config/routes.rb
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
35 changes: 35 additions & 0 deletions lib/middleware/simpler_logger.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions lib/simpler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require_relative 'simpler/application'

module Simpler

class << self
def application
Application.instance
Expand All @@ -12,5 +11,4 @@ def root
Pathname.new(File.expand_path('..', __dir__))
end
end

end
17 changes: 13 additions & 4 deletions lib/simpler/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

module Simpler
class Application

include Singleton

attr_reader :db
Expand All @@ -29,9 +28,13 @@ def routes(&block)
def call(env)
route = @router.route_for(env)
controller = route.controller.new(env)
rescue StandardError
invalid_request_path
else
action = route.action
params = route.params

make_response(controller, action)
make_response(controller, action, params)
end

private
Expand All @@ -50,9 +53,15 @@ 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
response = Rack::Response.new
response.status = 404
response.write("Route for this URL not found\n")
response.finish
end
end
end
31 changes: 23 additions & 8 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Simpler
class Controller

attr_reader :name, :request, :response

def initialize(env)
Expand All @@ -11,11 +10,16 @@ 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

set_default_headers
set_params(params)
@request.env['simpler.params'] = self.params

set_headers
set_status

send(action)
write_response

Expand All @@ -28,8 +32,16 @@ def extract_name
self.class.name.match('(?<name>.+)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 set_status(sym = :ok)
@response.status = "#{Rack::Utils::SYMBOL_TO_STATUS_CODE[sym]} #{sym}"
end

def set_params(hash)
hash.each { |key, value| @request.update_param(key, value) }
end

def write_response
Expand All @@ -39,16 +51,19 @@ def write_response
end

def render_body
View.new(@request.env).render(binding)
@request.env['simpler.plain_text'] || View.new(@request.env).render(binding)
end

def params
@request.params
end

def render(template)
@request.env['simpler.template'] = template
if template.is_a? String
@request.env['simpler.template'] = template
elsif template.is_a? Hash
@request.env['simpler.plain_text'] = template[:plain] if template[:plain]
end
end

end
end
2 changes: 0 additions & 2 deletions lib/simpler/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Simpler
class Router

def initialize
@routes = []
end
Expand Down Expand Up @@ -36,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
36 changes: 33 additions & 3 deletions lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
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 && 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(request_path, route_path)
return false unless request_path.size == route_path.size

request_path.each_index do |i|
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])
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
4 changes: 1 addition & 3 deletions lib/simpler/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Simpler
class View

VIEW_BASE_PATH = 'app/views'.freeze

def initialize(env)
Expand Down Expand Up @@ -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
Empty file added log/app.log
Empty file.